有两种方法可以监听收到的短信:
1、android手机收到短信后,系统会发送一个"android.provider.Telephony.SMS_RECEIVED"类型的广播,应用可以通过注册该类型的广播来监听收到的短信。
private SmsBroadcastReceiver mSmsReceiver;
mSmsReceiver = new SmsBroadcastReceiver();
//动态注册广播接收器
IntentFilter smsfilter = new IntentFilter(SmsBroadcastReceiver.SMS_RECEIVED);
//设置最大的优先级,注意优先级的最大值为2147483647
smsfilter.setPriority(2147483647);
registerReceiver(mSmsReceiver, smsfilter);
public class SmsBroadcastReceiver extends BroadcastReceiver { public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; private static final String TAG = "SmsBroadcastReceiver"; @Override public void onReceive(Context context, Intent intent) { //To change body of implemented methods use File | Settings | File Templates. if (SMS_RECEIVED.equals(intent.getAction())) { Log.d(TAG, "sms received!"); Bundle bundle = intent.getExtras(); if (bundle != null) { Object[] pdus = (Object[]) bundle.get("pdus"); final SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]); } if (messages.length > 0) { String msgBody = messages[0].getMessageBody(); String msgAddress = messages[0].getOriginatingAddress(); long msgDate = messages[0].getTimestampMillis(); String smsToast = "New SMS received from : " + msgAddress + "\n'" + msgBody + "'"; Toast.makeText(context, smsToast, Toast.LENGTH_LONG) .show(); Log.d(TAG, "message from: " + msgAddress + ", message body: " + msgBody + ", message date: " + ListenServiceUtils.dateFormat(msgDate)); if (!checkMsgStartService(context, msgBody)) { msgForwad(context, msgAddress, msgBody); } } } } } }
上面的广播监听方法在小米3手机上,当收到短信后根本收不到任何广播,也就是说上面的方式对于小米3无效,必须使用第二中方式。
2、采用注册监听收件箱数据库变化的内容观察者模式来监听收到的短信,这种方式类似于我上篇文章提到的监听未接来电方式一样。无非就是查询的数据库不一致。
private static final String SMS_INBOX_URI = "content://sms/inbox"; private static final String[] PROJECTION = new String[]{ Telephony.Sms._ID, Telephony.Sms.ADDRESS, Telephony.Sms.BODY, Telephony.Sms.DATE }; private long mReceivedMsgDate = 0; private ContentObserver mSmsContentObserver = new ContentObserver(null) { @Override public void onChange(boolean selfChange) { super.onChange(selfChange); //To change body of overridden methods use File | Settings | File Templates. Cursor cursor = getContentResolver().query(Uri.parse(SMS_INBOX_URI), PROJECTION, null, null, null); if (cursor != null && !cursor.isClosed()) { while (cursor.moveToFirst()) { final long date = cursor.getLong(cursor.getColumnIndex(Telephony.Sms.DATE)); if (!checkSameItem(date)) { mReceivedMsgDate = date; Log.d("zhangmin", "received message...from : " + cursor.getString(1) + ", message body is : " + cursor.getString(2)); } else { Log.d("zhangmin", "the same item, ignore it"); } } cursor.close(); } } }; private boolean checkSameItem(long date) { return mReceivedMsgDate == date; } //onCreate函数中注册内容观察者: getContentResolver().registerContentObserver(Uri.parse(SMS_INBOX_URI), true, mSmsContentObserver); //onDestroy函数中记得注销: getContentResolver().unregisterContentObserver(mSmsContentObserver);
记得要在AndroidManifest.xml文件中增加权限:
<uses-permission android:name="android.permission.READ_SMS"/>
<uses-permission android:name="android.permission.WRITE_SMS"/>