我已经构建了一个应用程序来读取传入的短信。但每当有任何新的短信出现时,它都会异常。 logcat提供以下消息:
goto
这是错误吗?我怎么解决这个问题?
SmsBroadcastReceiver的代码如下:
10-04 12:18:47.392: E/AndroidRuntime(8225): java.lang.RuntimeException: Unable to start receiver com.commlink.smscheck.SmsBroadcastReceiver: java.lang.NullPointerException
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.app.ActivityThread.access$1700(ActivityThread.java:151)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1418)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.os.Handler.dispatchMessage(Handler.java:110)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.os.Looper.loop(Looper.java:193)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.app.ActivityThread.main(ActivityThread.java:5330)
10-04 12:18:47.392: E/AndroidRuntime(8225): at java.lang.reflect.Method.invokeNative(Native Method)
10-04 12:18:47.392: E/AndroidRuntime(8225): at java.lang.reflect.Method.invoke(Method.java:515)
10-04 12:18:47.392: E/AndroidRuntime(8225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
10-04 12:18:47.392: E/AndroidRuntime(8225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
10-04 12:18:47.392: E/AndroidRuntime(8225): at dalvik.system.NativeStart.main(Native Method)
10-04 12:18:47.392: E/AndroidRuntime(8225): Caused by: java.lang.NullPointerException
10-04 12:18:47.392: E/AndroidRuntime(8225): at com.commlink.smscheck.SmsBroadcastReceiver.onReceive(SmsBroadcastReceiver.java:44)
10-04 12:18:47.392: E/AndroidRuntime(8225): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2609)
10-04 12:18:47.392: E/AndroidRuntime(8225): ... 10 more
实例方法如下:
public class SmsBroadcastReceiver extends BroadcastReceiver {
public static final String SMS_BUNDLE = "pdus";
public String pre_address = "03590000004";
String smsBody;
String address;
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage
.createFromPdu((byte[]) sms[i]);
smsBody = smsMessage.getMessageBody().toString();
address = smsMessage.getOriginatingAddress();
if (pre_address.equals(address)) {
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
// this will update the UI with message
SmsActivity inst = SmsActivity.instance();
inst.updateList(smsBody);
if (pre_address.equals(address))
inst.Notify(inst.getMessageCountUnread()
+ " unread message", address);
}
}
}
答案 0 :(得分:0)
NPE之所以发生,是因为您尝试从BroadcastReceiver访问actvity方法。这不是正确的方法。要在收到短信时通知您的活动,您可以从广播接收器发送广播。
public class SmsReceiver extends BroadcastReceiver {
public static final String ACTION_UNREAD_SMS = "YOUR.PACKAGENAME.ACTION_UNREAD_SMS";
public static final String EXTRA_SMS_BODY = "YOUR.PACKAGENAME.EXTRA_SMS_BODY";
public static final String EXTRA_SMS_ADDRESS = "YOUR.PACKAGENAME.EXTRA_SMS_ADDRESS";
public static final String SMS_BUNDLE = "pdus";
public static final String PRE_ADDRESS = "03590000004";
String smsBody;
String address;
public void onReceive(Context context, Intent intent) {
Bundle intentExtras = intent.getExtras();
if (intentExtras != null) {
Object[] sms = (Object[]) intentExtras.get(SMS_BUNDLE);
String smsMessageStr = "";
for (int i = 0; i < sms.length; ++i) {
SmsMessage smsMessage = SmsMessage
.createFromPdu((byte[]) sms[i]);
smsBody = smsMessage.getMessageBody().toString();
address = smsMessage.getOriginatingAddress();
if (PRE_ADDRESS.equals(address)) {
smsMessageStr += "SMS From: " + address + "\n";
smsMessageStr += smsBody + "\n";
}
}
Toast.makeText(context, smsMessageStr, Toast.LENGTH_SHORT).show();
Intent smsIntent = new Intent(ACTION_UNREAD_SMS);
smsIntent.putExtra(EXTRA_SMS_BODY, smsBody);
smsIntent.putExtra(EXTRA_SMS_ADDRESS, address);
context.sendBroadcast(smsIntent);
}
}
}
要捕捉广播,您可以在活动中这样做。
public class MainActivity extends AppCompatActivity {
private BroadcastReceiver mUnreadSmsBroadCastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String smsBody = intent.getStringExtra(SmsReceiver.EXTRA_SMS_BODY);
String smsAddress = intent.getStringExtra(SmsReceiver.EXTRA_SMS_ADDRESS);
updateList(smsBody);
if (SmsReceiver.PRE_ADDRESS.equals(smsAddress)) {
// notify your unread message
}
}
};
@Override
protected void onResume() {
super.onResume();
registerReceiver(mUnreadSmsBroadCastReceiver, new IntentFilter(SmsReceiver.ACTION_UNREAD_SMS));
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(mUnreadSmsBroadCastReceiver);
}
}
希望它对你有用。