Site icon C1CTech

Android Broadcast Receiver Example

&NewLine;<p>This article is about Android broadcasts&comma; broadcastReceiver&comma; and how to work with the broadcastReceiver with the help of a simple example&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons is-content-justification-center is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;BroadcastReceiverExp" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<span class&equals;"embed-youtube" style&equals;"text-align&colon;center&semi; display&colon; block&semi;"><amp-youtube data-videoid&equals;"xf4fHmoK09w" data-param-rel&equals;"1" data-param-showsearch&equals;"0" data-param-showinfo&equals;"1" data-param-iv&lowbar;load&lowbar;policy&equals;"1" data-param-fs&equals;"1" data-param-hl&equals;"en-US" data-param-autohide&equals;"2" data-param-wmode&equals;"transparent" width&equals;"1200" height&equals;"675" layout&equals;"responsive"><a href&equals;"https&colon;&sol;&sol;www&period;youtube&period;com&sol;watch&quest;v&equals;xf4fHmoK09w" placeholder><amp-img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;xf4fHmoK09w&sol;hqdefault&period;jpg" alt&equals;"YouTube Poster" layout&equals;"fill" object-fit&equals;"cover"><noscript><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;xf4fHmoK09w&sol;hqdefault&period;jpg" loading&equals;"lazy" decoding&equals;"async" alt&equals;"YouTube Poster"><&sol;noscript><&sol;amp-img><&sol;a><&sol;amp-youtube><&sol;span>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Broadcasts<&sol;b><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">Broadcasts are messages that the Android system and Android apps send when an event of interest occurs&period; The broadcast message itself is wrapped in an <span style&equals;"color&colon; &num;0000ff&semi;"><strong>Intent<&sol;strong><&sol;span>&nbsp&semi;object&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">There are two types of broadcasts&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li2"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>System broadcasts<&sol;strong><&sol;span>&nbsp&semi;are delivered by the system&period;<&sol;li><li class&equals;"li2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Custom broadcasts&nbsp&semi;<&sol;span><&sol;strong>are delivered by your app&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>System broadcasts<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">A&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>system broadcast<&sol;strong><&sol;span> is a message that the Android system sends when a system event occurs&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>System broadcasts are sent to all apps that are subscribed to receive the event&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2"><strong>System broadcasts Example&colon;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li2">When the device boots&comma; the system broadcasts a system&nbsp&semi;Intent&nbsp&semi;with the action&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;Intent&period;html&num;ACTION&lowbar;BOOT&lowbar;COMPLETED"><span class&equals;"s1">ACTION&lowbar;BOOT&lowbar;COMPLETED<&sol;span><&sol;a><&sol;strong><&sol;span>&period;<&sol;li><li class&equals;"li2">When a wired headset is connected or disconnected&comma; the system sends a system Intent with the <strong><span style&equals;"color&colon; &num;008000&semi;">action<&sol;span><&sol;strong> field <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;Intent&num;ACTION&lowbar;HEADSET&lowbar;PLUG"><span class&equals;"s1">android&period;intent&period;action&period;HEADSET&lowbar;PLUG<&sol;span><&sol;a><&sol;span><&sol;strong>&nbsp&semi;and also contains more data about the event in its <span style&equals;"color&colon; &num;008000&semi;"><strong>extra<&sol;strong><&sol;span> field&comma; for example&comma; a boolean extra indicating whether a headset is connected or disconnected&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Custom broadcasts<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2"><strong>Custom broadcasts<&sol;strong> are broadcasts that your app sends out&period; To create a custom broadcast&comma; define a custom&nbsp&semi;Intent&nbsp&semi;action&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2"><strong>For example<&sol;strong>&comma; use a custom broadcast when you want to let other apps know that data has been downloaded to the device and is available for them to use&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>BroadcastReceiver<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Broadcast receivers are app components that listen for the events that have been broadcast from apps or from the Android system and respond accordingly&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">For instance&comma; if you are implementing a media app and you&&num;8217&semi;re interested in knowing when the user connects or disconnects a headset&comma; register for the&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><strong>ACTION&lowbar;HEADSET&lowbar;PLUG<&sol;strong><&sol;span>&nbsp&semi;intent action&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>Follow the two steps to make BroadcastReceiver works for your application&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Creating the Broadcast Receiver<&sol;b><&sol;span><&sol;li><li class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Registering a BroadcastReceiver &sol; Receiving Broadcasts<&sol;b><&sol;span><&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Creating the Broadcast Receiver<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ol1 wp-block-list"><li class&equals;"li1">To create a broadcast receiver&comma; define a subclass of the <span style&equals;"color&colon; &num;0000ff&semi;"><strong><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;BroadcastReceiver"><span class&equals;"s1">BroadcastReceiver<&sol;span><&sol;a><&sol;strong><&sol;span>&nbsp&semi;class and implement the&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>onReceive&lpar;&rpar;<&sol;strong><&sol;span> method&period; This subclass is where&nbsp&semi;Intent&nbsp&semi;objects are delivered if they match the intent filters you register for&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">class MyReceiver &colon; BroadcastReceiver&lpar;&rpar; &lbrace;&NewLine; override fun onReceive&lpar;context&colon; Context&quest;&comma; intent&colon; Intent&quest;&rpar; &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; logic of the code needs to be written here<&sol;span><&sol;strong>&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Registering a BroadcastReceiver<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">There are two types of broadcast receivers&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li2"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Static receivers<&sol;span><&sol;strong> or <strong><span style&equals;"color&colon; &num;0000ff&semi;">Manifest-declared receivers<&sol;span><&sol;strong>&colon; register in the Android manifest file&period;<&sol;li><li class&equals;"li2"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Dynamic receivers<&sol;strong><&sol;span> or <strong><span style&equals;"color&colon; &num;0000ff&semi;">Context-registered receivers<&sol;span><&sol;strong>&colon; register using a context&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Manifest-declared receivers<&sol;b><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">To register a <strong><span style&equals;"color&colon; &num;000000&semi;">manifest-declared receiver<&sol;span><&sol;strong>&comma; include the following attributes inside the <span style&equals;"color&colon; &num;008000&semi;"><strong>&lt&semi;receiver&gt&semi;<&sol;strong><&sol;span> element in your <span style&equals;"color&colon; &num;0000ff&semi;"><strong>AndroidManifest&period;xml <&sol;strong><&sol;span>file&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;receiver android&colon;name&equals;"&period;MyReceiver" android&colon;exported&equals;"true"&gt&semi;&NewLine; &lt&semi;intent-filter&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;BOOT&lowbar;COMPLETED"&sol;&gt&semi;&NewLine; &lt&semi;action android&colon;name&equals;"android&period;intent&period;action&period;BATTERY&lowbar;LOW" &sol;&gt&semi;&NewLine; &lt&semi;&sol;intent-filter&gt&semi;&NewLine;&lt&semi;&sol;receiver&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li1"><span style&equals;"color&colon; &num;0000ff&semi;"><strong><span class&equals;"s1"><span class&equals;"s2">android&colon; name<&sol;span><&sol;span><&sol;strong><&sol;span>&colon; name of the&nbsp&semi;BroadcastReceiver&nbsp&semi;subclass&period;<&sol;li><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s1"><span class&equals;"s2">android&colon; exported<&sol;span><&sol;span>&nbsp&semi;&lpar;optional&rpar;&colon;<&sol;span><&sol;strong> If this boolean value is set to&nbsp&semi;false&comma; other apps cannot send broadcasts to your receiver&period;<&sol;li><li class&equals;"li1"><strong><span style&equals;"color&colon; &num;0000ff&semi;"><span class&equals;"s1"><span class&equals;"s2">&lt&semi;intent-filter&gt&semi;<&sol;span><&sol;span>&colon;<&sol;span><&sol;strong><span class&equals;"Apple-converted-space">&nbsp&semi; <&sol;span>specify the broadcast&nbsp&semi;Intent&nbsp&semi;actions that your broadcast receiver component is listening for&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1"><span style&equals;"color&colon; &num;0000ff&semi;"><b>Note&colon;<&sol;b><&sol;span> For Android 8&period;0 &lpar;API level 26&rpar; and higher&comma; static receivers can&&num;8217&semi;t receive most implicit broadcasts &lpar;<i>Implicit&nbsp&semi;<&sol;i>broadcasts are broadcasts that don&&num;8217&semi;t target your app specifically&rpar;&period; Even if you register for these broadcasts in the manifest&comma; the Android system won&&num;8217&semi;t deliver them to your app&period; However&comma; you can still use a dynamic receiver to register for these broadcasts&period; To learn more&comma; see the complete list of <strong><span style&equals;"color&colon; &num;0000ff&semi;"><a style&equals;"color&colon; &num;0000ff&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;guide&sol;components&sol;broadcast-exceptions" target&equals;"&lowbar;blank" rel&equals;"noopener">implicit broadcast exceptions<&sol;a><&sol;span><&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Context-registered receivers<&sol;b><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">To register a dynamic receiver use an application context or an Activity context&period; A dynamic receiver receives broadcasts as long as the registering context is valid&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">To register a&nbsp&semi;<b>Context-registered receiver<&sol;b>&comma; follow the below steps&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">1&period; Create an instance of BroadcastReceiver&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">val br&colon; BroadcastReceiver &equals; MyReceiver&lpar;&rpar;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">2&period; Create an instance of IntentFilter and register the receiver by calling&nbsp&semi;<b>registerReceiver&lpar;BroadCastReceiver&comma; IntentFilter&rpar;&period;<&sol;b><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">IntentFilter&lpar;"com&period;c1ctech&period;broadcast&period;CUSTOM&lowbar;INTENT"&rpar;&period;also &lbrace;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; registering the receiver&NewLine; &sol;&sol; it parameter which is passed in registerReceiver&lpar;&rpar; function&NewLine; &sol;&sol; is the intent filter that we have just created<&sol;strong><&sol;span>&NewLine; registerReceiver&lpar;br&comma; it&rpar;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p1">Be sure to unregister the receiver when you no longer need it or the context is no longer valid using <a href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;Context&num;unregisterReceiver&lpar;android&period;content&period;BroadcastReceiver&rpar;"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>unregisterReceiver&lpar;BroadcastReceiver&rpar;<&sol;strong><&sol;span>&period;<&sol;a><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h4 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><b>Sending broadcasts<&sol;b><&sol;span><&sol;h4>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"p2">Android provides three ways for apps to send broadcast&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"p1 wp-block-heading"><span style&equals;"color&colon; &num;0000ff&semi;"><strong>Ordered Broadcasts<&sol;strong><&sol;span><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li1">sends broadcasts to one receiver at a time&period;<&sol;li><li class&equals;"li1">As each receiver executes in turn&comma; it can propagate a result to the next receiver&period;<&sol;li><li class&equals;"li1">Receiver can abort the broadcast and hence no broadcast is received by other receivers&period;<&sol;li><li class&equals;"li1">The order of receivers is managed and controlled by the attribute&nbsp&semi;<span style&equals;"color&colon; &num;0000ff&semi;"><b>android&colon;priority<&sol;b><&sol;span>&nbsp&semi;in corresponding intent-filter&period;<&sol;li><li class&equals;"li1">If receivers will have the same priority then they may run in any order&period;<&sol;li><li>These broadcasts are sent with <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;Context&num;sendOrderedBroadcast&lpar;android&period;content&period;Intent&comma;&percnt;20java&period;lang&period;String&rpar;"><span class&equals;"s2">sendOrderedBroadcast&lpar;Intent&comma; String&rpar;<&sol;span><&sol;a>&nbsp&semi;<&sol;strong><&sol;span>method&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"wp-block-heading" id&equals;"normal-broadcasts"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Normal broadcasts<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li2">Sends broadcasts to all receivers in an undefined order&period;<&sol;li><li class&equals;"li2">This is more efficient&period;<&sol;li><li class&equals;"li1">Receivers cannot read results from other receivers&period;<&sol;li><li class&equals;"li1">They cannot propagate data received from the broadcast&comma; or abort the broadcast&period;<&sol;li><li class&equals;"li1">These broadcasts are sent with <strong><span style&equals;"color&colon; &num;008000&semi;"><a style&equals;"font-size&colon; 16px&semi; background-color&colon; &num;ffffff&semi; font-family&colon; -apple-system&comma; BlinkMacSystemFont&comma; 'Segoe UI'&comma; Roboto&comma; Oxygen-Sans&comma; Ubuntu&comma; Cantarell&comma; 'Helvetica Neue'&comma; sans-serif&semi; color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;android&sol;content&sol;Context&num;sendBroadcast&lpar;android&period;content&period;Intent&rpar;"><span class&equals;"s2">sendBroadcast&lpar;Intent&rpar; <&sol;span><&sol;a><&sol;span><&sol;strong>method&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h5 class&equals;"p1 wp-block-heading"><strong><span style&equals;"color&colon; &num;0000ff&semi;">Local Broadcasts<&sol;span><&sol;strong><&sol;h5>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"ul1 wp-block-list"><li class&equals;"li2">sends broadcasts to receivers that are in the same app as the sender&period;<&sol;li><li class&equals;"li2">Use local broadcasts&comma; if you don&&num;8217&semi;t need to send broadcasts across apps&period;<&sol;li><li class&equals;"li2">The implementation is much more efficient &lpar;no interprocess communication needed&rpar;&period;<&sol;li><li class&equals;"li2">No security issues related to other apps being able to receive or send your broadcasts&period;<&sol;li><li>These broadcasts are sent with <span style&equals;"color&colon; &num;008000&semi;"><strong><a style&equals;"color&colon; &num;008000&semi;" href&equals;"https&colon;&sol;&sol;developer&period;android&period;com&sol;reference&sol;androidx&sol;localbroadcastmanager&sol;content&sol;LocalBroadcastManager&num;sendBroadcast&lpar;android&period;content&period;Intent&rpar;"><span class&equals;"s2">LocalBroadcastManager&period;sendBroadcast<&sol;span><&sol;a> <&sol;strong><&sol;span>method&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon; &num;000080&semi;"><strong>Creating new project<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>1 &period; Create a new project by going to <span style&equals;"color&colon; &num;008000&semi;"><b>File&nbsp&semi;<&sol;b><span class&equals;"s1"><b>&Implies;<&sol;b><&sol;span><&sol;span><b><span style&equals;"color&colon; &num;008000&semi;">&nbsp&semi;New Android Project<&sol;span>&comma;<&sol;b>&nbsp&semi;select&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>Empty Activity<&sol;strong><&sol;span>&nbsp&semi;&comma; provide&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>app name<&sol;strong><&sol;span>&comma; select language to&nbsp&semi;<strong><span style&equals;"color&colon; &num;008000&semi;">kotlin<&sol;span>&nbsp&semi;<&sol;strong>and then finally click on&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><strong>finish<&sol;strong><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>2 &period; Open <span style&equals;"color&colon; &num;008000&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong> <&sol;span>layout file to include a button to broadcast the custom intent&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>activity&lowbar;main&period;xml<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">&lt&semi;&quest;xml version&equals;"1&period;0" encoding&equals;"utf-8"&quest;&gt&semi;&NewLine;&lt&semi;androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"match&lowbar;parent"&NewLine; tools&colon;context&equals;"&period;MainActivity"&gt&semi;&NewLine;&NewLine; &lt&semi;Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;send&lowbar;broadcast"&NewLine; android&colon;layout&lowbar;width&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;text&equals;"Show Broadcast"&NewLine; android&colon;textSize&equals;"20sp"&NewLine; android&colon;textAllCaps&equals;"false"&NewLine; android&colon;padding&equals;"15dp"&NewLine; android&colon;background&equals;"&commat;color&sol;colorPrimary"&NewLine; android&colon;textColor&equals;"&commat;android&colon;color&sol;white"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent" &sol;&gt&semi;&NewLine;&NewLine;&lt&semi;&sol;androidx&period;constraintlayout&period;widget&period;ConstraintLayout&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>3 &period; Create a new Kotlin class by simply Go to<b>&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;">app &gt&semi; java &gt&semi; your package name&lpar;in which the MainActicity is present&rpar; &gt&semi; right-click &gt&semi; New &gt&semi; Kotlin File&sol;Class<&sol;span><&sol;b> and name the file as&nbsp&semi;<span style&equals;"color&colon; &num;008000&semi;"><b>MyReceiver<&sol;b><&sol;span>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><b>MyReceiver&period;kt<&sol;b><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; MyReceiver class extending BroadcastReceiver class<&sol;strong><&sol;span>&NewLine;class MyReceiver &colon; BroadcastReceiver&lpar;&rpar; &lbrace;&NewLine;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol;executed when sendBroadcast&lpar;&rpar; is called on button click<&sol;span><&sol;strong>&NewLine; override fun onReceive&lpar;context&colon; Context&quest;&comma; intent&colon; Intent&quest;&rpar; &lbrace;&NewLine;&NewLine; val data &equals; intent&excl;&excl;&period;extras&excl;&excl;&period;getString&lpar;"data"&rpar;&NewLine;&NewLine; Toast&period;makeText&lpar;context&comma; " Broadcast Received with data " &plus; data&comma; Toast&period;LENGTH&lowbar;LONG&rpar;&period;show&lpar;&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>4 &period; In <span style&equals;"color&colon; &num;008000&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span> file&comma; we are receiving broadcast dynamically and sending broadcast at the click of a button&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon; &num;0000ff&semi;"><strong>MainActivity&period;kt<&sol;strong><&sol;span><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted">package com&period;c1ctech&period;broadcastreceiverexp&NewLine;import android&period;content&period;Intent&NewLine;import android&period;content&period;IntentFilter&NewLine;import android&period;os&period;Bundle&NewLine;import androidx&period;appcompat&period;app&period;AppCompatActivity&NewLine;import kotlinx&period;android&period;synthetic&period;main&period;activity&lowbar;main&period;&ast;&NewLine;&NewLine;class MainActivity &colon; AppCompatActivity&lpar;&rpar; &lbrace;&NewLine;&NewLine; lateinit var br&colon; MyReceiver&NewLine;&NewLine; override fun onCreate&lpar;savedInstanceState&colon; Bundle&quest;&rpar; &lbrace;&NewLine; super&period;onCreate&lpar;savedInstanceState&rpar;&NewLine; setContentView&lpar;R&period;layout&period;activity&lowbar;main&rpar;&NewLine;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong> &sol;&sol;creating BroadcastReceiver instance<&sol;strong><&sol;span>&NewLine; br &equals; MyReceiver&lpar;&rpar;&NewLine;&NewLine; IntentFilter&lpar;"com&period;c1ctech&period;broadcast&period;CUSTOM&lowbar;INTENT"&rpar;&period;also &lbrace;&NewLine; <strong><span style&equals;"color&colon; &num;008000&semi;">&sol;&sol; registering the receiver&NewLine; &sol;&sol; it parameter which is passed in registerReceiver&lpar;&rpar; function&NewLine; &sol;&sol; is the intent filter that we have just created<&sol;span><&sol;strong>&NewLine; registerReceiver&lpar;br&comma; it&rpar;&NewLine; &rcub;&NewLine;&NewLine; btn&lowbar;send&lowbar;broadcast&period;setOnClickListener &lbrace;&NewLine;&NewLine; Intent&lpar;&rpar;&period;also &lbrace; intent -&gt&semi;&NewLine; intent&period;setAction&lpar;"com&period;c1ctech&period;broadcast&period;CUSTOM&lowbar;INTENT"&rpar;&NewLine; intent&period;putExtra&lpar;"data"&comma; "Hello World&excl;"&rpar;&NewLine; sendBroadcast&lpar;intent&rpar;&NewLine; &rcub;&NewLine; &rcub;&NewLine; &rcub;&NewLine;&NewLine; override fun onDestroy&lpar;&rpar; &lbrace;&NewLine; super&period;onDestroy&lpar;&rpar;&NewLine; <span style&equals;"color&colon; &num;008000&semi;"><strong>&sol;&sol; unregister the BroadcastReceiver<&sol;strong><&sol;span>&NewLine; unregisterReceiver&lpar;br&rpar;&NewLine; &rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>When you run the app it will look like this&colon;<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-image"><figure class&equals;"aligncenter is-resized"><img src&equals;"https&colon;&sol;&sol;c1ctech&period;com&sol;wp-content&sol;uploads&sol;2021&sol;06&sol;Screenshot&lowbar;1624883700&period;png" alt&equals;"" class&equals;"wp-image-2619" width&equals;"309" height&equals;"549"&sol;><&sol;figure><&sol;div>&NewLine;&NewLine;

Exit mobile version