Skip to content

8. SDK: Broadcast Providers

Kieron Quinn edited this page Oct 23, 2023 · 1 revision

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.

Setup

Follow the pages for creating a Target or Complication first.

The Broadcast Provider Class

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
    }
    
}

Declaring in the Manifest

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 the broadcastProvider.

Configuration

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
)

Handling Received Broadcasts

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.