Working on an Android widget that needs to be able to receive a broadcast from my application installed on the device itself. Basically want to be able to to notify the widget/s when certain events occur so they can update rather than wait the full 30 minutes.
Custom action: "com.example.UPDATE"
It looks like static register isn't an option anymore for my widget to receive custom intent broadcasts from my main application since they are separate entities and from the documentation it looks like Google locked down what is possible with static registers. I wouldn't want to specific a single receiver anyways as I would like to be able to support multiple widgets and each receive the broadcast if they need to.
What would be the right way to dynamically register a broadcast receiver for a widget's context?
Would I register it in the onUpdate function of my AppWidgetProvider and just make sure its registered each time the onUpdate gets called? That provides a context I could register it to.
Working on an Android widget that needs to be able to receive a broadcast from my application installed on the device itself.
You act as though there are two separate apps: the "real" app and a separate app for the app widget. I strongly encourage you not to go that route, and instead have a single app. In that case, your app can simply use AppWidgetManager
when it wishes to update the app widget's UI.
If you are convinced that having two separate apps is the right approach, read on.
It looks like static register isn't an option anymore for my widget to receive custom intent broadcasts from my main application since they are separate entities and from the documentation it looks like Google locked down what is possible with static registers
Only for implicit broadcasts. In your case, you can use an explicit broadcast, where you identify the specific app or component to receive the broadcast. setPackage()
on the Intent
should be sufficient.
What would be the right way to dynamically register a broadcast receiver for a widget's context?
Implement a foreground service and have it register for the broadcasts. It can then use AppWidgetManager
to update your app widget. Since users will not like this, I recommend that you not go this route.
Would I register it in the onUpdate function of my AppWidgetProvider and just make sure its registered each time the onUpdate gets called?
No, because your process will go away shortly after onUpdate()
returns, if there is nothing keeping that process around, such as a foreground service. This is why using a manifest-registered receiver and an explicit broadcast is a better solution.