Thursday, 5 November 2015

Read incoming message automatically in android for verify OTP

Hi guys i am going to explain how to read the incoming message in android automatically to verify OTP without closing the app using the BrodcastReceiver

Step 1).IncomingSms.class

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;

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

final Bundle bundle = intent.getExtras();
  try {
  if (bundle != null) 
{
   final Object[] pdusObj = (Object[]) bundle.get("pdus");
   for (int i = 0; i < pdusObj .length; i++) 
 {
    SmsMessage currentMessage = SmsMessage.createFromPdu((byte[])                                                                                                    pdusObj[i]);
String phoneNumber currentMessage.getDisplayOriginatingAddress();
String senderNum phoneNumber ;
String message currentMessage .getDisplayMessageBody();
    try
  {
   if (senderNum .equals("TA-DOCOMO")) 
   {
Otp Sms = new Otp();
Sms.recivedSms(message );
   }
}
catch(Exception e){}

}
  }

} catch (Exception e) 
{
                
 }
}

}

Step 2).Otp.class

   public class Otp extends Activity 
   {

      static EditText OtpNumber;
       protected void onCreate(Bundle savedInstanceState) 
         {
super.onCreate(savedInstanceState);
setContentView(R.layout.otp);
                OtpNumber= (EditText) findViewById(R.id.txtName);
        }
      public void recivedSms(String message
       {
try 
         {
OtpNumber.setText(message);
       } 
         catch (Exception e) 
             {         
                   }
      }

       }   

Step 3).Add the below permissions and receiver to your manifest file


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

    <receiver android:name=".IncomingSms">
        <intent-filter>
         <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>

13 comments:

  1. Replies
    1. i have tested it's working fine also this code working one app.

      Delete
    2. i have tested it's working fine also this code working one app.

      Delete
    3. Hi Ashok...do we need to add any other classes for it ?
      Can I send me the full pledge code?

      Delete
  2. very nice , it is help full for me , but i need to read otp from email also, please help me on that

    ReplyDelete
  3. Do we need to add any other classes for this code???
    Please reply and thank you in advance.........

    ReplyDelete
  4. Hello Ashok I have tried it and it work, I am presently using www.textlocal.in to send my OTP's will suggest this code to my customers thank you.

    ReplyDelete
  5. code is not working...what is TA-DOCOMO???

    ReplyDelete
  6. its not working send me the full code

    my mail id is: mahesh0419@gmail.com

    ReplyDelete