Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: adminHow to detect OTP automatically in android
#1
OTP term:
                     A one-time password is a password that is valid for only one login session or transaction, on a computer system or other digital device.
                Now a days in android application are intelligent to detect mobile no stored on sim or detect OTP automatically  that are some advantages or some work for android application user. So programmer trying to detect OTP automatically in there android application.

Follow following simple steps to detect OTP automatically in android application.


Step 1: Create a android project named as MyOTPDetective

Step 2: In Android Manifest file add following Permission

   
Code:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.READ_SMS" />
   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>


Step 3: Now create a SMSBroadCastReceiver.java class and extends it with BroadcastReceiver and implements onReceive method inside it as follows.


Code:
public class SMSBroadCastReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {

   }
}



Step 4:Now add code for getting sms from bundle passed by SMS Intent  in SMSBroadCastReceiver.java as follows


Code:
public class SMSBroadCastReceiver extends BroadcastReceiver {
   @Override
   public void onReceive(Context context, Intent intent) {

       // Create a bundle object to get intent passed from SMS
       Bundle bundle = intent.getExtras();
       SmsMessage[] smsm = null;
       String sms_str ="";

       if (bundle != null)
       {
           // Get the SMS message from intent
           Object[] pdus = (Object[]) bundle.get("pdus");
           smsm = new SmsMessage[pdus.length];
           for (int i=0; i<smsm.length; i++){
               smsm[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
               sms_str += smsm[i].getMessageBody().toString();


               String Sender = smsm[i].getOriginatingAddress();
               //Sender Verification here
              
                   Intent smsIntent = new Intent("otp");
                   smsIntent.putExtra("message",sms_str);
                   LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent);
               

           }
       }
   }
}

Note: if you want to sender verification then just add following code this is not neccessary but if you want it you can modify code As following.

How to Test REST API in Android



Code:
//Sender Verification here
if(Sender.equals("sendernohere"))
{
  Intent smsIntent = new Intent("otp");
  smsIntent.putExtra("message",sms_str);
  LocalBroadcastManager.getInstance(context).sendBroadcast(smsIntent);
}





Step 5:Now Register broadcast receiver in android manifest inside <application> tag as following.

Code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.ravisblogs.myotpdetective">
   <uses-permission android:name="android.permission.RECEIVE_SMS" />
   <uses-permission android:name="android.permission.READ_SMS" />
   <uses-permission android:name="android.permission.READ_PHONE_STATE"/>

   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:roundIcon="@mipmap/ic_launcher_round"
       android:supportsRtl="true"
       android:theme="@style/AppTheme">
       <activity android:name=".MainActivity">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />

               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
       <receiver android:name=".SMSBroadCastReceiver" >
           <intent-filter android:priority="999" >
               <action android:name="android.provider.Telephony.SMS_RECEIVED" />
           </intent-filter>
       </receiver>
   </application>

</manifest>

Step 6: Now in MainActivity.java  create BroadcastReceiver object and get message sent by SMS BroadcastReceiver

Code:
private BroadcastReceiver receiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equalsIgnoreCase("otp")) {
               final String message = intent.getStringExtra("message");
               Toast.makeText(context, message, Toast.LENGTH_SHORT).show();

           }
       }
   };



Step 7: Now add some run time permission to read sms in MainActivity.java as follows:

Code:
private  boolean CheckSMSPermission() {
       int permissionSendMessage = ContextCompat.checkSelfPermission(this,
               Manifest.permission.SEND_SMS);
       int receiveSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);
       int readSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
       List<String> listPermissionsNeeded = new ArrayList<>();
       if (receiveSMS != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.RECEIVE_MMS);
       }
       if (readSMS != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.READ_SMS);
       }
       if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
       }
       if (!listPermissionsNeeded.isEmpty()) {
           ActivityCompat.requestPermissions(this,
                   listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                   REQUEST_ID_MULTIPLE_PERMISSIONS);
           return false;
       }
       return true;
   }


Note:
1. on  onResume() method  --> registerReceiver(This is used when application in OnResume() method means sms detected only when Application in OnResume() state )

2.on  onPause() method  --> unregisterReceiver(This will unregister sms broadcast recever means that your application in onPause() state the sms received in smsbox cant be detected by your android application)

Step 8: Full code of MainActivity.java as follows

Code:
public class MainActivity extends AppCompatActivity {
   public static final int REQUEST_ID_MULTIPLE_PERMISSIONS = 1;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       // now check here permission got or not
       if (CheckSMSPermission()) {
           // after got permission do main code here
       }




   }
   private BroadcastReceiver receiver = new BroadcastReceiver() {
       @Override
       public void onReceive(Context context, Intent intent) {
           if (intent.getAction().equalsIgnoreCase("otp")) {
               final String message = intent.getStringExtra("message");
               Toast.makeText(context, "SMS RECEIVED:"+message, Toast.LENGTH_SHORT).show();

           }
       }
   };
   private  boolean CheckSMSPermission() {
       int permissionSendMessage = ContextCompat.checkSelfPermission(this,
               Manifest.permission.SEND_SMS);
       int receiveSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.RECEIVE_SMS);
       int readSMS = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_SMS);
       List<String> listPermissionsNeeded = new ArrayList<>();
       if (receiveSMS != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.RECEIVE_MMS);
       }
       if (readSMS != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.READ_SMS);
       }
       if (permissionSendMessage != PackageManager.PERMISSION_GRANTED) {
           listPermissionsNeeded.add(Manifest.permission.SEND_SMS);
       }
       if (!listPermissionsNeeded.isEmpty()) {
           ActivityCompat.requestPermissions(this,
                   listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]),
                   REQUEST_ID_MULTIPLE_PERMISSIONS);
           return false;
       }
       return true;
   }

   @Override
   public void onResume() {
       LocalBroadcastManager.getInstance(this).registerReceiver(receiver, new IntentFilter("otp"));
       super.onResume();
   }

   @Override
   public void onPause() {
       super.onPause();
       LocalBroadcastManager.getInstance(this).unregisterReceiver(receiver);
   }

}

OUTPUT:
[Image: 8tERSO2.png]


If Any Problem Found in above code replay me problem



How to Test REST API in Android
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)