Android power Management related application skills sharing

Source: Internet
Author: User
Tags reset sleep

For friends who have just contacted the Android operating system, they are deeply attracted to the capabilities of the Open-source mobile operating system. In this article, we have an in-depth understanding of the concept of the Android power management system.

Next we discuss in detail from the Java application level, the Android framework level, the Linux kernel level:

Use of the Android Power management application layer:

Android provides a ready-made Android.os.PowerManager class that controls the switching of the power state of a device.

This class has three external interface functions:

1.void Gotosleep (long time);

Force device into sleep state

Note:

The attempt to invoke the function at the application layer does not succeed, and the error appears to be insufficient, but it is possible to invoke the service under the framework.

2.newWakeLock (int flags, String tag);//Get the lock of the corresponding level

Flags parameter Description:

3.partial_wake_lock:screen off, keyboard light off

4.screen_dim_wake_lock:screen DIM, keyboard light off

5.screen_bright_wake_lock:screen BRIGHT, keyboard light off

6.full_wake_lock:screen Bright, keyboard bright

Acquire_causes_wakeup: Force screen and keyboard to open once a lock is requested light

On_after_release: Reset the activity timer when the lock is released

Note:

If you apply for partial wakelock, the system does not sleep even if you press the Power key, as music plays

If you apply for another wakelocks, press power, the system will still sleep.

7.void useractivity (Long when, Boolean nochangelights);

The User activity event occurs and the device is switched to the full on State,

Reset screen off timer at the same time.

8.Sample Code:

9.PowerManager pm = (powermanager) getsystemservice

(Context.power_service);

10.powermanager.wakelock WL = Pm.newwakelock

(Powermanager.screen_dim_wake_lock, "my Tag");

11.wl.acquire ();

12. ...

13.wl.release ();

Note:

1. In an application that uses the above function, you must include the following permissions in its manifest.xml file:

14.< uses-permission android:name=

"Android.permission.WAKE_LOCK"/>

15.< uses-permission android:name=

"Android.permission.DEVICE_POWER"/>

2. All locks must be used in pairs, if the application is not released in a timely manner will cause system failure. If the application for partial wakelock, but not timely release, the system will never enter the sleep mode.

Android Framework Level:

The main code files are as follows:

16.frameworksbasecorejavaandroidos

Powermanager.java

17.frameworksbaseservicesjavacomandroidserver

Powermanagerservice.java

18.frameworksbasecorejavaandroidospower.java

19.frameworksbasecorejniandroid_os_power.cpp

20.hardwarelibhardwarepowerpower.c

Where Powermanagerservice.java is the core, Power.java provides the underlying function interface, interacts with the JNI layer, the JNI layer's code mainly in the file Android_os_power.cpp, and Linux Kernel interaction is achieved through the POWER.C, the Android power management and kernel interaction is mainly through the way of the SYS file, specifically, please refer to the kernel layer introduction.

The function of this layer is comparatively complex, such as System State switching, backlight adjustment and switch, Wake lock application and release, etc., but this layer is not related to the hardware platform, and the maintenance by Google, the problem is relatively less, interested friends can see the relevant code.

Kernel layer:

Its main code is in the following locations:

21.drivers/android/power.c

The interface functions provided to Kernel are

22.export_symbol (Android_init_suspend_lock);

Initialize suspend lock, which must be initialized before use

23.export_symbol (Android_uninit_suspend_lock);

Releasing suspend lock-related resources

24.export_symbol (Android_lock_suspend);

To request lock, you must invoke the corresponding unlock to release it

25.export_symbol (Android_lock_suspend_auto_expire);

Apply for partial Wakelock and will be released automatically when time is up

26.export_symbol (Android_unlock_suspend); Release lock

27.export_symbol (Android_power_wakeup); Wake the system to On

28.export_symbol (Android_register_early_suspend);

Driver for registered early suspend

29.export_symbol (Android_unregister_early_suspend);

Cancels the driver of a registered early suspend

The proc files provided to the Android framework layer are as follows:

"/sys/android_power/acquire_partial_wake_lock"

Apply for partial wake lock

"/sys/android_power/acquire_full_wake_lock"

Apply for full wake lock

"/sys/android_power/release_wake_lock"

Release the corresponding Wake lock

"/sys/android_power/request_state"

Request to change System state, enter standby and return to wakeup two states

"/sys/android_power/state"//indicating the status of the current system

Android Power management is implemented primarily through the wake Lock, which is managed at the bottom by three queues below:

35.static List_head (g_inactive_locks);

36.static List_head (g_active_partial_wake_locks);

37.static List_head (g_active_full_wake_locks);

All initialized lock is inserted into the g_inactive_locks queue, and the currently active partial wake lock is inserted into the g_active_partial_wake_locks queue, and the active full wake Lock is inserted into the g_active_full_wake_locks queue, and all partial wake lock and full wake Lock are moved to the unlock queue after the expiration or inactive, waiting for the next call.

Use the wake lock step at the kernel layer as follows:

1. Call function Android_init_suspend_lock Initialize a wake lock

2. Call the related application lock function android_lock_suspend or Android_lock_suspend_auto_expire request lock, here can only apply for partial wake lock, if you want to apply full wake Lock, you need to call the function Android_lock_partial_suspend_auto_expire (the function is not export), this name is a little strange, do not follow the previous android_lock_suspend_auto_ Expire's confused.

3. If it is auto expire wake Lock can be ignored, otherwise it must be timely to the relevant Wake lock release, otherwise it will cause the system to run in the state of high power consumption.

4. Remember to call Android_uninit_suspend_lock release resources in a timely manner when driving uninstall or no longer uses wake lock.

The Android Power management content 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.