Card image cap

Table of Contents:

  1. Introduction
  2. What is a Custom Broadcast Receiver?
  3. Normal Broadcasts
  4. Ordered Broadcasts
  5. Local Broadcasts
  6. An Example:
    1. Example Introduction
    2. Creating three custom Broadcast Receivers class
    3. Registering a Broadcast Receiver
    4. Designing Layout
    5. Implementing Broadcast Receiver in Activity
    6. Sending Broadcasts
    7. Receiving Broadcasts
    8. Output



Introduction


In previous blog we talked about some default intent filters you can use with Broadcast Receivers such as Battery Low , Headphone plugged , Airplane Mode etc. Check out here , in this blog we gonna talk about our custom intent filters actions and Broadcast Receivers and how to use it in Android.


What is a Custom Broadcast Receiver?


Custom Broadcast Reciever : In addition to responding to system events , your app can respond to Custom events also.For Custom Broadcast we need to define our own actions in Intent object and broadcast it to receiver. We can send Custom Broadcast in three ways :
  • Normal Broadcasts
  • Ordered Broadcasts
  • Local Broadcasts
Note: We will be covering all these types. So , Let's get started..


Normal Broadcasts


These type of broadcasts are asynchronous in nature . Receivers attached to these broadcasts run in an undefined order , approx at a same time. To send a Normal Broadcast create a new Intent object passing your own custom action into it and call sendBroadcast(Intent) method.


Ordered Broadcasts


These type of broadcasts are delivered to one receiver at a time.The priority value in Intent filter decides which receiver to call first , When first receiver finished executing then it can propagate the result to second receiver and so on. To send an ordered Broadcast create a new Intent object passing your own custom action into it and then call sendOrderedBroadcast(Intent , String) method.


Local Broadcasts


These type of broadcasts are used when sender and receiver are in the same app . To send Local Broadcast create a new Intent object passing your own custom action into it and call LocalBroadcastManager.getInstance(context).sendBroadcast(intent).


Example Introduction


We will build the simple app which contains three buttons. On Click of first Button we will send a Normal Custom Broadcast , On click of second button we will send a Ordered Custom Broadcast and on click of third button we will send a Local custom Broadcast.

Let's begin the coding.


Creating three custom Broadcast Receivers class


Create three java classes "FirstCustomBroadcastReceiver.java and SecondCustomBroadcastReceiver.java for Normal and Ordered Broadcasts and LocalBroadcast.java for Local Broadcast which extends from Broadcast Receiver class and override "onReceive()" method.".






Registering a Broadcast Receiver


Open AndroidManifest.xml and register all the receivers class under application tag. For classes FirstCustomBroadcastReceiver.java and SecondCustomBroadcastReceiver.java declare the priority value in Intent filter , bigger the priority value first to receive a ordered Broadcast and add your custom action under intent filter tag.
  • Intent Filter
    It specifies the type of intents that a broadcast receiver can respond to in your app . Basically it contains all the implicit intents and used for filtering the intents based on some actions and category.

  • Custom Action
    A String which specifies a generic action to perform in app , The action generally determines the information contained in data or extras.In this we are specifying our own action which we will used by our Broadcast receivers.





Designing Layout


Open activity_main.xml file and add the following code.The layout contains three buttons on click of which we will send our Normal , Ordered and Local Broadcast.




Implementing Broadcast Receiver in Activity


Open MainActivity.java file and add the following code , we are creating objects of our custom Receiver classes , button and defining our custom action . In addition to this we are registering the receiver in onStart() and unregistering the receiver in onDestroy() .

Why registering the third receiver is different?

If you want to send the broadcasts locally within your app then you need to use Local Broadcasts and Android system provides LocalBroadcastManager class to register the Local Broadcast. This has a number of advantages :
  • This makes impossible for other apps to send the broadcasts to your app.This helps in maintaining security.
  • Data you are broadcasting in your app won't leave outside of your app.So you don't need to worry about your private data.


  • Why we need to register a Broadcast Receiver in onStart()?

    In order to send and receive Broadcast receivers in Android you need to first register them so that Android system can notify the receivers when specific event occur.

    Why we need to unregister a Broadcast Receiver in onDestroy()?

    If we don't unregister a receiver in onDestroy() then it still holds the reference to activity , even if we close the activity this leads to Memory leaks(Memory which is allocated but never freed) in android and Garbage collector will not be able to free that unused memory.


    I would like to explain the above code .

    In onStart() method , FirstCustomBroadcastReceiver and SecondCustomBroadcastReceiver are registered using registerReceiver() method because these are Normal and Ordered Broadcasts .

    On other hand LocalBroadcast.java is registered using LocalBroadcastManager.registerReceiver() because it's a Local Broadcast.


    Sending Broadcasts


    Open MainActivity.java file and add the following code , on click of three buttons we are sending normal , ordered and Local broadcast respectively.

    First we are passing our custom action in Intent object and then sending Local Broadcast using LocalBroadcastManager.sendBroadcast().

    Similarly we are sending Normal Broadcast using sendBroadcast() and Ordered broadcast using sendOrderedBroadcast() .






    Receiving Broadcasts


    Open FirstCustomBroadcastReceiver.java , SecondCustomBroadcastReceiver.java and LocalBroadcast.java and add the following code inside onReceive() . Inside this if action is equal to our custom action then Log message will be displayed.





    Output


    • Normal Broadcast



      The above image shows the Log message when normal broadcast is received , if you notice the receivers gets called approximately at a same time.

    • Ordered Broadcast



      The above image shows the Log message when Ordered broadcast is received , if you notice FirstReceiver class is called first because it's priority is high and SecondReceiver class is called after FirstReceiver because it's priority is low.

    • Local Broadcast



      The above image shows the Log message when local broadcast is received .



    That's All! Happy Coding!


    The complete code is available here.


    Staying in the Loop


    If you want to stay in the loop and get an email when I write new blog posts, Follow me on Instagram or join the CodingWithMitch community on my website. It only takes about 30 seconds to register.





    Authors


    By Manik Gupta

    github.com/Manik1094

    Google Certified Associate Android Developer ❤️


    Create an Account

    Have an account? Log In

    CodingWithMitch Members

    Unlimited access to all courses and videos

    Step by step guides to build real projects

    Video downloads for offline viewing

    Members can vote on what kind of content they want to see

    Access to a private chat with other communnity members & Mitch

    Become a Member

    CodingWithMitch Members

    Unlimited access to all courses and videos

    Step by step guides to build real projects

    Video downloads for offline viewing

    Members can vote on what kind of content they want to see

    Access to a private chat with other communnity members & Mitch

    Become a Member

    Comments