Created
October 29, 2023 06:45
-
-
Save MaksimDmitriev/47d7463952c06aa8090272d683f4dc9e to your computer and use it in GitHub Desktop.
Broadcast receivers in Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
| android:layout_width="match_parent" | |
| android:layout_height="match_parent" | |
| android:orientation="vertical"> | |
| <Button | |
| android:id="@+id/button_explicit_broadcast" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="Explicit broadcast" /> | |
| <Button | |
| android:id="@+id/button_implicit_broadcast" | |
| android:layout_width="wrap_content" | |
| android:layout_height="wrap_content" | |
| android:text="implicit broadcast" /> | |
| </LinearLayout> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?xml version="1.0" encoding="utf-8"?> | |
| <manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
| xmlns:tools="http://schemas.android.com/tools" | |
| package="ru.maksim.sample"> | |
| <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> | |
| <application | |
| android:name=".MyApp" | |
| android:allowBackup="true" | |
| android:dataExtractionRules="@xml/data_extraction_rules" | |
| android:fullBackupContent="@xml/backup_rules" | |
| android:icon="@mipmap/ic_launcher" | |
| android:label="@string/app_name" | |
| android:roundIcon="@mipmap/ic_launcher_round" | |
| android:supportsRtl="true" | |
| android:theme="@style/Theme.SampleApplication" | |
| tools:targetApi="31"> | |
| <receiver | |
| android:name=".MyBootCompletedReceiver" | |
| android:exported="true"> | |
| <intent-filter> | |
| <action android:name="android.intent.action.BOOT_COMPLETED" /> | |
| </intent-filter> | |
| </receiver> | |
| <receiver | |
| android:name=".MyReceiver" | |
| android:exported="false"> | |
| <intent-filter> | |
| <action android:name="ru.maksim.sample.MY_ACTION" /> | |
| </intent-filter> | |
| </receiver> | |
| <activity | |
| android:name=".ClientAppActivity" | |
| android:exported="true" | |
| android:theme="@style/Theme.SampleApplication.NoActionBar"> | |
| <intent-filter> | |
| <action android:name="android.intent.action.MAIN" /> | |
| <category android:name="android.intent.category.LAUNCHER" /> | |
| </intent-filter> | |
| </activity> | |
| </application> | |
| </manifest> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class ClientAppActivity : AppCompatActivity() { | |
| override fun onCreate(savedInstanceState: Bundle?) { | |
| super.onCreate(savedInstanceState) | |
| setContentView(R.layout.activity_main2) | |
| findViewById<Button>(R.id.button_explicit_broadcast).setOnClickListener { | |
| sendBroadcast(Intent(applicationContext, MyReceiver::class.java)) | |
| // log printed: | |
| // MyReceiver#onReceive: Intent { flg=0x10 cmp=ru.maksim.sample/.MyReceiver } | |
| } | |
| findViewById<Button>(R.id.button_implicit_broadcast).setOnClickListener { | |
| sendBroadcast(Intent("ru.maksim.sample.MY_ACTION")) | |
| // nothing happens | |
| } | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyBootCompletedReceiver : BroadcastReceiver() { | |
| override fun onReceive(context: Context, intent: Intent) { | |
| // This method is called when the BroadcastReceiver is receiving an Intent broadcast. | |
| Log.d("SampleApp", "MyBootCompletedReceiver#onReceive: $intent") | |
| // Log printed after device reboot via adb reboot: | |
| // MyBootCompletedReceiver#onReceive: Intent { act=android.intent.action.BOOT_COMPLETED flg=0x89000010 | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| class MyReceiver : BroadcastReceiver() { | |
| override fun onReceive(context: Context, intent: Intent) { | |
| // This method is called when the BroadcastReceiver is receiving an Intent broadcast. | |
| Log.d("SampleApp", "MyReceiver#onReceive: $intent") | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment