BroadcastReceiver Tutorial Part 3 – CUSTOM BROADCASTS – Android Studio Tutorial

16
70



In part 3 of the BroadcastReceiver tutorial, we will send and receive an implicit custom broadcast with an intent action string that we define ourselves. The same as system broadcast our custom broadcast can contain extra data, which we add by calling putExtra on the intent and passing a value together with a name. We then pass our intent to the sendBroadcast method, which will send the broadcast immediately and trigger all BroadcastReceivers that were registered dynamically with the registerReceiver method.
To access the variables in our Activity, we can add the BroadcastReceiver as an inner class and update the UI in it’s onReceive method directly.

Watch the whole playlist:

Example code:

____________________
❗ Subscribe to the channel:

⏯ Watch more tutorials:

⭐ Become a channel member for exclusive tutorials and other perks:

㊙ Help translating the videos:

💚 Support the content:

💬 Join the Discord chat:

🎧 Audiobooks and podcasts for programmers:

👶 How to get started with Android development:

📣 Follow Coding in Flow on social media:

Facebook:

Instagram:

Twitter:

Blog:

Nguồn:https://quydinh.com/

16 COMMENTS

  1. @5:20 it's confusing naming the function sendBroadcast cause if you wouldn't know about overloading it would seem the function is making intents and after that it calls itself to make a new intent and after that make a new intent then call itself and make a new intent etc etc. PS great videos, many thanks

  2. I've been following your tutorials as they give clear picture of android workflow than any other online tutorials. Thank you and keep doing more. I have a doubt regarding this tutorial. As onReceive() is triggered only when is a broadcast received, we didn't send any broadcast from 1st app. I wonder how onRecieve() of 2nd app is getting triggered as it's updating UI with extra from received intent. Please help!

  3. As you told if we want to keep broadcast receiver active as long as app is running then register it in oncreate() of Application class then where should we unregister it?
    Please reply.

  4. Why do we use these pairs to register & unregister: (onCreate() & onDestroy()), (onStart() & onStop()), (onPause() & onResume()) . I mean I understand foreground/background difference, but is there anything wrong with registering in onStart() and unregistering in onDestroy(), for example? Do we HAVE TO stick to those pairs for some reason?

  5. 7:55 are you sure that when you close your app onDestroy callback method is called??
    Because I think that sometimes onDestroy isn't called when you close your app.

    Check this out: https://stackoverflow.com/questions/41744933/android-ondestroy-isnt-called-when-i-close-the-app-from-the-recent-apps-button

LEAVE A REPLY

Please enter your comment!
Please enter your name here