0
一切工作正常,我也在我的設備上獲得otp,但我面臨的問題不會自動設置在我的editText上我是新的android,這裏是我的getSMSclass。無法在我的EditText上自動設置OTP消息
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);
}
感謝。它非常有幫助 – andy02