public class SMSReceiver extends BroadcastReceiver { private static String TAG = "SMSReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.d(TAG, "onReceive"); if (intent != null && intent.getAction() != null && "android.provider.Telephony.SMS_RECEIVED".compareToIgnoreCase(intent.getAction()) == 0) { Object[] pduArr = (Object[]) intent.getExtras().get("pdus"); Log.d(TAG, pduArr.length + " messages received"); SmsMessage msg; for (Object pdu : pduArr) { msg = SmsMessage.createFromPdu((byte[]) pdu); Log.d(TAG, "From: " + msg.getOriginatingAddress()); Log.d(TAG, "Msg: " + msg.getMessageBody()); } } else { Log.d(TAG, "Nothing found"); } } }並在 manifest 裡註冊這個 receiver。
<receiver android:name="SMSReceiver"> <intent-filter> <action android:name="android.provider.Telephony.SMS_RECEIVED"></action> </intent-filter> </receiver>最後給予 android.permission.RECEIVE_SMS 的權限就可以。
可以開啟另一個 Emulator 或使用 Emulator Control 來測試,詳細作法參考 在 Android 裡傳送簡訊。
沒有留言:
張貼留言