Automatically SMS OTP reader using Broadcast Receiver in android studio
Automatically SMS OTP reader using Broadcast Receiver in android studio

Hi Guys, Welcome to Proto Coders Point, In this Android Tutorial we will build an android application that will be able in reading incoming message automatically in android to verify otp.

FINAL OUTPUT OF READING OTP AUTOMATICALLY

Automatically SMS OTP reader using Broadcast Receiver in android studio

Video Tutorial on How to auto read otp in android using Breadcast Receiver.

At the end of this android tutorial your app will be able the read sms and fetch all the text in that sms and then it will copy the OTP from SMS and paste it in EditText box.

So let’s begin implementation of otp sms reader

Create a new Android Project

Ofcourse you need to create a new android project or open any existing android project

To do so go to File > New > New Project

Give a name as per you need in my case i have names my application as “Auto sms otp reader”

Create a custom background for EditText and a Vector Image

1. edittextbackground.xml

create a new xml file in drawable folder

res > drawable(right click) > New > Drawable resource file

copy paste below xml code

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <corners android:radius="5dp"/>
    <solid android:color="#dcdcdc"/>

</shape>

2. Create a Vector Image

res > drawable (right click) > New > Vector Asset

a dialog box will appear on your screen as shown below

How to create Vector image asset on android studio

Then, Here you can select any kind of image clip Art.

Create a new Java Class names OTPReceiver

OTPReceiver.java

package com.example.autosmsotpreader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Telephony;
import android.telephony.SmsMessage;
import android.widget.EditText;

public class OTP_Receiver extends BroadcastReceiver {
    private  static EditText editText;

    public void setEditText(EditText editText)
    {
        OTP_Receiver.editText=editText;
    }

// OnReceive will keep trace when sms is been received in mobile
    @Override
    public void onReceive(Context context, Intent intent) {
        //message will be holding complete sms that is received
        SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent); 

        for(SmsMessage sms : messages)
        {
  
            String msg = sms.getMessageBody();
            // here we are spliting the sms using " : " symbol
            String otp = msg.split(": ")[1]; 

            editText.setText(otp);
        }

    }
}

eg: sms received is Your OTP is : 4587, Then in above code i have split the complete message string in to 2 parts using “:” this special symbol.

Open AndroidManifest.xml file & add the uses permission

adding uses permission

add this 4 user-permission inside <manifest>  here </manifest> tags

<!-- this are the permission need so that application can read sms -->
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.READ_PHONE_NUMBERS"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <uses-permission android:name="android.permission.WRITE_SMS"/>

Now, Add the following receiver OTPReceiver.java in manifest under <application> tag

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

as shown in the below screenshot

adding permission in android manifest xml

MainActivity.java

Copy paste the below lines of code in MainActivity.java

package com.example.autosmsotpreader;


import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.widget.EditText;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;


public class MainActivity extends AppCompatActivity {

    EditText otpnumber;
    
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //we need to ask user permission to auto read sms
        requestsmspermission();
        otpnumber = (EditText)findViewById(R.id.edittext);

        new OTP_Receiver().setEditText(otpnumber);
    }

  
    private void requestsmspermission() {
        String smspermission = Manifest.permission.RECEIVE_SMS;
        int grant = ContextCompat.checkSelfPermission(this,smspermission);

        //check if read SMS permission is granted or not
        if(grant!= PackageManager.PERMISSION_GRANTED)
        {
            String[] permission_list = new String[1];
            permission_list[0]=smspermission;

            ActivityCompat.requestPermissions(this,permission_list,1);
        }
    }


}

Recommended Android Articles

otp verification in android

textlocal api to send sms/otp verification

android check internet connection continuously

Comments are closed.