How to get the SMS sent to your emulator within your application? OR Get notified when an SMS arrives to your phone.

By | June 21, 2012

Actually these are done using services in ANDROID. These are called BroadcastReceivers.Your class has to extend the BroadcastReceiver class to get these broadcast events. But note that these services need to have an interface. So you will not see any UI on the device. But don’t think your application is not running or not installed. Look at the console for results. You will probably see this type of console on running this application.

SMS receiver in android

SMS Receiver in android

This means that your service has been installed in the emulator. Check the apps installed. You will probably see your application.

Now we are going to create this SMS receiver application.
Create a new project and copy this code to the java file created.

package pack.coderzheaven;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

@SuppressWarnings("deprecation")
public class SMS_Receiver extends BroadcastReceiver {
	@Override
	public void onReceive(Context context, Intent intent) {
		Bundle bundle = intent.getExtras();

		Object messages[] = (Object[]) bundle.get("pdus");
		SmsMessage SMS[] = new SmsMessage[messages.length];
		for (int n = 0; n < messages.length; n++) {
			SMS[n] = SmsMessage.createFromPdu((byte[]) messages[n]);
		}
		/* GETTING THE LAST JUST ARRIVED MESSAGE FROM INBOX */
		Toast toast = Toast.makeText(context,"Received SMS inside SMS_Receiver : " + SMS[0].getMessageBody(), Toast.LENGTH_LONG);
		toast.show();
	}
}

In the above class you can see that “SMS_Receiver” extends the BroadcastReceiver class.So this class has to implement the onReceive method inorder to receive broadcast events. The intent is get using

Bundle bundle = intent.getExtras();

After that an array is created for all the SMS and here I am showing only the last SMS received by the intent using a toast. How to simulate an SMS to your emulator is explained in here and another method here
No XML file for layout is used here.

AndroidManifest.xml file.

Note that every receiver should be registered in the manifest file in order to receive intents using the receiver tag. Take a look at this code.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="pack.coderzheaven"
      android:versionCode="1"
      android:versionName="1.0">

      <uses-permission android:name="android.permission.RECEIVE_SMS" />
      <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />

 <application android:icon="@drawable/icon" android:label="@string/app_name">
       <receiver android:name=".SMS_Receiver" android:enabled="true">
<intent-filter>
	<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
	</receiver>
</application>
</manifest>

After sending the SMS mentioned in one of the methods after running this application, quickly switch to the emulator to see the output, because I am using a toast to display the message, So it will come and go and you may not see.
Sending SMS using emulator control.

SMS receiver example

SMS Receiver example

SMS Receiver service example

SMS Receiver service example

Please leave your comments if you found this post useful.

3 thoughts on “How to get the SMS sent to your emulator within your application? OR Get notified when an SMS arrives to your phone.

  1. Pingback: Listening incoming sms message in Android | Coderz Heaven

  2. Ishwari Shah

    Hello..Thanks for your detail guidance …But i also want to view the sender’s number in the Toast along with the Sms Body.So please can you help me how to view it please…
    I appreciate you guidance as early as possible..

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *