2016-11-16 53 views
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); 

} 

回答

0

檢查這個網站我這裏是如何得到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); 
} 
+0

感謝。它非常有幫助 – andy02

相關問題