-
-
Notifications
You must be signed in to change notification settings - Fork 41
8. SDK: Broadcast Providers
A Broadcast Provider works with a Smartspacer Target or Complication to allow listening for implicit broadcasts without needing a constantly running service. This is only useful if the broadcasts you need to listen for are implicit, otherwise you can use a BroadcastReceiver specified in your manifest as normal.
Smartspacer handles registering the broadcast receiver for you, and will send your plugin's Broadcast Provider the raw intents so you can load any data from them, and act as required.
Follow the pages for creating a Target or Complication first.
Broadcast Providers at their core are Content Providers, but the Smartspacer SDK handles most of the logic required for a Provider to function.
The basic Broadcast Provider class is as follows:
class ExampleBroadcastProvider: SmartspacerBroadcastProvider() {
override fun onReceive(intent: Intent) {
//Handle receiving the intent
}
override fun getConfig(smartspacerId: String): Config {
//Return your config
}
}
Since Broadcast Providers are based on ContentProviders, it must be specified as one, with a specific permission. An action is not required for this provider:
<provider
android:name=".ExampleBroadcastProvider"
android:authorities="${applicationId}.broadcast.example"
android:permission="com.kieronquinn.app.smartspacer.permission.ACCESS_SMARTSPACER_BROADCASTS"
android:exported="true"/>
Note: The
authority
specified here is the authority you must specify in your Target or Complication's configuration, as thebroadcastProvider
.
The getConfig
method must return some basic information for Smartspacer about your Broadcast Provider. The available options are:
Config(
intentFilters = // A list of IntentFilters to specify when registering the receiver
)
When Smartspacer receives a broadcast on behalf of your plugin, the onReceive
method of your Broadcast Provider will be called, passing in the unmodified Intent it was sent. You can then use this as you wish, extracting any data from its extras or refreshing your Targets or Complications.