This article describes Android power management. Share it for your reference, as follows:
1. Related concepts
1. For the need for power saving, the screen dimmed when the user has no operation for a period of time and then entered a dormant state.
2. Users can only set the default screen brightness and standby time for all applications in "Settings->Sound and Display".
3. The implementation of power management is divided into two parts: kernel application. Through the interface introduced below, we can set up the power management of the application to control the state related to its hibernation (whether it is necessary to enter hibernation, adjust the CPU frequency, the switch of the keyboard light, the light and dark screen, etc.)
2. Set up several common states for power management
PARTIAL_WAKE_LOCK Screen off, keyboard light off, no sleep
SCREEN_MID_WAKE_LOCK The screen is gray, the keyboard light is off, and it does not sleep
SCREEN_BRIGHT_WEEK_LOCK The screen is on, the keyboard light is off, and it does not sleep
FULL_WAKE_LOCK The screen is on, the keyboard lights are on, and it does not sleep
3. Precautions for using power management
1. The power management of this interface can be set when onCreate and cancel the setting when onDestroy
2. The power management of this interface can be set during onResume and cancel the settings during onPause.
3. Note that the settings are in Activity, not in Application
4. Note that the application has permission to set power management in it.
5. Pay attention to unlocking and appear in pairs.
6. Be careful that multiple locks are best used for multiple purposes. Do not use multiple locks for multiple purposes to avoid errors.
7. Pay attention to the handling of locks when running in the background and exceptions
8. Be careful to add locks when connecting to or transmitting the network to avoid interruption of transmission.
9. Pay attention to locking to ensure program logic
4. Code examples
1. Source code modification
1) Introduce power management package to use related classes
import ;
2) Add variables to the class
mWakeLock;
3) Modify onCreate
public void onCreate(Bundle savedInstanceState) { (savedInstanceState); PowerManager pm =(PowerManager)getSystemService(Context.POWER_SERVICE); mWakeLock = (PowerManager.SCREEN_BRIGHT_WAKE_LOCK, "XYTEST"); (); }
4) Modify onDestroy
public void onDestroy() { (); (); }
2. File modification
PS: For more information about configuration items and their functions, please refer to the online tools of this site:
Android Manifest function and permission description:
http://tools./table/AndroidManifest
For more information about Android related content, please check out the topic of this site:Android database operation skills summary》、《Android programming activity operation skills summary》、《Android file operation skills summary》、《A summary of SD card operation methods for Android programming and development》、《Android development introduction and advanced tutorial》、《Android resource operation skills summary》、《Android View View Tips Summary"and"Android control usage summary》
I hope this article will be helpful to everyone's Android programming design.