If i would like to develop a simple battery widget, which update the widget picture whenever any battery information changes (AC plugged, battery level changes), how should i keep track to the battery status? i do consider to use AlarmManager but then the widget will be updated periodically (say, 5mins), how can i make the update be done right after any event happens?
Asked
Active
Viewed 2,458 times
0
-
Please stick with your original strategy. The solution requires you to have a `Service` running all of the time with a `BroadcastReceiver` watching for `ACTION_BATTERY_CHANGED`. This is user-hostile -- apps like this are the reason why the "task killer" was invented. – CommonsWare Apr 10 '11 at 16:10
-
What happens to me is my service will not be functioning after a certain period of time. I believe is somehow the Android system disable that and release the resources it holds. I tried to startForeground to avoid this but a notification msg will be shown which is so annoying. – Eason Kazuya Yim Apr 11 '11 at 02:33
-
Kazyua Kim: "What happens to me is my service will not be functioning after a certain period of time" -- which is why I am recommending that you do not create an everlasting service, and that you use `AlarmManager`. – CommonsWare Apr 11 '11 at 11:57
-
but for the battery widget available on the market currently, they all can give an immediate response when the device is plugged. how can that be done without a service staying all the time? – Eason Kazuya Yim Apr 14 '11 at 03:34
-
It cannot. ACTION_BATTERY_CHANGED receivers only receive notifications when they are running. [http://developer.android.com/reference/android/content/Intent.html#ACTION_POWER_DISCONNECTED] – Programmer Bruce May 20 '11 at 05:17
-
I have written a blog about how to create a battery widget including a working sample program which you can download. Maybe you can use this as a starting point. See http://www.cmwmobile.com for more information. – Casper Dec 03 '12 at 11:46
1 Answers
0
Sounds like you need a broadcast receiver.
Already answered here: Android Battery in SDK
Quoting Jarret:
You can register an Intent receiver to receive the broadcast for ACTION_BATTERY_CHANGED: http://developer.android.com/reference/android/content/Intent.html#ACTION_BATTERY_CHANGED. The docs say that the broadcast is sticky, so you'll be able to grab it even after the moment the battery state change occurs
-
but how can i get rid of the service being killed after some time? – Eason Kazuya Yim Apr 11 '11 at 02:33
-
If you start your service STICKY It will only be stopped when the system is running out of resources. and even then you could set an alarm for it to restart in onLowMemory() – Blundell Apr 11 '11 at 07:38
-
the sdk said that onLowMemory is for releasing the memory occupied by the service instead of how to restart the service. are there any other method that i can ensure my service is running properly all the time? i can see some Battery Widget on the market, they all have a service running to monitor the device's situation all the time – Eason Kazuya Yim Apr 14 '11 at 03:36
-
Simply start your service, and register for it to be restarted upon phone turned on. Most the time the OS manaages widgets for you. Why don't you test it first and then deal with the issues that arise. – Blundell Apr 14 '11 at 08:56