Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to read otp from message inbox if new message contains otp?

    • 0
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 145
    Comment on it
    //We know that now a days apps like watsapp automatically read otp from messages //so this post will show you how to do that programatically.
    
    //First of all we need Broadcast Reciever that will announce broadcast that a new //message has arrived.
    
            //In this broadcast we basically check for message number that we are //recieving and check that is correct or not.
    
    public class SmsReciever extends BroadcastReceiver {
    
        public void onReceive(Context context, Intent intent)
        {
            Bundle myBundle = intent.getExtras();
            SmsMessage [] messages;
            String mMessageFrom = null;
            String mMessageBody = null;
            String mOtp = null;
    
            if (myBundle != null)
            {
                Object [] pdus = (Object[]) myBundle.get("pdus");
    
                messages = new SmsMessage[pdus.length];
    
                for (int i = 0; i < messages.length; i++)
                {
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                        String format = myBundle.getString("format");
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
                    }
                    else {
                        messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                    }
    
                    String phoneNumber = messages[i].getDisplayOriginatingAddress();
                    mMessageFrom = phoneNumber ;
                    mMessageBody = messages[i] .getDisplayMessageBody();
    
                }
    
                if (mMessageFrom != null) {
                    if (mMessageFrom.equals("DM-ADHAAR")){
                        Matcher m = Pattern.compile(
                                Pattern.quote("is ")
                                        + "(.*?)"
                                        + Pattern.quote(" and")
                        ).matcher(mMessageBody);
                        while(m.find()){
                            mOtp = m.group(1);
                        }
                    }
                }
                SocialMediaActivity socialMediaActivity = SocialMediaActivity.instance();
                socialMediaActivity.updateOTP(mOtp);
            }
        }
    
    }
    
    //And in Social Media activity we checks for activity instance and call the method //to update edittext like this :
    
    private static SocialMediaActivity mSocialMediaActivity;
    
    public static SocialMediaActivity instance() {
        return mSocialMediaActivity;
    }
    
    @Override
    public void onStart() {
        super.onStart();
        mSocialMediaActivity = this;
    }
    
    public void updateOTP(String otp){
    
        etAadhaarOtp.setText(otp);
    
    }
    
    //so we call this method from our broadcast reciever.

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: