一切正常,我在设备上也获得了otp,但是我面临的问题没有在我的editText上自动设置,我是android的新手,这是我的getSMSclass。

public class GetIncomingSms extends BroadcastReceiver
{
SmsMessage currentMessage;
final SmsManager sms = SmsManager.getDefault();

@Override
public void onReceive(Context context, Intent intent)
{
    if(intent.getAction() == "android.provider.Telephony.SMS_RECEIVED") {


        final Bundle bundle = intent.getExtras();

        try {
            if (bundle != null) {
                final Object[] pdusObj = (Object[]) bundle.get("pdus");
                for (int i = 0; i < pdusObj.length; i++) {

                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = bundle.getString("format");
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i], format);
                    } else {
                        currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                    }

                    String phoneNumber = currentMessage.getDisplayOriginatingAddress();
                    String senderNum = phoneNumber;
                    String message = currentMessage.getDisplayMessageBody();

                    try {
                        if (senderNum.equals("'myphonenumber'") ) {
                            Otp Sms = new Otp();
                            Sms.recivedSms(message);
                                                        }
                    } catch (Exception e) {
                    }
                }
            }
        } catch (Exception e) {
        }

    }
}

}


和我的otp类,我想在我的editText上设置短信

public void recivedSms(String message)
{
        System.out.println("Register Otp = "+message);
        otp.setText(message);

}

最佳答案

检查此站点,这里是示例如何自动获取otp

http://androiddhina.blogspot.in/2015/06/reading-incoming-message-automatically-to-verify-OTP.html

并改变你的

 Otp Sms = new Otp();
 Sms.recivedSms(message);




Otp.recivedSms(message);


以及要在edittext上设置otp的位置,请确保它是静态的:

public void recivedSms(String message)
{
    System.out.println("Register Otp = "+message);
    otp.setText(message);
 }




public static void recivedSms(final String message)
{
    otp.setText(message);
}

关于android - 无法在我的EditText上自动设置OTP消息,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40639650/

10-12 03:40