I have seen many questions along these lines, and I keep seeing the same answer over and over. I don’t want to have to have a Service for every kind of widget my app has, especially seeing as my app already has 2 persistent services.
Specifically, if one of my existing services sees that data has changed, I want to update my widgets. Doing this on a timer is annoying, as it could be days between updates or there might be several within one hour. I want my widgets to ALWAYS show up to date information.
Android widget design seems to work on the basis that your widget pulls information when it wants it, I think there are many sensible scenarios where an activity may wish to push data to a widget.
Read the answer below for how I worked out how to do precisely this. As far as I can see there are no adverse effects if it is done properly.
To force our widgets for a particular widget provider to be updated, we will need to do the following:
Step 1 – Set up our AppWidgetProvider
I will not go through the details of creating the info xml file or changes to the Android Manifest – if you don’t know how to create a widget properly, then there are plenty of tutorials out there you should read first.
Here is an example
AppWidgetProviderClass:Step 2 – Sending the broadcast
Here we can create a static method that will get our widgets to update. It is important that we use our own keys with the widget update action, if we use AppWidgetmanager.EXTRA_WIDGET_IDS we will not only break our own widget, but others as well.
If using this method with multiple providers MAKE SURE they use different keys. Otherwise you may find widget a’s code updating widget b and that can have some bizarre consequences.
Step 3 – updating on click
Another nice thing to do is to get our widget to update magiacally whenever it is clicked. Add the following code into the update method to acheive this:
This code will cause the onUpdate method to be called whenever the widget is clicked on.
Notes