概述
我正在制作一个应用程序,它可以读取传入的短信,然后检索文本短信以将其写入我的活动的edittext中。使用短信验证代码时,我的应用就像Whatsapp。当持有验证文本代码的活动仍在运行时,应用程序正常运行,但当活动未运行(活动完成)时,应用程序在传入短信时会崩溃。 请帮助我,我一直有这个问题很长一段时间,它还没有得到解决。非常感谢你的帮助。 我得到这样的错误信息,使用广播接收器接收短信时应用程序崩溃Android Studio
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
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);
}
});
}
这是明显的,
最后
以上就是甜蜜朋友为你收集整理的Android短信接收器,使用广播接收器接收短信时应用程序崩溃Android Studio的全部内容,希望文章能够帮你解决Android短信接收器,使用广播接收器接收短信时应用程序崩溃Android Studio所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复