Dynamic Broadcast Receiver in Android

How to create Dynamic broadcast receivers in Android

I have already posted an article here about Broadcast Receivers in Android , you can refer to this if you are not familiar with broadcast receivers. And that was all about creating broad cast receivers statically.  The main difference in working between the static and dynamic receivers are the static receivers will run if the application is running/not running. But the dynamic receivers will run if the application is running. Consider the following situation this will explain you where you want to define a dynamic broad cast receiver.

Suppose we have a normal broadcast receiver registered for BATTERY_LOW and we are doing some urgent work in the mobile phone and its battery is very low so we closed all the applications which are currently running. And then the Android os will send the battery low broad cast to all the receivers this will be received in our application and the system will start our application for handling this broadcast, here we are doing some urgent work and the starting of the application will annoy the user. in this situations we are using a dynamic receivers which will check weather our application is running currently then only it will show.

For creating a dynamic receiver we don't need to add the receiver in the manifest file. We need to create this through the java code. Here I will show how to create a broadcast receiver dynamically. I have an activity which has two buttons; one for registering the receiver and one for sending the broadcast. See the manifest file below.

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.     <Button  
  8. android:layout_width="wrap_content"  
  9. android:layout_height="wrap_content"  
  10. android:text="Register"  
  11. android:id="@+id/registerButton"  
  12. android:layout_marginTop="103dp"  
  13. android:layout_alignParentTop="true"  
  14. android:layout_centerHorizontal="true" />  
  15.     <Button  
  16. android:layout_width="wrap_content"  
  17. android:layout_height="wrap_content"  
  18. android:text="Send"  
  19. android:id="@+id/sendButton"  
  20. android:layout_below="@+id/registerButton"  
  21. android:layout_centerHorizontal="true"  
  22. android:layout_marginTop="99dp" />  
  23. </RelativeLayout>  
Initialize the intentfilter in the java file like the code below,
  1. intentFilter = new IntentFilter();  
  2. intentFilter.addAction("tutorial.negociosit.com.tutorial6.TEST_BROADCAST"); 
Create a broadcast listner in the java class.
  1. private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {  
  2. @Override  
  3. public void onReceive(Context context, Intent intent) {  
  4.         Toast.makeText(context,"This is the broadcast",Toast.LENGTH_SHORT).show();  
  5.     }  
  6. };  
In the java activity file register the broadcast receiver in the register button click listner, see the code below,
  1. registerButton.setOnClickListener(new View.OnClickListener() {  
  2. @Override  
  3. public void onClick(View view) {  
  4.         registerReceiver(broadcastReceiver,intentFilter);  
  5.     }  
  6. });  
Now see the code for sending broadcast in the send button click event,
  1. sendButton.setOnClickListener(new View.OnClickListener() {  
  2. @Override  
  3. public void onClick(View view) {  
  4.         Intent intent = new Intent();  
  5.         intent.setAction("tutorial.negociosit.com.tutorial6.TEST_BROADCAST");  
  6.         sendBroadcast(intent);  
  7.     }  
  8. });  
This is how we can create the broadcast receivers dynamically in Android. Please see the out put also.
 

Up Next
    Ebook Download
    View all
    Learn
    View all