我正在尝试实现广播接收器,以便在我的Android设备上收到短信时实现通知弹出窗口。但是,我设置了一个条件,只检测来自预定义发件人的特定SMS,然后弹出通知。这是我到目前为止编码的内容:
public class MyBroadcastReceiver extends BroadcastReceiver
{
private static final String TAG = "MyBroadCastReceiver";
String str = "";
static Context context;
String sender;
@Override
public void onReceive(Context arg0, Intent arg1)
{
// Log.i(TAG,"OnReceive ++ ");
Bundle bndl = arg1.getExtras();
SmsMessage[] msg = null;
if (null != bndl)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bndl.get("pdus");
msg = new SmsMessage[pdus.length];
if(msg[0].getOriginatingAddress().endsWith("AIRMTA") ||
msg[0].getOriginatingAddress().endsWith("ICICIB") ||
msg[0].getOriginatingAddress().endsWith("FCHRGE") ||
msg[0].getOriginatingAddress().endsWith("MYAMEX") ||
msg[0].getOriginatingAddress().endsWith("MOBIKW") ||
msg[0].getOriginatingAddress().endsWith("OLACAB") ||
msg[0].getOriginatingAddress().endsWith("HDFCB") ||
msg[0].getOriginatingAddress().endsWith("AIRMNY")
)
{
for (int i=0; i<msg.length; i++)
{
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
// str += "SMS From " + msg[i].getOriginatingAddress();
sender = msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
context = arg0;
}
//---display incoming SMS as a Android Toast---
// Toast.makeText(arg0, str, Toast.LENGTH_SHORT).show();
//---Create a status bar notification for incoming sms-->
int mNotificationId = 001;
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context);
mBuilder.setContentTitle(sender);
mBuilder.setContentText(str);
mBuilder.setTicker("New Message Alert!");
mBuilder.setSmallIcon(R.drawable.notification);
Intent resultIntent = new Intent(arg0, MainActivity.class);
PendingIntent resultPendingIntent = PendingIntent.getActivity(arg0, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotifyMgr = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
mNotifyMgr.notify(mNotificationId, mBuilder.build());
MapsFragment obj = new MapsFragment();
obj.initilizeMap();
}
}
}
}
不幸的是,每次收到短信时,应用程序都会崩溃,无论是来自任何发件人。我无法理解代码出了什么问题! 任何人都可以帮我解决这个问题吗?
以下是日志cat错误报告:
Process: com.techfrk.fetchinboxsms, PID: 21956
java.lang.RuntimeException: Unable to start receiver com.techfrk.fetchinboxsms.MyBroadcastReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.telephony.SmsMessage.getOriginatingAddress()' on a null object reference
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2616)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1380)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
答案 0 :(得分:1)
您收到NullPointerException
异常,因为在调用msg[0].getOriginatingAddress().endsWith()
时,您的msg数组始终为空。
使用msg = new SmsMessage[pdus.length]
创建一个大小为pdus.length
的空数组,但这不会向其添加任何对象。
而不是
if(msg[0].getOriginatingAddress().endsWith("AIRMTA") ||
msg[0].getOriginatingAddress().endsWith("ICICIB") ||
msg[0].getOriginatingAddress().endsWith("FCHRGE") ||
msg[0].getOriginatingAddress().endsWith("MYAMEX") ||
msg[0].getOriginatingAddress().endsWith("MOBIKW") ||
msg[0].getOriginatingAddress().endsWith("OLACAB") ||
msg[0].getOriginatingAddress().endsWith("HDFCB") ||
msg[0].getOriginatingAddress().endsWith("AIRMNY")
)
{
for (int i=0; i<msg.length; i++)
{
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
// str += "SMS From " + msg[i].getOriginatingAddress();
sender = msg[i].getOriginatingAddress();
str += " :\r\n";
str += msg[i].getMessageBody().toString();
str += "\n";
context = arg0;
}
}
你应该做点什么
SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(arg1);
if(messages.length < 1) return;
SmsMessage sms = messages[0];
sender = sms.getOriginatingAddress();
str = sms.getMessageBody().toString();
if (sms.getOriginatingAddress().endsWith("AIRMTA")) { // add all your needed statements
// show your notification
}
请注意,为了更好地理解,此代码非常简化。