Thursday 29 March 2012

BroadcastReceiver in Android

What is Broadcast receiver?

A broadcast receiver is a class which extends BroadcastReceiver and which is registered as a receiver in an Android Application via the AndroidManifest.xml file(or via code)

Alternatively to the this static registration, you can also register a BroadcastReceiver dynamically via the Context.registerReceiver() method.
This class will be able to receive intents. Intents can be generated via the Context.sendBroadcast() method.
The class BroadcastReceiver defines the onReceive() method. Only during this method your BroadcastReceiver object will be valid, afterwards the Android system can recycle the BroadcastReceiver. Therefore you cannot perform any asynchronous operation in the onReceive() method.

sticky Broadcast Receiver

A normal broadcast intent is not available any more after is was send and processed by the system.If you use sendBroadcast(Intent) method then the intent is sticky.That means the intent is stays around after the broadcast is completed.

You can retrieve that data through the return value registerReceiver(BroadcastReceiver,IntentFilter)

// Register for the battery changed event
IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);

/ Intent is sticky so using null as receiver works fine
// return value contains the status
Intent batteryStatus = this.registerReceiver(null, filter);

// Are we charging / charged?
int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING
 || status == BatteryManager.BATTERY_STATUS_FULL;

boolean isFull = status == BatteryManager.BATTERY_STATUS_FULL;

// How are we charging?
int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
   

Example

With the help of one example we can see the use of the broadcast receiver.
Here we creating one class called "notification" and giving one toast when SMS receives.



package com.ann;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.provider.Contacts.People;
import android.util.Log;
import android.widget.Toast;

public class notification extends BroadcastReceiver {
    NotificationManager mNotificationManager;
    int Notofication_id;

    @Override
    public void onReceive(Context context, Intent intent) {
       
        Log.i("lkkjh", "sms");
        Toast.makeText(context,"sms received" ,Toast.LENGTH_LONG).show();
}
}


Then we wand to give user-permission to receive SMS,this permission usually adding to the manifest file.



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

    <uses-sdk android:minSdkVersion="8" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
       
        <receiver android:name="notification" >
           <intent-filter >
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
                       </intent-filter>
        </receiver>
        <uses-library />
    </application>

</manifest>

 

No comments:

Post a Comment