Setup Broadcast receiver for incoming SMS

By

November 24, 2012AndroidNo comments

This tutorial is the part of Android Development series

1.What is Android ?
2.Hello world mobile application using android.
3.What is Android activity ?
4.Understanding Android layouts
5.Introduction to AndroidManifest file
6.Android programming using Intents
7.Get phone information using button click event
8.Setup Broadcast receiver for incoming SMS

What is Broadcast Receiver?

A broadcast receiver in android is a class that is used to receive the broadcast intents that are sent by the sendbroadcast() function. The core operating system events are broadcasted by default which enables us to listen for the system events.

A system event is the generic broadcast message sent by the operating system itself. For example when you receive a SMS on your phone the operating system will broadcast the SMS_RECEIVED intent. So if your application needs to check if any sms was received then you need to listen for the SMS_RECEIVED broadcast message.

In order to listen for a system generated broadcast intent you have to register the receiver in AndroidManifest.Xml. If you register the receiver in this way then your application will start working as background process.

Process Lifecycle

  • The process which is currently running the Broadcast Receiver is considered as foreground process and it will continue to run on the system.
  • When the broadcast receiver receives the intent it will trigger onReceive() function.
  • If you don’t want to run the receiver as background process then you can register the receiver in any activity so that it will be bound to that activity alone.

In the example, We will setup a broadcast receiver that checks for the incoming message using SMS_RECEIVED intent.When a sms is received onReceive() function will raise a toast notification.

Step 1:  Create a new project “Broadcastreceiver” and extend the class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package com.example.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class Broadcastreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub

if (intent.getAction()
.equals("android.provider.Telephony.SMS_RECEIVED")) {
Toast toast = Toast.makeText(context, "message received",
Toast.LENGTH_LONG);
toast.show();
}
}

}

We have to register the receiver in Manifest

1
2
3
4
5
6
<receiver android:name=".Broadcastreceiver">
         
            <intent -filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
            </intent>
    </receiver>

Since we are accessing sms here android.permission.RECEIVE_SMS has to be added.

The broadcasted intent is filtered using the intent.getAction() function that only checks for the SMS intent not the others.

Screenshot :

broadcast receiver demo

 Download the source code

Leave a Reply

*