Search code examples
androidbattery

constantly watching the battery?


I dont know if this can be done but right now i have a service that has an alarm manager which refreshed the battery every 5 mins to check the battery % and alert of the battery gets below a certain %. Is there a better way to watch the battery that does not require an alarm manager and refreshes when the battery changes because it seems better than refreshing after a certain amount of time


Solution

  • tyczj,

    I would create a BroadcastReceiver to receive the following Broadcast: ACTION_BATTERY_CHANGED. This way you aren't constantly polling the battery unnecessarily.

    Additional:

    It would be registered like this:

    <receiver android:name=".BatteryReceiver">
        <intent-filter>
            <action android:name="android.intent.action.BATTERY_CHANGED" />
        </intent-filter>
    </receiver>