Breaking News

27.Working with BroadcastReceiver in android

Working with BroadcastReceiver




Introduction to BroadcastReceiver?



◼ A broadcast receiver is one of the basic and important components of the Android application.


◼ A BroadcastReceiver is a dormant component of Android that respond to different messages that are broadcast by the system or other applications

◼ Such messages are broadcast as an Intent object that is waiting for broadcast receivers to respond. 

◼ The broadcast receiver responds to such broadcasted Intents and takes the necessary actions.

◼Unlike activities, a BroadcastReceiver doesn’t contain any user interface. It’s generally implemented to delegate the tasks to services depending on the type of intent data that are received.



Some important System Broadcasts

Working with BroadcastReceiver in android





Setting up A BroadcastReceiver


.
◼To set up a BroadcastReceiver in our android application we need to do the following two things.

            □Creating a BroadcastReceiver

            □Registering a BroadcastReceiver

                   。Statically in the manifest file.

                   。Dynamically in the code


Creating A BroadcastReceiver


◼A broadcast receiver is implemented as a subclass of BroadcastReceiver class and overriding the onReceive() method where each message is received as an Intent object parameter.

public class MyReceiver extends BroadcastReceiver {
@Override
    public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "Action: " + intent.getAction(), Toast.LENGTH_SHORT).show();
    }
}


◼BroadcastReceiver is an abstract class with the onReceiver() method being abstract.

◼The onReceive() method is first called on the registered BroadcastReceivers when an event occurs.

◼The Intent object is passed with all the additional data. 

◼A Context object is also available and is used to start an activity or service using context.startActivity(myIntent) or 
    context.startService(myService) respectively.



Registering A BroadcastReceiver


◼After we have created a BroadcastReceiver, it needs to be registered so that it can receive events (intents). 

◼There are two ways to do this:

           □Statically in the manifest file.

           □Dynamically in the code.




Registering in Manifest file

◼To declare a broadcast receiver in the manifest, we specify a <receiver> element in our app's manifest.

<receiver android:name=“.MyReceiver”  android:exported=“true”> 
   <intent-filter>      
        <action  android:name=“android.intent.action.BOOT_COMPLETED”/>       
        <action android:name=“android.intent.action.TIME_TICK” />  
   </intent-filter>
</receiver>




Registering Through Code


◼As an alternative, we can register our BroadcastReceiver implementation dynamically in our code.

◼This requires three steps:
Creating a BroadcastReceiver object

         □Creating an IntentFilter object

         □Registering it by calling the method registerReceiver( ) of Context


         □The registerReceiver() method takes two parameters:

Working with BroadcastReceiver in android


MyReceiver rec=new MyReceiver( );

IntentFilter filter=new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);

registerReceiver( rc,filter);



UnRegistering Through Code

◼To stop receiving broadcasts, we can call the method unregisterReceiver (android.content.BroadcastReceiver). 


◼We must surely unregister the receiver when we no longer need it or the context is no longer valid.



An Important Point!

◼We must be mindful of where we register and unregister the receiver.

◼For example, if we register a receiver in onCreate(Bundle) using the activity's context, we should unregister it in onDestroy() to prevent leaking the receiver out of the activity context.


◼If we register a receiver in onResume(), we should unregister it in onPause() to prevent registering it multiple times









Thanks . . .

No comments