Read Incoming Message in Android

Todays, Almost all application use text SMS for authentication purpose like OTP verification, Login via mobile etc. In this feature server send a text in any format and need to validate by the user that’s it. Using this modern feature user can save more time.

In this article, we are going to learn how to implement this feature in android app development. Create new project in android studio (You can choose any IDE).

Project Architecture :-

Let’s do code implmentation here

  1. Add a (You can choose class name) for entry point when new sms received.
package com.example.readmessage;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class MessageReceiver extends BroadcastReceiver {

private static MessageListener mListener;

public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");
for(int i=0; i<pdus.length; i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
String message = "Sender : " + smsMessage.getDisplayOriginatingAddress()
+ "Email From: " + smsMessage.getEmailFrom()
+ "Emal Body: " + smsMessage.getEmailBody()
+ "Display message body: " + smsMessage.getDisplayMessageBody()
+ "Time in millisecond: " + smsMessage.getTimestampMillis()
+ "Message: " + smsMessage.getMessageBody();

public static void bindListener(MessageListener listener){
mListener = listener;

Let’s discuss what’s the code behind. First of all create class and extends BroadcastReceiver class and override onReceive() method which has two argument first one is context, and second one is intent. The intent contains the PDU (Protocol Data Unit) object which is a protocol for transferring message. From this protocol get all object and SmsMessage object using SmsMessage.createFromPdu() method which take one argument of raw PDU. After creating message object we can get all information from this object and send back to UI(Any where you want) for sending this information I am using listener called ( interface.

2. Now the time to create listener ( interface. Code is here just use this.

package com.example.readmessage;

public interface MessageListener {
* To call this method when new message received and send back
@param message Message
void messageReceived(String message);

3. Now implement this listener where you want to receive your sms body and register this listener. As we getting sms data back to my . So my code is here

package com.example.readmessage;

import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements MessageListener {

protected void onCreate(Bundle savedInstanceState) {
//Register sms listener

public void messageReceived(String message) {
Toast.makeText(this, "New Message Received: " + message, Toast.LENGTH_SHORT).show();

In this file only register listener and override messageReceived() method which has only one argument called message. Here you can authenticate your OTP or do any thing with this message as per your requirements.

4. The last and very important things is open your AndroidMenifest.xml file and get permission and also entry Receiver. Here are complete code for manifest file is.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

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

<activity android:name=".MainActivity">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
<receiver android:name=".MessageReceiver">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />


5. Now Run you code and enjoy. Happy coding.