Android power management related application skills

Source: Internet
Author: User
    Android power management is a complicated knowledge point for beginners. We can master this knowledge through the content introduced in this article.

     

    For those who have just been familiar with the Android operating system, they are deeply attracted by the functions of this open-source mobile operating system. In this article, we will have a deep understanding of this system by interpreting the concepts related to Android power management.

    Next we will discuss in detail at the Java application level, Android framework level, and Linux kernel level:

    Android power management application layer:

    Android provides the ready-made Android. OS. powermanager class, which is used to control the switching of the Power status of the device.

    This class has three interface functions:

     

 
 
  1. Void Gotosleep (long time );
    // Force the device to enter the sleep status

Note:

If you try to call this function at the application layer, but it cannot be successful, it seems that the permission is not enough, but it is possible to call this function in the service under the framework.

 

 
 
  1. Newwakelock (INT flags, string tag); // obtain the lock of the corresponding level

 

Flags parameter description:

 

 
 
  1. PARTIAL_WAKE_LOCK: Screen off, keyboard light off  
  2. SCREEN_DIM_WAKE_LOCK: screen dim, keyboard light off  
  3. SCREEN_BRIGHT_WAKE_LOCK: screen bright, keyboard light off  
  4. FULL_WAKE_LOCK: screen bright, keyboard bright 

 

Acquire_causes_wakeup: Forces screen and keyboard light to be turned on when a request is locked.

On_after_release: reset activity timer when the lock is released

Note:

If you have applied for partial wakelock, the system will not sleep even if you press the power key, such as when playing music.

If you have applied for another wakelocks, press the power key and the system will still enter sleep.

 

 
 
  1. Void useractivity (long when, Boolean nochangelights );
    // When a user activity event occurs, the device is switched to the full on status,
    Reset screen off timer at the same time.
  2. Sample Code:
  3. Powermanager PM = (powermanager) getsystemservice
    (Context. power_service );
  4. Powermanager. wakelock WL = PM. newwakelock
    (Powermanager. screen_dim_wake_lock, "My tag ");
  5. WL. Acquire ();
  6. .......
  7. WL. Release ();

 

Note:

1. In applications that use the preceding functions, you must add the following permissions to the manifest. xml file:

 

 
 
  1. < uses-permission android:name=
    "android.permission.WAKE_LOCK" /> 
  2. < uses-permission android:name=
    "android.permission.DEVICE_POWER" /> 

 

2. all locks must be used in pairs. If the lock is applied and not released in time, the system will fail. if you have applied for a partial wakelock but have not released it in time, the system will never be able to enter the sleep mode.

Android framework:

The main code file is as follows:

 

 
 
  1. frameworks/base/core/java/android/os/
    PowerManager.java  
  2. frameworks/base/services/java/com/android/server/
    PowerManagerService.java  
  3. frameworks/base/core/java/android/os/Power.java  
  4. frameworks/base/core/jni/android_os_power.cpp  
  5. hardware/libhardware/power/power.c 

 

Powermanagerservice. java is the core, power. java provides underlying function interfaces for interaction with the JNI layer. The JNI Layer Code is mainly in the file android_ OS _power.cpp. The interaction with Linux kernel is through power. c. Android power management interacts with the kernel mainly through the Sys File. For details, see the introduction of the kernel layer.

 

The functions of this layer are relatively complex, such as system status switching, backlight adjustment and switch, and wake lock application and release. However, this layer has nothing to do with the hardware platform, in addition, Google is responsible for maintenance and has fewer problems. If you are interested, you can view the relevant code on your own.

Kernel layer:

The main code is in the following locations:

 

 
 
  1. drivers/android/power.c 

Its interface functions for Kernel include:

 

 
 
  1. Export_symbol (android_init_suspend_lock );
    // Initialize the suspend lock. Initialization is required before use.
  2. Export_symbol (android_uninit_suspend_lock );
    // Release suspend lock-related resources
  3. Export_symbol (android_lock_suspend );
    // Apply for a lock. The corresponding unlock must be called to release it.
  4. Export_symbol (android_lock_suspend_auto_expire );
    // Apply for partial wakelock, which is automatically released after the scheduled time
  5. Export_symbol (android_unlock_suspend); // release lock
  6. Export_symbol (android_power_wakeup); // wake up the system to the on
  7. Export_symbol (android_register_early_suspend );
    // Register the early suspend driver
  8. Export_symbol (android_unregister_early_suspend );
    // Cancel the registered early suspend driver

 

 

The proc file provided to the android framework layer is as follows:

 

 
 
  1. "/Sys/android_power/acquire_partial_wake_lock"
    // Apply for partial wake lock
  2. "/Sys/android_power/acquire_full_wake_lock"
    // Apply for full wake lock
  3. "/Sys/android_power/release_wake_lock"
    // Release the corresponding wake lock
  4. "/Sys/android_power/request_state"
    // The request changes the system status, including standby and wakeup.
  5. "/Sys/android_power/state" // indicates the status of the current system

 

 

Android power management is mainly implemented through wake lock. At the underlying layer, it is managed through the following three Queues:

 

 
 
  1. static LIST_HEAD(g_inactive_locks);  
  2. static LIST_HEAD(g_active_partial_wake_locks);  
  3. static LIST_HEAD(g_active_full_wake_locks); 

 

All initialized locks will be inserted into the g_inactive_locks queue, and the currently active partial wake lock will be inserted into the g_active_partial_wake_locks queue, the full wake lock of the activity is inserted into the g_active_full_wake_locks queue. All the partial wake locks and full wake locks will be moved to the inactive queue after expiration or unlock, waiting for the next call.

 

    To use wake lock on the kernel layer, follow these steps:

     

    1. Call the android_init_suspend_lock function to initialize a wake lock.

    2. call the related functions android_lock_suspend or android_lock_suspend_auto_expire to request the lock. Here, you can only apply for the partial wake lock. If you want to apply for the full wake lock, you need to call the handler function (this function does not ), this name is a bit strange. Do not confuse it with android_lock_suspend_auto_expire.

    3. If it is auto expire's wake lock, you can ignore it. Otherwise, you must release the relevant wake lock in time. Otherwise, the system will run at High Power Consumption for a long time.

    4. Remember to call android_uninit_suspend_lock to release resources in time when the driver is uninstalled or no longer used.

     

    Android power management is introduced here.

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: info-contact@alibabacloud.com 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.