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 MessageReceiver.java (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;

@Override
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();
mListener.messageReceived(message);
}
}

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

Let’s discuss what’s the code behind. First of all create MessageReceiver.java 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 (MessageListener.java) interface.

2. Now the time to create listener (MessageListener.java) 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 MainActivity.java . So my code is here

package com.example.readmessage;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements MessageListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Register sms listener
MessageReceiver.bindListener(this);
}

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

In this MainActivity.java 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="http://schemas.android.com/apk/res/android"
package="com.example.readmessage"
>

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

<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=".MessageReceiver">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>

</manifest>

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