Android Power Management Framework

來源:互聯網
上載者:User

Mobile devices have a very hard constraint on power consumption due to its limited nature: battery capacity. On a mobile phone, if power consumption is managed correctly the user experience is severely harmed. Can you imagine the consequences of not being able to call home to ask what should you bring from the supermarket? - Serious trouble. Hence, Android Power Management support, which sits on top of Linux Power Management, was designed with the premise that the CPU shouldn't consume power if no applications or services require power.

Android implements, on top of the standard Linux Power Management, a more aggressive Power Management policy, in which applications and services must request CPU resources with "wake locks" through the Android application framework and native Linux libraries in order to keep power on. If there are no active wake locks, Android will shut down the CPU.

Android Power Management Architecture

The figure below illustrates Android Power Management Architecture.

Solid elements represent Android blocks and dashed elements represent partner-specific proprietary blocks.

The Android Power Management Framework is implemented as a driver on top of Linux PM. Then, the Application PM Framework, which is written in Java, interfaces with android_power driver through JNI (Java Native Interface). This Framework is available to the user space applications through the class PowerManager. This class gives you control of the power state of the device. Hence, the application must get an instance of PowerManager in order to enforce its power requirements. Your application can obtain a handle to the PowerManager class by calling Context.getSystemService(), specifying by name which system-level service it requires. For instance:

Activity mContext = this;
PowerManager pm = (PowerManager)mContext.getSystemService(Context.POWER_SERVICE);

Then, the application informs the PM Framework its power requirements, which can be viewed as constraints for suspending the system components. These constraints are implemented by means of “wake locks”, which can be created after acquiring a handle to PowerManager. Upon creation of the wake lock, the application must specify the power management flag it needs, as shown below:

PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyTag");

Application Interface

We've seen that Android requires that applications and services request CPU resources with "wake locks" through the Android PM Application Framework, which is available to the application by means of the class PowerManager. The code snippets above illustrate how to accomplish this. Now, let's briefly describe the API, more information can be obtain on Android Power Management Framework.

Classes

android.os.PowerManager
android.os.PowerManager.WakeLock

The nested class PowerManager.WakeLock has three public methods:
void goToSleep(long time) – forces the device to go to sleep.
void newWakeLock(int flags, String tag) - gets a wake lock at the level of the flags parameter.
void userActivity(long when, boolean noChangeLights) - turns the device from whatever state it's in to full on, and resets the auto-off timer.

Wake Locks

Wake locks are used by applications and services to request CPU resources.

Wake Lock

Description

FULL_WAKE_LOCK

Wake lock that ensures that the screen and
keyboard are on at full brightness.

PARTIAL_WAKE_LOCK

Wake lock that ensures that the CPU is
running. The screen might not be on. With this lock the CPU will
continue to run, irrespective of any timers and even after the user
presses the power button. In all other wake locks, the CPU will run,
but the user can still put the device to sleep using the power button.

SCREEN_BRIGHT_WAKE_LOCK

Wake lock that ensures that the screen is on
at full brightness; the keyboard backlight will be allowed to go off.

SCREEN_DIM_WAKE_LOCK

Wake lock that
ensures that the screen is on, but the keyboard backlight will be
allowed to go off, and the screen backlight will be allowed to go dim.

The above flags are mutually exclusive, hence you can specify only one of them. In addition to the above flags, two more flags, which affect the behaviour of the screen only.

 

Wake Lock

Description

ACQUIRE_CAUSES_WAKEUP

Normally wake locks don't actually wake the
device, they just cause it to remain on once it's already on. Think of
the video player app as the normal behaviour. Notifications that pop up
and want the device to be on are the exception; use this flag to be
like them.

ON_AFTER_RELEASE

If this flag is set, the user activity timer
will be reset when the WakeLock is released, causing the illumination
to remain on a bit longer. This can be used to reduce flicker if you
are cycling between wake lock conditions.

 

Summary

Android implements a very aggressive Power Management Framework on top of Linux PM. In this Framework, the application informs the PM Framework its power requirements, which can be viewed as constraints for suspending the system components. The Android Framework exposes power management to services and applications through the PowerManager class. Applications and services in need of CPU resources must request them from the PM Framework in the following steps:

1.Get a handle to the PowerManager.
2.Create a WakeLock and specify the power management flag.
3.Acquire a wake lock.
4.Perform operation (play MP3, open HTML page, etc.).
5.Release wake lock.

The code snippet below illustrates these steps.

Activity mContext = this;PowerManager pm = (PowerManager)mContext.getSystemService(                                          Context.POWER_SERVICE);PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK                                          | PowerManager.ON_AFTER_RELEASE,                                           “MyTag”);wl.acquire(); // … Perform operationswl.release();
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.