开发者

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 an开发者_运维知识库d refreshes when the battery changes because it seems better than refreshing after a certain amount of time


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>


You can listen to the systems broadcast intents:

ACTION_BATTER_CHANGED

ACTION_POWER_CONNECTED

ACTION_POWER_DISCONNECTED

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜