Android Development Tutorial Power Management detailed _android

Source: Internet
Author: User

This example describes the power management of Android. Share to everyone for your reference, specific as follows:

I. Related Concepts

1. For the needs of electricity saving, the general application of the user for a period of time without operation of the screen darkened, and then into hibernation

2. Users can set all the default screen brightness and standby time in "Set-> sound and display"

3. Power management implementation of the two parts of the core application, through the interface described below, we can set up the application of power management to control its sleep-related state (whether need to go into hibernation, adjust the CPU frequency, keyboard lights switch, screen light and dark, etc.)

Two, set up the power management commonly used several states

Partial_wake_lock screen off, keyboard light off, no sleep
Screen_mid_wake_lock screen ash, keyboard light off, no sleep
Screen_bright_week_lock screen light, keyboard light off, no sleep
Full_wake_lock screen is lit, keyboard light is on, no sleep

Third, the use of power management considerations

1. You can set the power management of the interface when OnCreate, and cancel the setting when OnDestroy
2. You can set the power management of the interface when Onresume, and cancel the setting when OnPause
3. Note that the setting is in the activity unit, not in the application unit
4. Note In Androidmanifest.xml that the application has permissions to set power management
5. Note Lock unlock to appear in pairs
6. Note that multiple uses are best to use multiple locks, do not use a lock multi-purpose, so as to avoid errors
7. Note The handling of locks when running in background and exceptions
8. Note that it is best to lock the network connection or transmission to prevent transmission from being interrupted
9. Note Lock to ensure program logic

Iv. Examples of code

1. Source Code Modification

1) Introduction of power Management Packs to use related classes

Import Android.os.PowerManager;

2) Add a variable to the class

Powermanager.wakelock Mwakelock;

3) Modify OnCreate

public void OnCreate (Bundle savedinstancestate) {
   super.oncreate (savedinstancestate);
   PowerManager pm = (powermanager) getsystemservice (context.power_service);
   Mwakelock = Pm.newwakelock (Powermanager.screen_bright_wake_lock, "xytest");
   Mwakelock.acquire ();

4) Modify OnDestroy

public void OnDestroy ()
   Super.ondestroy ();
   Mwakelock.release ();

2. androidmanifest.xml File Modification

Copy Code code as follows:
<uses-permission android:name= "Android.permission.WAKE_LOCK"/>

PS: More about Androidmanifest.xml configuration items and their functions can refer to the site online tools:

Android manifest Features and Permissions description Encyclopedia:

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.