Sie sind auf Seite 1von 5

Android Power Management Framework

Isaac Ben­Akiva
isaac.ben­akiva@mathembedded.com

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.

MathEmbedded Consulting Ltd.
 http://www.mathembedded.com
        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 

MathEmbedded Consulting Ltd.
 http://www.mathembedded.com
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.

MathEmbedded Consulting Ltd.
 http://www.mathembedded.com
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 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;

MathEmbedded Consulting Ltd.
 http://www.mathembedded.com
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 operations
wl.release();

References
Android Open source Project Documentation
Android Developer Reference (Javadoc)

MathEmbedded Consulting Ltd.
 http://www.mathembedded.com

Das könnte Ihnen auch gefallen