我正在尝试从这样的交付广播接收器Intent
中的 PDU 中找出接收短信的人的电话号码onReceive()
@Override
public void onReceive(Context context, Intent intent) {
ThizLog.d(TAG, "Inside onReceive");
Bundle bundle = intent.getExtras();
Object[] messages = (Object[]) bundle.get("pdus");
SmsMessage[] sms = new SmsMessage[messages.length];
for (int n = 0; n < messages.length; n++) {
sms[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
}
for (SmsMessage msg : sms) {
// Verify if the message came from our known sender
ThizLog.d(TAG,"!!! " + msg.getOriginatingAddress());
}
switch (getResultCode()) {
case Activity.RESULT_OK:
// SMS delivered
break;
case Activity.RESULT_CANCELED:
/*
* SMS not delivered, show SMS_NOT_DELIVERED_MESSAGE
*/
Toast toast = Toast.makeText(context, SMS_NOT_DELIVERED_MESSAGE, Toast.LENGTH_LONG);
// shows toast on left top corner increase the zero value to
// slide it right and down
//toast.setGravity(Gravity.TOP | Gravity.LEFT, 0, 0);
toast.show();
break;
}
}
但这导致了以下错误
02-04 03:19:34.008: E/AndroidRuntime(21881): FATAL EXCEPTION: main
02-04 03:19:34.008: E/AndroidRuntime(21881): java.lang.RuntimeException: Unable to start receiver com.abc.mnp.xyz.DeliveredSMSReceiver: java.lang.NullPointerException
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2827)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.app.ActivityThread.access$3200(ActivityThread.java:126)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2088)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.os.Looper.loop(Looper.java:123)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.app.ActivityThread.main(ActivityThread.java:4633)
02-04 03:19:34.008: E/AndroidRuntime(21881): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 03:19:34.008: E/AndroidRuntime(21881): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 03:19:34.008: E/AndroidRuntime(21881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
02-04 03:19:34.008: E/AndroidRuntime(21881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
02-04 03:19:34.008: E/AndroidRuntime(21881): at dalvik.system.NativeStart.main(Native Method)
02-04 03:19:34.008: E/AndroidRuntime(21881): Caused by: java.lang.NullPointerException
02-04 03:19:34.008: E/AndroidRuntime(21881): at com.abc.mnp.xyz.DeliveredSMSReceiver.onReceive(DeliveredSMSReceiver.java:37)
02-04 03:19:34.008: E/AndroidRuntime(21881): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2816)
02-04 03:19:34.008: E/AndroidRuntime(21881): ... 10 more
有可能,Android 的原生 SMS 应用程序可以做到这一点。