Android實現(xiàn)雙模(CDMA/GSM)手機短信監(jiān)聽的方法
本文實例講述了Android實現(xiàn)雙模(CDMA/GSM)手機短信監(jiān)聽的方法。分享給大家供大家參考,具體如下:
一、問題分析:
最近在做一個通過短信遠(yuǎn)程啟動應(yīng)用的功能,要用到短信監(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{ /*當(dāng)收到短信時,就會觸發(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"); //得到由短信內(nèi)容組成的數(shù)組對象 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]; //得到短信內(nèi)容,內(nèi)容是以pdu格式存放的 messages[i] = SmsMessage.createFromPdu(pdu); } for(SmsMessage msg:messages){ String smscontent = msg.getMessageBody(); //得到短信內(nèi)容 String smssender = msg.getOriginatingAddress(); //得到短信發(fā)送者的手機號 } } } } }
實際應(yīng)用時發(fā)現(xiàn)雙模手機對接收到的短信處理時總是在SmsMessage.createFromPdu的地方出現(xiàn)異常,異常信息:
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()); @Override public 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); } } /** * 短信內(nèi)容觀察者 * @author sinber * */ private class SmsContentObserver extends ContentObserver{ public SmsContentObserver(Handler handler) { super(handler); } /** * @Description 當(dāng)短信表發(fā)送改變時,調(diào)用該方法 * 需要兩種權(quán)限 *<li>android.permission.READ_SMS讀取短信 </li> *<li>android.permission.WRITE_SMS寫短信 </li> * @Author sinebr * */ @Override public 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】")){ //此處略去啟動應(yīng)用的代碼 hasDone = true; break; } if (hasDone){ break; } } } }catch(Exception e){ e.printStackTrace(); }finally{ if(cursor!=null) cursor.close(); } } } }
如果是雙模手機,調(diào)用此方法時會產(chǎn)生錯誤,問題就在于源碼的TelephonyManager.getDefault().getPhoneType();該方法的返回值沒有對應(yīng)的雙模手機的類型,而原生的android系統(tǒng)是不支持雙模手機的。
二、解決辦法:
我們可以采用廣播接收者和內(nèi)容觀察者相結(jié)合的方式,直接讀取手機的短信數(shù)據(jù)庫,這樣就避免了錯誤的產(chǎn)生,廢話就不多說了,直接上代碼:
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()); @Override public 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); } } /** * 短信內(nèi)容觀察者 * @author sinber * */ private class SmsContentObserver extends ContentObserver{ public SmsContentObserver(Handler handler) { super(handler); } /** * @Description 當(dāng)短信表發(fā)送改變時,調(diào)用該方法 * 需要兩種權(quán)限 * <li>android.permission.READ_SMS讀取短信 </li> * <li>android.permission.WRITE_SMS寫短信 </li> * @Author sinebr * */ @Override public 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】")){ //此處略去啟動應(yīng)用的代碼 hasDone = true; break; } if (hasDone){ break; } } } }catch(Exception e){ e.printStackTrace(); }finally{ if(cursor!=null) cursor.close(); } } } }
最后別忘了在AndroidManifest.xml中添加相應(yīng)的權(quán)限,
<!-- 接收短信權(quán)限 --> <uses-permission android:name="android.permission.RECEIVE_SMS"/> <!-- 發(fā)送短信權(quán)限 --> <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>
這樣就能適應(yīng)所有的android手機了,無論是雙模還是單模都沒問題,問題解決了。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android數(shù)據(jù)庫操作技巧總結(jié)》、《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android文件操作技巧匯總》、《Android編程開發(fā)之SD卡操作方法匯總》、《Android開發(fā)入門與進(jìn)階教程》、《Android資源操作技巧匯總》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
相關(guān)文章
android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實例介紹
android中選中菜單的顯示跳轉(zhuǎn)和隱式跳轉(zhuǎn)的實例介紹,需要的朋友可以參考一下2013-05-05Android Activity啟動模式之singleTop實例詳解
這篇文章主要介紹了Android Activity啟動模式之singleTop,結(jié)合實例形式較為詳細(xì)的分析了singleTop模式的功能、使用方法與相關(guān)注意事項,需要的朋友可以參考下2016-01-01Android根據(jù)不同身份配置APP對應(yīng)的不同模塊方法
今天小編就為大家分享一篇Android根據(jù)不同身份配置APP對應(yīng)的不同模塊方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-07-07Android TreeView效果實現(xiàn)方法(附demo源碼下載)
這篇文章主要介紹了Android TreeView效果實現(xiàn)方法,結(jié)合實例形式分析了Android TreeView效果的實現(xiàn)原理與具體技巧,并附帶demo源碼供讀者下載,需要的朋友可以參考下2016-02-02Android編程圖片操作類定義與用法示例【拍照,相冊選圖及裁剪】
這篇文章主要介紹了Android編程圖片操作類定義與用法,涉及Android拍照,相冊選圖及裁剪等圖片操作功能及權(quán)限控制相關(guān)操作技巧,需要的朋友可以參考下2018-02-02Android利用ViewPager實現(xiàn)用戶引導(dǎo)界面效果的方法
這篇文章主要介紹了Android利用ViewPager實現(xiàn)用戶引導(dǎo)界面效果的方法,結(jié)合實例形式詳細(xì)分析了Android軟件功能界面的初始化、view實例化、動畫功能實現(xiàn)與布局相關(guān)技巧,需要的朋友可以參考下2016-07-07