我制作的应用程序可以读取传入的短信然后检索文本短信,以便在我的活动中将其写在edittext上。我的应用程序就像使用短信验证代码时的Whatsapp。当保存验证文本代码的活动仍在运行时,应用程序正常运行,但是当活动未运行(活动完成)时,应用程序在传入短信时崩溃。 请帮助我,我一直有这个问题已经很久没有解决了。非常感谢你的帮助。 我收到这样的错误消息,
java.lang.RuntimeException: Unable to start receiver com.rental.roby.okkarent.aplikasi.helper.SmsReceiver: java.lang.NullPointerException
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2590)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5421)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.rental.roby.okkarent.aplikasi.helper.SmsReceiver.onReceive(SmsReceiver.java:40)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:2571)
at android.app.ActivityThread.access$1700(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1342)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5421)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:979)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:795)
at dalvik.system.NativeStart.main(Native Method)
这是我的广播接收器代码,
public class SmsReceiver extends BroadcastReceiver {
private static SmsListener mListener;
@Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");
for(int i=0;i<pdus.length;i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String sender = smsMessage.getDisplayOriginatingAddress();
//You must check here if the sender is your provider and not another one with same text.
String messageBody = smsMessage.getMessageBody();
String verificationCode = getVerificationCode(messageBody);
//Pass on the text to our listener.
Log.d("","--------------------------------------------------------"+context);
if(verificationCode != null){
// Toast.makeText(context, verificationCode, Toast.LENGTH_LONG).show();
mListener.messageReceived(verificationCode);
}
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
private String getVerificationCode(String message) {
String code = null;
int index = message.indexOf(":");
if (index != -1) {
int start = index + 2;
int length = 4;
code = message.substring(start, start + length);
return code;
}
return code;
}
}
这是界面,
public interface SmsListener {
public void messageReceived(String messageText);
}
最后在我的活动中,我添加了这样的代码来自动编写代码验证,
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fom_verifikasi);
// asking for sms code verification,
get_verification_code(no_hp);
// catch incoming sms
SmsReceiver.bindListener(new SmsListener() {
@Override
public void messageReceived(String messageText) {
//Log.d("Text",messageText);
// write verficaation code to edittext
kode.setText(messageText);
}
});
}
这是清单,
<receiver android:name="com.rental.roby.okkarent.aplikasi.helper.SmsReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
答案 0 :(得分:0)
有时候SMS的包是空的,所以在处理之前检查一下,就像这个
一样public void onReceive(Context context, Intent intent) {
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String message = currentMessage.getDisplayMessageBody();
//
}
}
} catch (Exception e) {
}
}