Tutorial details

Android beginner tutorial: Part 65 - Receiving system broadcasts | App Code for Sale | Preview

Android beginner tutorial: Part 65 - Receiving system broadcasts | Android Tutorial

How to catch a broadcast sent by the system

Overview PAGE TOP

In this tutorial we're going to catch a broadcast sent by the system.

The Android system sends out broadcasts just like applications do. They are Intents that can be caught by BroadcastReceivers. Using a receiver, we can listen to actions such as phone calls, SMS text messages, battery status change, timezone change and so on.

In this tutorial we're going to catch an SMS text message and display it using a Toast.

We're only going to use one Application, which will have the BroadcastReceiver.

Go to the AndroidManifest.xml file of that application and declare a new BroadcastReceiver. Set its filter's action field to android.provider.Telephony.SMS_RECEIVED. The android.provider.Telephony class is used by the system and isn't a part of the Public API Android SDK, so we can't refer to it directly. But we can still simply set the value to a string, like shown below:

    <receiver android:name="TestReceiver" android:exported="true" android:enabled="true">
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />

After the application tags, add permissions for reading and receiving SMS:

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

Now the TestReceiver will respond to new SMS.

Open TestReceiver.java class. Inside the onReceive() function we'll use the intent.getAction() value and compare it to "android.provider.Telephony.SMS_RECEIVED" to make sure that we're responding to the correct Intent.

Extract the extras out of the Intent, create a new StringBuilder object, which will let us compose a message for the Toast out of multiple strings.

Check if the bundle isn't null. If it isn't, create a new array of objects out of "pdus" object from the extras. This is an array that contains data of the received message. The pdu itself isn't used though, first it is convered to an understandable format using createFromPdu() method of the SmsMessage class.

Use the getOriginatingAddress() and getMessageBody() methods of the SmsMessage object to get the sender and the text.

Display it all using a Toast.

package com.example.codeforfoodtest_two;

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

public class TestReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
StringBuilder sb = new StringBuilder();

Object[] pdus = (Object[])extras.get("pdus");
for(int i=0; i<pdus.length; i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
sb.append("\nFrom: " + smsMessage.getOriginatingAddress());
sb.append("\nFrom: " + smsMessage.getMessageBody());

Toast.makeText(context, "New SMS message" + sb.toString(), Toast.LENGTH_LONG).show();


That's all!

After installing the application on your device, whenever you receive SMS messages they will be displayed in a Toast.

If you test this on an emulator, you can use DDMS to send mock sms messages to your emulator. To do that, open th DDMS tab in Eclipse, select your emulator/device in the left side of the screen, then open the Emulator Control tab on the right, set the Incoming number to 5554 (or whatever your emulator's port number is - it is displayed near the emulator's name). Select SMS and write a message, then hit Send.

Thanks for reading!

Reference PAGE TOP


1 Comments Leave a comment

Please login in order to leave a comment.

Newest first
  • collection 2014-03-16 11:18:27 Thread #65

    sir/ma'am those apps need database?if they have a database what can i use for?


to your Chupamobile Account.

The Easiest way to Launch your next App or Game.

Join Chupamobile and get instant access to thousands of ready made App and Game Templates.

Creating an account means you’re okay with Chupamobile’s Terms of Service and Privacy Policy.