OTP (token) should be automatically read from the message

前端 未结 7 868
栀梦
栀梦 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:46

    **activity_main.xml**
    
    
    
    
        
    
    
    
    
    
    **MainActivity.java**
    import android.content.BroadcastReceiver;
    import android.content.IntentFilter;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    
    public class MainActivity extends AppCompatActivity {
    
        private BroadcastReceiver broadcastReceiver;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            broadcastReceiver =new MyBroadcastReceiver();
        }
    
    @Override
        protected void onStart()
    {
        super.onStart();
        IntentFilter intentFilter=new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
        registerReceiver(broadcastReceiver,intentFilter);
    }
    @Override
        protected void onStop()
    {
        super.onStop();
        unregisterReceiver(broadcastReceiver);
    }
    
    }
    
    
    **MyBroadcastReceiver.java**
    
    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    import android.widget.Toast;
    
    /**
     * Created by mukundwn on 12/02/18.
     */
    
    public class MyBroadcastReceiver extends BroadcastReceiver {
    
    
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context,"hello received an sms",Toast.LENGTH_SHORT).show();
        }
    }
    
    
    **Manifest.xml**
    
    
    
    
        
        
        
    
        
            
                
                    
    
                    
                
            
            
            
                
            
            
        
    
    
    

提交回复
热议问题