OTP (token) should be automatically read from the message

前端 未结 7 844
栀梦
栀梦 2020-12-04 10:50

I am working on an Android App, in which the server sends an OTP and the user needs to enter this OTP in the App, to SignUp for my App. What I want is, that my App should be

7条回答
  •  渐次进展
    2020-12-04 11:39

    I will recommend you not to use any third party libraries for auto fetch OTP from SMS Inbox. This can be done easily if you have basic understanding of Broadcast Receiver and how it works. Just Try following approach :

    Step 1) Create single interface i.e SmsListner

    package com.wnrcorp.reba;
    public interface SmsListener{
    public void messageReceived(String messageText);}
    

    Step 2) Create single Broadcast Receiver i.e SmsReceiver

    package com.wnrcorp.reba;
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.os.Bundle;
    import android.telephony.SmsMessage;
    public class SmsReceiver extends BroadcastReceiver {
    private static SmsListener mListener;
    Boolean b;
    String abcd,xyz;
    @Override
    public void onReceive(Context context, Intent intent) {
    Bundle data  = intent.getExtras();
    Object[] pdus = (Object[]) data.get("pdus");
        for(int i=0;i

    Step 3) Add Listener i.e broadcast receiver in android manifest file

        
            
                
            
    
    

    and add permission

    Final Step 4) The activity where you going to auto fetch otp when it is received in inbox. In my case I'm fetching otp and setting on edittext field.

    public class OtpVerificationActivity extends AppCompatActivity {
    EditText ed;
    TextView tv;
    String otp_generated,contactNo,id1;
    GlobalData gd = new GlobalData();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_otp_verification);
        ed=(EditText)findViewById(R.id.otp);
        tv=(TextView) findViewById(R.id.verify_otp); 
        /*This is important because this will be called every time you receive 
         any sms */            
     SmsReceiver.bindListener(new SmsListener() {
            @Override
            public void messageReceived(String messageText) {
                ed.setText(messageText);     
            }
        });
        tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try
                {
                    InputMethodManager imm=
      (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);                    
      imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),0);
                }
                catch(Exception e)
                {}           
                if (ed.getText().toString().equals(otp_generated))
                {
                    Toast.makeText(OtpVerificationActivity.this, "OTP Verified 
           Successfully !", Toast.LENGTH_SHORT).show();           
                 }
        });
       }
    }
    

    Layout File for OtpVerificationActivity

    
    
    
       
            
            
            
            
            
            
    

    Screenshots for OTP Verification Activity where you fetch OTP as soons as messages received

提交回复
热议问题