Compose Broadcasts is a powerful Jetpack Compose library that simplifies the process of working with Android's BroadcastReceivers in a composable environment. It provides an intuitive API to observe and react to system-wide events and changes in your Compose UI.
- ๐ Easy integration with Jetpack Compose
- ๐ก Observe system events like battery level, airplane mode, and more
- ๐๏ธ Custom BroadcastReceiver support
- ๐งฉ Composable functions for common system events
- ๐ ๏ธ Flexible API for creating custom broadcast listeners
- โฎ๏ธ No need to worry about registering / unregistering listeners anymore
Add the following to your app's build.gradle.kts
:
// Get the latest version from GitHub Releases/Tags
implementation("io.github.shubhamsinghshubham777:composebroadcasts:0.0.1")
Add the following to your project level settings.gradle.kts
:
dependencyResolutionManagement {
repositories {
...
// Add this
maven("https://s01.oss.sonatype.org/content/repositories/snapshots/")
}
}
Here's the complete list of composables Compose Broadcasts provides at the moment:
Composable | Return Type |
---|---|
rememberBroadcastReceiverAsState | Generic (T) |
rememberIsAirplaneModeOn | Boolean |
rememberBatteryLevel | Int |
rememberIsCharging | Boolean |
rememberPackageInfo | CBPackageInfo? |
rememberCurrentTimeMillis | Long |
rememberSystemLocale | Locale |
rememberIsScreenOn | Boolean |
rememberIsHeadsetConnected | Boolean |
rememberCurrentInputMethod | CBInputMethodInfo? |
And here are some examples of how to use them in your project:
val isAirplaneModeOn by rememberIsAirplaneModeOn()
Text("Airplane mode is ${if (isAirplaneModeOn) "ON" else "OFF"}")
val batteryLevel by rememberBatteryLevel()
Text("Current battery level: $batteryLevel%")
val isCharging by rememberIsCharging()
Text("Device is ${if (isCharging) "charging" else "not charging"}")
Check out the ๐งฉ Custom BroadcastReceivers section below to learn how to create PackageInfoReceiver.
val packageInfoReceiver = PackageInfoReceiver()
val packageInfo by rememberPackageInfo(packageInfoReceiver)
Text("Last package change: $packageInfo")
val currentTimeMillis by rememberCurrentTimeMillis()
Text("Current time: ${convertMillisToTimeString(currentTimeMillis)}")
Check out the ๐งฉ Custom BroadcastReceivers section below to learn how to create LocaleReceiver.
val localeReceiver = LocaleReceiver()
val currentLocale by rememberSystemLocale(localeReceiver)
Text("Current system locale: ${currentLocale.toLanguageTag()}")
You can create custom BroadcastReceivers by extending the CBBroadcastReceiver
class:
class MyCustomReceiver : CBBroadcastReceiver(tag = "my_custom_receiver") {
override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
// Your custom logic here (if you like the old way of writing receivers)
// Ideally, your logic should be a part of the composable itself
// This class should just be left blank, for example:
// class MyCustomReceiver : CBBroadcastReceiver(tag = "my_custom_receiver")
}
}
Then, register the receiver in your AndroidManifest.xml file:
<manifest>
<application>
<receiver android:name=".MyCustomReceiver" android:exported="false">
<intent-filter>
<!-- Example: android.intent.action.PACKAGE_ADDED -->
<action android:name="YOUR_CUSTOM_ACTION" />
</intent-filter>
</receiver>
</application>
</manifest>
Finally, use it in your composable:
val customState by rememberBroadcastReceiverAsState(
initialValue = initialState,
// Example: CBIntentAction.Custom(Intent.ACTION_PACKAGE_ADDED)
intentFilters = listOf(CBIntentFilter(CBIntentAction.Custom("YOUR_CUSTOM_ACTION"))),
broadcastReceiver = MyCustomReceiver(),
) { context, intent ->
// Map the received intent to your state
}
Contributions are welcome! Please feel free to submit a Pull Request.
This library is licensed under the Apache 2.0 License. See the LICENSE file for details.