Local Broadcast, less overhead and secure in Android




Broadcast receiver is an Android component which allows you to send or receive Android system or application events. All the registered application are notified by the Android runtime once event happens.
It works similar to the publish-subscribe design pattern and used for asynchronous inter-process communication.

For example, applications can register for various system events like boot complete or battery low, and Android system sends broadcast when specific event occur. Any application can also create its own custom broadcasts.


Basics of broadcast
Let’s discuss some basic concepts of broadcast receiver.

Register Broadcast
There are two ways to register broadcast receiver-

Manifest-declared (Statically): By this receiver can be registered via the AndroidManifest.xml file.

Context-registered (Dynamically): By this register a receiver dynamically via the Context.registerReceiver() method.


Receive Broadcasts

To be able to receive a broadcast, application have to extends the BroadcastReceiver abstract class and override its onReceive() method.
If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.


Problem with global broadcast

It is good practice to use broadcast receivers when you want to send or receive data between different applications. But if the communication is limited to your application then it is not good to use the global broadcast.
In this case Android provides local broadcasts with the LocalBroadcastManager class.
There are many reasons for NOT using broadcast:
·       Using global broadcast, any other application can also send and receives broadcast messages to and from our application. This can be a serious security thread for our application.
·       Global broadcast is sent system-wide, so it is not performance efficient.


What is LocalBroadcastManager?

For obvious reasons, global broadcasts must never contain sensitive information. You can, however, broadcast such information locally using the LocalBroadcastManager class, which is a part of the Android Support Library.
The LocalBroadcastManager is much more efficient as it doesn’t need inter-process communication.

Below are some of its benefits:
·       Broadcast data won't leave your app, so don't need to worry about leaking private data.
·       It is not possible for other applications to send these broadcasts to your app, so you don't need to worry about having security holes they can exploit.
·       It is more efficient than sending a global broadcast through the system.
·       No overhead of system-wide broadcast.


Implementation

There is no additional support library dependency required in the latest version of Android Studio. However, if you want to implement local broadcasts in an old project, following dependency needs to be add in the app module's build.gradle file:

compile 'com.android.support:support-v4:23.4.0'

Create a new instance of the LocalBroadcastManager

LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(context);


You can now send local broadcasts using the sendBroadcast() method like

// Create intent with action
Intent localIntent = new Intent("CUSTOM_ACTION");
 // Send local broadcast
localBroadcastManager.sendBroadcast(localIntent);

Now create a broadcast receiver that can respond to the local-broadcast action:

private BroadcastReceiver listener = new BroadcastReceiver() {
  @Override
  public void onReceive( Context context, Intent intent ) {
    String data = intent.getStringExtra("DATA");
    Log.d( "Received data : ", data);
  }
};

Dynamically registered receivers must be unregistered when they are no longer necessary like:

localBroadcastManager.unregisterReceiver(myBroadcastReceiver);

You can find the reference code to implement Local Broadcast receiver from GitHub. In the sample code I have created an IntentService, which broadcasts current date and it is received by an Activity of the same application.



How to secure broadcasts

Restrict your app to receive broadcast

·       Specify a permission parameter when registering a broadcast receiver then only broadcasters who have requested the permission can send an Intent to the receiver.
For example, receiving app has a declared SEND_SMS permission in the receiver as shown below:

<receiver android:name=".MyBroadcastReceiver"
          android:permission="android.permission.SEND_SMS">
    <intent-filter>
        <action android:name="android.intent.action.AIRPLANE_MODE"/>
    </intent-filter>
</receiver>

·       Set the android:exported attribute to "false" in the manifest. This restrict to receive broadcasts from sources outside of the app.
·       Limit yourself to only local broadcasts with LocalBroadcastManager.


Control receiver of your broadcast

·       You can specify a permission when sending a broadcast then only receivers who have requested that permission can receive the broadcast. For example, the following code sends a broadcast:

sendBroadcast(new Intent("com.example.NOTIFY"), Manifest.permission.SEND_SMS);

·       In Android 4.0 and higher, you can specify a package with setPackage(String) when sending a broadcast. The system restricts the broadcast to the set of apps that match the package.
·       Send local broadcasts with LocalBroadcastManager.


Conclusion

Hope you understand about the global and local broadcasts and their security consideration. To improve the performance of system Android O has changed the registration of broadcast receiver. From Android O you cannot register implicit broadcasts in your application manifest (few exceptions is there) but application can continue to register for explicit broadcasts in their manifests or at run-time.

Read more on broadcast receiver at Android official document.

To find more interesting topics on Software development follow me at Medium  Twitter  GitHub


Comments

  1. We are one of the best cloud computing companies in Indore & India.Contact Tekzee Technologies for any Cloud Computing Services.Cloud Computing Company Indore

    ReplyDelete
  2. We are one of the best cloud computing companies in Indore & India.Contact Tekzee Technologies for any Cloud Computing Services.Cloud Computing Company Indore

    ReplyDelete
  3. We are one of the best cloud computing companies in Indore & India.Contact Tekzee Technologies for any Cloud Computing Services.Cloud Computing Company Indore

    ReplyDelete
  4. Thanks for the highlights of Andriod Oreo....
    can u list out some of the main drawbacks of this version
    ios App Development Company
    Android App Development Company

    ReplyDelete
  5. Sniper Shooter 3d - Real Mission. In this gun shooting games you play the role of a superb shot technology elite killer. You will be faced with fully armed murderous terrorists, they are heinous, with little attention you will be killed by them. In order to complete the task and not be killed by them, you have to go all out.
    Sniper Shooter 3D
    HD Sniper shooter 3D
    Sniper Shooter 3D Game

    ReplyDelete
  6. Thanks for sharing above information.
    We (Netguru Solution India Pvt. Ltd.) is one of the leading and developing Website Design and Mobile Application Development Company in Pune with a specialist management and expert group.
    Mobile Application Development Company In Pune
    Website Design Company In Pune

    ReplyDelete
  7. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand Local Broadcast, less overhead and secure in Android .Thanks for your informative article
    Android Training in chennai | Android Training institute | Android Training in Velachery

    ReplyDelete
  8. Fantastic post for readers, you have explained all the concept in this post really helpful guidance for readers.

    Now app development buffalo ny service is available from Crystal Tech Solution and develop your business App in a reasonable cost.

    ReplyDelete
  9. Really your content is so informative. so please share some more content ..
    Android training in Delhi

    ReplyDelete
  10. This is really too useful and have more ideas and keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    Latest updates
    Education

    ReplyDelete
  11. informative article and very useful ideas. explained concepts are very useful. best java training in chennai

    ReplyDelete
  12. impressive article. Its a valuable information.
    Android dose provide you with all the information, updates and newly launched phones by android , their specification, price and reviews.

    Android dose

    ReplyDelete
  13. Excellent post!!!. The strategy you have posted on this technology helped me to get into the next level and had lot of information in it.

    Data Science training in Chennai
    Data science training in bangalore
    Data science training in pune
    Data science online training

    ReplyDelete
  14. Thank you for taking the time and sharing this information with us. It was indeed very helpful and insightful while being straight forward and to the point.
    Java training in Chennai | Java training in Tambaram | Java training in Chennai | Java training in Velachery

    Java training in Chennai | Java training in Omr | Oracle training in Chennai

    ReplyDelete
  15. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python interview questions and answers
    python tutorials
    python course institute in electronic city

    ReplyDelete

  16. § Then your normal target is 20000 points for two celebrities, to reach two stars is very simple.

    § Finally your goal will be 300000 points for three celebrities. To attain three stars is quite simple.

    § The components worth is only 7900 points.

    § The level indicates very hard.

    Reason of collapse

    You have to collect at least four yellowish candies each motions. If You couldn't collect this you'll fail in the level. The blessed candy might not help you in the plank. So try outside to collect the yellow candy sequence. Attempt to create color bomb you can't complete your order. Remove the marmalade first. Bring down all the ingredients and remove them otherwise you won't triumph. With routine six colours candy is very simple to complete yellow candies order. That means you should to focus the regular candy.

    Thus, level 951 candy crush is forms of quite hard.

    FAQ

    Qu:- how many yellow candy is collect in the level?

    Ans:- there are 79 yellow candies have to collect in the level.

    Qu:- how many spaces Are in the level?

    Ans:- you will find 49 spaces at the level.

    Qu:- how many particular candy in the level?

    Ans:- there are 3 particular candies at the level.

    Meta description

    You must gather at least four yellow candies each motions. If You couldn't collect this you will fail in candies crush 951 levels.
    Candy Crush 762

    ReplyDelete
  17. fore more info...
    https://www.alltips24a2z.com/

    ReplyDelete
  18. This comment has been removed by the author.

    ReplyDelete
  19. This comment has been removed by the author.

    ReplyDelete
  20. Thank you for your post, I look for such article along time, today i find it finally. this post give me lots of advise it is very useful for me.
    data analytics certification courses in Bangalore
    ExcelR Data science courses in Bangalore

    ReplyDelete
  21. Amazing knowledge and I like to share this kind of information with my friends and hope they like it they why I do
    Data Science Course in Pune

    ReplyDelete
  22. I just got to this amazing site not long ago. I was actually captured with the piece of resources you have got here. Big thumbs up for making such wonderful blog page!




    BIG DATA COURSE

    ReplyDelete
  23. This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for posting this again.
    big data course malaysia

    ReplyDelete
  24. I was just browsing through the internet looking for some information and came across your blog. I am impressed by the information that you have on this blog. It shows how well you understand this subject. Bookmarked this page, will come back for more.
    data science course in singapore

    ReplyDelete
  25. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    Love it, Thanks For Sharing.

    ReplyDelete
  26. This comment has been removed by the author.

    ReplyDelete
  27. Thanks for writing this great article. I’ve been using some of these techniques on by blog. But I didn’t know the phrase “Social Proof”. Thanks for sharing.
    science courses
    Machine.co

    ReplyDelete
  28. Thanks for writing this great article. I’ve been using some of these techniques on by blog. But I didn’t know the phrase “Social Proof”. Thanks for sharing.
    science courses
    Machine.co

    ReplyDelete
  29. I like viewing web sites which comprehend the price of delivering the excellent useful resource free of charge. I truly adored reading your posting. Thank you!
    data science course

    ReplyDelete
  30. Me2call4u is random Video chat, and connect with anyone from the anywhere in the world with a single swipe.

    ReplyDelete
  31. I love the way you write Business Analytics Online Course and share your niche! Very interesting and different! Keep it coming!

    ReplyDelete
  32. Awesome blog. I enjoyed reading your articles. This is truly a great read for me. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work!
    ExcelR Data Analytics Course
    Data Science Interview Questions

    ReplyDelete
  33. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Best Institute for Data Science in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Best Institute for Data Science

    ReplyDelete
  34. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Data Science institute in Mumbai which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Data Science institute in Mumbai

    ReplyDelete
  35. Data Science is the future of Artificial Intelligence. Therefore, it is very important to understand what is Data Science and how can it add value to your business.
    Best institute for Data Science in Bangalore which includes classroom and online training. Along with Classroom training, we also conduct online training using state-of-the-art technologies to ensure the wonderful experience of online interactive learning. Best institute for Data Science in Bangalore

    ReplyDelete
  36. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog. ExcelR Data Science Class In Pune

    ReplyDelete
  37. It is extremely nice to see the greatest details presented in an easy and understanding manner..!. data science course Bangalore

    ReplyDelete
  38. I am really enjoying reading your well written articles. It looks like you spend a lot of effort and time on your blog. I have bookmarked it and I am looking forward to reading new articles. Keep up the good work.
    data analytics course mumbai
    data science interview questions

    ReplyDelete
  39. Positive site, where did u come up with the information on this posting?I have read a few of the articles on your website now, and I really like your style. Thanks a million and please keep up the effective work.mobile phone repair in Novi
    iphone repair in Novi
    cell phone repair in Novi
    phone repair in Novi
    tablet repair in Novi
    ipad repair in Novi
    mobile phone repair Novi
    iphone repair Novi
    cell phone repair Novi
    phone repair Novi


    ReplyDelete
  40. I feel very grateful that I read this. It is very helpful and very informative and I really learned a lot from it.
    app and you are doing well.





    Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery

    ReplyDelete
  41. Excellent Blog! I would like to thank for the efforts you have made in writing this post. I am hoping the same best work from you in the future as well. I wanted to thank you for this websites! Thanks for sharing. Great websites!

    AI Training in hyderabad

    ReplyDelete
  42. It is amazing and wonderful to visit your site. Thanks for sharing information; this is useful to us....

    data analytics course in Delhi

    ReplyDelete
  43. First You got a great blog .I will be interested in more similar topics. i see you got really very useful topics, i will be always checking your blog thanks.
    data scientist training and placement

    ReplyDelete
  44. Health Structure is a relatively new procedure that offers the promise of long-term pain relief. It has thebuy aged bulk gmail twitter instagram accounts pva youtube buy instagram accounts instagram pva accounts buy bulk instagram accounts pvaaccountss com potential to offer sufferers a chance to end their chronic pain, but first, they need to meet the needs of their doctor and undergo the surgical process. With so many people in pain today, it is important to have access to the latest procedures that can provide positive results and long-lasting comfort. If you or a friend are interested in having this type of procedure, there are some things you should be aware of.
    Buy pinterest accounts

    ReplyDelete
  45. I am genuinely thankful to the holder of this web page who has shared this wonderful paragraph at at this place data science course in surat

    ReplyDelete
  46. Great tips and very easy to understand. This will definitely be very useful for me when I get a chance to start my blog.
    data science institutes in hyderabad

    ReplyDelete
  47. Truly exceptionally interesting post. I was searching for such a data and enjoyed investigating this one. Continue to post. Appreciative for sharing..data scientist course in ghaziabad

    ReplyDelete
  48. https://devel0p.net/php-creating-a-javascript-api-interface

    ReplyDelete

  49. Digital Marketing Course in Delhi is now an important method to market your business. It allows companies to interact with customers on multiple levels and make meaningful connections that create lasting relationships. The job of a digital marketing professional involves planning, creating, and implementing online marketing strategies. It can encompass web design and management of social media, the optimization of search engines and content creation.

    ReplyDelete
  50. Thanks for share this. Andaman tourism package design customized or pre-packaged itineraries for tourists. This includes selecting accommodations, transportation, and activities based on the client's preferences.

    ReplyDelete

Post a Comment

Popular posts from this blog

Android Performance: Avoid using ENUM on Android

Android O: Impact On Running Apps And Developer Viewpoint

Smart way to update RecyclerView using DiffUtil