Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to create Dynamic BroadCast Receiver in Android?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 3.77k
    Comment on it

    In this tutorial, we have defined a simple way for creating a dynamic broadcast receiver with a just single activity which has a dynamic broadcast receiver in it. This Activity has two buttons on one button click registered dynamic broadcast receiver and clicks on another button send a broadcast message which triggers our dynamic receiver.

    What are the ways to create a  broadcast receiver in android? :-

    There are two way to create broadcast receiver in android.

    1- Static BroadCast Receiver:- In this way, we create broadcast receiver in application manifest file with receiver tag.

    Example:

    <!--Registered Receiver-->
    
    <receiver android:name="MyReceiver" >
       <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>
    Custom action - define any predefined action name , event will broadcast with that name.

    2- Dynamic BroadCast Receiver:- Dynamic BroadCast Receiver is littler different from Static BroadCast Receiver. In this way, we directly registered receiver in our activity code with a custom action.

    Why do we use Dynamic BroadCast Receiver? :-

    If Any action which is defined with the static broadcast receiver in manifest file  sends a broadcast with intent and receiver intent-filter filter match with it, then the static broadcast receiver will start even application is not running currently in foreground state. But sometimes we don't want this situation.

    Example :- We wanted our receiver should be triggered if device battery goes down, and if user playing a game on the device. In this situation if we registered broadcast receiver by the static way ( in the manifest file). Then broadcast triggered with specific action whether the user currently opens application or not. But sometimes we don't need this situation, we want our receiver should be triggered only if our application is currently running( in memory).

    Solution to this situation, we use Dynamic BroadCast Receiver .

    Dynamic BroadCast Receiver Properties:

    1- It will get executed only if an application which has registered it, is currently running in memory.

    2- It will not execute if an application is not currently running.

    3- We could not create it by using manifest file. rather by using android code.

    Dynamic BroadCast Receiver Registered:-

    // 

    private BroadcastReceiver broadcastReceiver;

    // call this function on registered button onClick

    private void broadCastReceiverMethod() {
    
        //Registering Broadcast receiver to receive data from notification for Updating chat badges
        try {
            IntentFilter intentFilter = new IntentFilter();
            intentFilter.addAction("customreceiveraction");
            broadcastReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
    
                    Toast.makeText(context, "Executed Receiver......", Toast.LENGTH_SHORT).show();
                }
            };
    
            registerReceiver(broadcastReceiver, intentFilter);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
    }
    
    

     

    Send BroadCast Reciver message :-

    // call this code on sendButton on onClick

    // send message to receiver 
    
    Intent intent = new Intent();
    intent.setAction("customreceiveraction");
    this.sendBroadcast(intent);

     

    Note:- We must unregistered broadcast receiver on  onStop() call back.

    @Override 
    protected void onStop() { 
    // TODO Auto-generated method stub 
    unregisterReceiver(broadcastReceiver);
    super.onStop();
     }

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: