Wakelocks

来自WordPress中文文档
跳转至: 导航搜索

Android Wakelocks

XDA forum member chamonix has created the perfect app to help you track down and eliminate causes for excessive drain. Using undocumented APIcalls to the batteryinfo service, BetterBatteryStats is able to monitor process stats, network usage, and wakelock stats on your Android 2.2- or 2.3-equipped device. http://www.xda-developers.com/android/tune-your-androids-battery-life-with-betterbatterystats/

Wakelocks statistics are good indicators if the battery drain is high when the phone is off and supposed to sleep. Android Froyo provided battery history to spare parts and BetterBatteryStats [APP] adds battery history back to Gingerbread. A good place to start is to unplug your phone and switch the screen off for a few hours. Then look at Better Battery Stats "Since unplugged" and see what apps/processes have prevented the phone from sleeping. Don't go through the whole list, check the top1-2 first and iterate till there are no values through the roof.

1/ Reboot the phone

2/ Leave it in standby for at least one hour (2 hours would be better)

3/ After that time open BetterBatteryStats and check under 'Process since charged' and 'Wakelocks since charged'

Then you will see what has been causing wake and which processes have been running since the device restarted.

This wiki lists some wakelocks with links to further information and suggestions on how to reduce the number of wakelocks and therefore reduce battery usage while the phone is off and meant to be asleep.

Activity Manager

http://developer.android.com/reference/android/app/ActivityManager.html

Activity Manager is a system process and looks after processes including tasks you have recently started, running processes, or those that may be in an 'error' position.

If you were not to use your device for a while then this activity will be very low. If however you were to have opened lots of apps just before turning your screen off, then this activity will be active for a while before entering true deep sleep.

Activity Manager is normal. If you stop it your phone won't work. If you want to reduce it's 'footprint' however, make sure you exit apps you no longer need before turning the screen off. By this I mean pressing the back arrow to exit them rather than pressing the home button to return to the homescreen - and leaving them running in the background.

Just to re-iterate on hitting the back button to leave apps and why it helps reducing wakelocks. It is related to the lifecycle on activities (jargon for windows). You can see onPause is an important transition because it's the transition where the persistent state is to be saved (eg to a database). Pressing back on an activity calls onPause, meaning android can remove the app without the overhead of doing I/Os and the associated Garbage collection later as it does that straight away.

Alarm Manager

http://developer.android.com/reference/android/app/AlarmManager.html

Alarm Manager manages intents to Be triggered at Specific Times. Alarm Manager has little to do with alarm clocks. It is related to Android holding a process in state until it is either needed again or exited fully.

Quote:

This class provides access to the system alarm services. These allow you to schedule your application to be run at some point in the future. When an alarm goes off, the Intent that had been registered for it is broadcast by the system, automatically starting the target application if it is not already running. Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted. ... The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing.

Make sure you exit apps you no longer want by using the back button before you put your device to sleep. Do not use the Home button to exit an app.

RILJ

http://www.kandroid.org/online-pdk/guide/telephony.html

Dialer RILJ is the userland counterpart of the RIL (radio interface lib). RILJ handles whatever the phone need to tell or get from the RIL = phone, e.g. dialing but also coarse location.