一切都正常,我也在我的设备上收到了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);
}