Thursday, 21 March 2013

what is broadcast receiver and broadcast receiver simple example

What is BroadCast Receiver in android ?
    Ans- Only an Intent (for which it is registered) can bring it into action. The Broadcast Receiver’s job is to pass a notification to the user, in case a specific event occurs.
    Using a Broadcast Receiver, applications can register for a particular event. Once the event occurs, the system will notify all the registered applications.

There are Two Way to Register BroadCast Receiver --
  1. Static : Use <receiver> tag and register in menifest file
  2. Dynamic : Context.registerReceiver() to add dynamically .

There are two main Classes Of BroadCast --
  1. Normal BroadCast- the registered receivers often run all at the same time. This is very efficient, but the Receivers are unable to utilize the results.
  2. Ordered BroadCast - These broadcasts are synchronous, and therefore follow a specific order. The order is defined using android: priority attribute.

Difference between Activity Intent and Broadcast Intent --
The intent used to start an Activity makes changes to an operation the user is interacting with, so the user is aware of the process. However, in case of broadcasting intent, the operation runs completely in the background, and is therefore invisible to the user.

Create A BroadCast Recevier with following steps--

  1. Creating Sub Class of BroadCast Recevier
  2. Overriding onReceiver() method of BraodCast Receiver class with following arguments-
  • Context (Indicate reference to your application)
  • Intent

Sercurity in BroadCast Receiver

As the broadcast receivers have a global work-space, security is very important concern here. If you do not define the limitations and filters for the registered receivers, other applications can abuse them.
Here are a few limitations that might help:
  • Whenever you publish a receiver in your application’s manifest, make it unavailable to external applications by using android: exported=”false”. You might think that specifying Intent filters.
  • Similarly, when you register your receiver using registerReceiver, any application may send it broadcasts. This can be prevented using permissions as well.
  • When you send a broadcast, it is possible for the external applications too to receive them. This can be prevented by specifying a few limitations.

Example of BroadCast Receiver in android

Receiving SMS using BroadCast Receiver with the following steps --

  1. Create a Sub Class of Braodcast Receiver with the follow code which will receiver the SMS

package com.example.sms_receiving;

import android.content.*;
import android.os.Bundle;
import android.telephony.*;
import android.widget.Toast;

public class SMSReceiver extends BroadcastReceiver {

public void onReceive(Context context, Intent intent) {

Bundle bundle = intent.getExtras();

Object messages[] = (Object[]) bundle.get("pdus");
SmsMessage smsMessage[] = new SmsMessage[messages.length];
for (int n = 0;n< messages.length; n++) {
smsMessage[n] = SmsMessage.createFromPdu((byte[]) messages[n]);

// show first message
Toast toast = Toast.makeText(context,
"Received SMS: " + smsMessage[0].getMessageBody(), Toast.LENGTH_LONG);;

    2 - Now Register your Receiver in android menifest file as follows-

<manifest xmlns:android=""
android:versionName="1.0" >

android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<uses-permission android:name="android.permission.RECEIVE_SMS">
android:theme="@style/AppTheme" >
/** This is the Receiver registration in android menifest **/
/** ----------------------------------------------------------**/


Now when ever Receiver will receiver the message then it will Toast the message to the user screen .

Some Important Topic Of Android With There Practical Example