本文實例講述了Android實現雙模(CDMA/GSM)手機短信監(jiān)聽的方法。分享給大家供大家參考,具體如下:
一、問題分析:
最近在做一個通過短信遠程啟動應用的功能,要用到短信監(jiān)聽,代碼如下:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.os.Bundle;import android.telephony.SmsMessage;import android.widget.Toast;public class SMSReceiver extends BroadcastReceiver{/*當收到短信時,就會觸發(fā)此方法*/public void onReceive(Context context, Intent intent) {Bundle bundle = intent.getExtras();if(bundle!=null && bundle.get("pdus")!=null){Object[] pdus = (Object[]) bundle.get("pdus");//得到由短信內容組成的數組對象if(pdus!=null && pdus.length>0){SmsMessage[] messages = new SmsMessage[pdus.length];for(int i=0;i<pdus.length;i++){byte[] pdu = (byte[]) pdus[i];//得到短信內容,內容是以pdu格式存放的messages[i] = SmsMessage.createFromPdu(pdu);}for(SmsMessage msg:messages){String smscontent = msg.getMessageBody(); //得到短信內容String smssender = msg.getOriginatingAddress(); //得到短信發(fā)送者的手機號}}}}}實際應用時發(fā)現雙模手機對接收到的短信處理時總是在SmsMessage.createFromPdu的地方出現異常,異常信息:
java.lang.OutOfMemoryError: array size too large
at com.android.internal.telephony.cdma.SmsMessage.parsePdu(SmsMessage.java:658)
at com.android.internal.telephony.cdma.SmsMessage.createFromPdu(SmsMessage.java:116)
at android.telephony.SmsMessage.createFromPdu(SmsMessage.java:162)
而在android的源碼中可以看到createFromPdu方法:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Handler;public class SMSReceiver extends BroadcastReceiver {private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";private Context m_Context;private SmsContentObserver m_Smsobserver = new SmsContentObserver(new Handler());@Overridepublic void onReceive(Context context, Intent intent) {this.m_Context = context;if (intent.getAction().equals(SMS_RECEIVED)) {//注冊短信變化監(jiān)聽context.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, m_Smsobserver);}}/*** 短信內容觀察者* @author sinber**/private class SmsContentObserver extends ContentObserver{public SmsContentObserver(Handler handler) {super(handler);}/*** @Description 當短信表發(fā)送改變時,調用該方法* 需要兩種權限*<li>android.permission.READ_SMS讀取短信 </li>*<li>android.permission.WRITE_SMS寫短信 </li>* @Author sinebr**/@Overridepublic void onChange(boolean selfChange) {super.onChange(selfChange);Cursor cursor = null;try{//讀取收件箱中的短信cursor = m_Context.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "date desc");String body;boolean hasDone = false;if (cursor != null){while (cursor.moveToNext()){body = cursor.getString(cursor.getColumnIndex("body"));if(body != null && body.equals("【startMyActivity】")){//此處略去啟動應用的代碼hasDone = true;break;}if (hasDone){break;}}}}catch(Exception e){e.printStackTrace();}finally{if(cursor!=null)cursor.close();}}}}如果是雙模手機,調用此方法時會產生錯誤,問題就在于源碼的TelephonyManager.getDefault().getPhoneType();該方法的返回值沒有對應的雙模手機的類型,而原生的android系統是不支持雙模手機的。
二、解決辦法:
我們可以采用廣播接收者和內容觀察者相結合的方式,直接讀取手機的短信數據庫,這樣就避免了錯誤的產生,廢話就不多說了,直接上代碼:
import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.database.ContentObserver;import android.database.Cursor;import android.net.Uri;import android.os.Handler;public class SMSReceiver extends BroadcastReceiver {private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";private Context m_Context;private SmsContentObserver m_Smsobserver = new SmsContentObserver(new Handler());@Overridepublic void onReceive(Context context, Intent intent) {this.m_Context = context;if (intent.getAction().equals(SMS_RECEIVED)) {//注冊短信變化監(jiān)聽context.getContentResolver().registerContentObserver(Uri.parse("content://sms/"), true, m_Smsobserver);}}/*** 短信內容觀察者* @author sinber**/private class SmsContentObserver extends ContentObserver{public SmsContentObserver(Handler handler) {super(handler);}/*** @Description 當短信表發(fā)送改變時,調用該方法* 需要兩種權限* <li>android.permission.READ_SMS讀取短信 </li>* <li>android.permission.WRITE_SMS寫短信 </li>* @Author sinebr**/@Overridepublic void onChange(boolean selfChange) {super.onChange(selfChange);Cursor cursor = null;try{//讀取收件箱中的短信cursor = m_Context.getContentResolver().query(Uri.parse("content://sms/inbox"), null, null, null, "date desc");String body;boolean hasDone = false;if (cursor != null){while (cursor.moveToNext()){body = cursor.getString(cursor.getColumnIndex("body"));if(body != null && body.equals("【startMyActivity】")){//此處略去啟動應用的代碼hasDone = true;break;}if (hasDone){break;}}}}catch(Exception e){e.printStackTrace();}finally{if(cursor!=null)cursor.close();}}}}最后別忘了在AndroidManifest.xml中添加相應的權限,
<!-- 接收短信權限 --><uses-permission android:name="android.permission.RECEIVE_SMS"/><!-- 發(fā)送短信權限 --><uses-permission android:name="android.permission.SEND_SMS"/>
還有別忘了注冊廣播接收者:
<receiver android:name=".SMSReceiver"><intent-filter><action android:name="android.provider.Telephony.SMS_RECEIVED"/></intent-filter></receiver>
這樣就能適應所有的android手機了,無論是雙模還是單模都沒問題,問題解決了。
更多關于Android相關內容感興趣的讀者可查看本站專題:《Android數據庫操作技巧總結》、《Android編程之activity操作技巧總結》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結》及《Android控件用法總結》
希望本文所述對大家Android程序設計有所幫助。
新聞熱點
疑難解答