I’m just starting with android development and have written an application to show details about the battery status.
Now I wanted to put the whole thing into an widget – and here’s the question:
Do I really need a update service in the background to listen for ACTION_BATTERY_CHANGED intents? My first thought was to put just a
line into the intet-filter tag of the widget in the AndroidManifest.xml – but obviously it’s not that simple.
If the UpdateService is the right way I’ll do so – but I just wanted to make sure what the proper solution is
You cannot use a manifest-registered
BroadcastReceiverforACTION_BATTERY_CHANGED. It can only be registered by an activity or service viaregisterReceiver().If you do not have a service in memory for other reasons, a better approach is to poll. Set up an
AlarmManagerto check the battery level every so often (configurable, please!). To check the battery level, callregisterReceiver()for anACTION_BATTERY_CHANGEDIntentFilterwithout aBroadcastReceiver(nullfor the first parameter). This will return the last-broadcastACTION_BATTERY_CHANGEDIntent, from which you can get the most-recent charge level.