Search This Blog

Saturday, 4 February 2012

Broadcast Receiver example in Android

I know there are lots of tutorial present on BroadcastReceiver . But still not best for the who is beginning in android application development. As we know there are four basic component in android

  • Content Provider
  • Activity
  • Service
  • Broadcast Receiver

In this article i am going to explain about broad cast receiver

What exactly a BroadCastReceiver  is?

Definition: Broadcast is way of telling someone that now you have to that this is right time to do this.So do this.

Ex..There is teacher in class.If he broadcast a message while teaching and he does not specify any specific audience then all receiver(Student and will act).But if specify any action to particular student then he will act.
See in the pic.Only student having name Tofeeq will act
Using Broadcast receiver in android:There are two ways to use broad cast receiver in android --

  • One is using  dynamically in activity
  • Second to register through manifest

In first way register receiver when you feel, now you have to send broad cast message and then unregistered when task completed(As its very battery consuming process)

In second declare in Manifest file and then no need to register. In that case life cycle of receiver will be from when your application is installed and until your application uninstalled

Step by step procedure: Let consider second way to use BroadCastReceiver through Manifest file


Step 1) Take a class and extends Broadcast receiver class in this. 


public class OurCustomReceiver extends BroadcastReceiver{

 @Override
 public void onReceive(Context arg0, Intent arg1) {
  Log.i("My Rceiver ", arg1.getAction());
 }

}

Step 2) Register Receiver inside manifest file and specifying one action - Notable thing is that Receiver will be the part of application. In below code, two action has been specified for our receiver. so it will called whenever android device perform any of them


        <receiver android:name=".OurCustomReceiver">
            <intent-filter>
                <action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>

Step 3) Running application and catching action - Now just run your application it will catch both action. And onReceive() method will call for each.

Step 4) Handling multiple action by BroadCastReceiver - This application is handling two action, either device Wifi state changed or Device is restarted onReceive() method will call. So we have to check to perform desired action. Now class OurCustomReceiver's onReceive() method will like


 Log.i("My Rceiver ", arg1.getAction());
 if(arg1.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED)){
  // Do something 
 }else{
  // Do something
 }

1 comment:

Feedback always help in improvement. If you have any query suggestion feel free to comment and Keep visiting my blog to encourage me to blogging

Android News and source code