---
title: "PowerManager源码"
author: "mmdev"
date: "2011-12-10T09:15:00Z"
tags: []
slug: "powermanager"
draft: false
lastmod: "2011-12-10T16:18:57Z"
id: 1301536
url: "https://mmdev.iteye.com/blog/1301536"
---

http://hi-android.info/src/android/os/PowerManager.java.html  
PowerManager类为对Android电源进行管理提供了接口。它位于android/os/PowerManager.java，但是它本身实现的东西很少，主要是调用了些更底层（C/C++）的函数.

关于如何通过PowerManager类来对Android电源进行管理请参考《[PowerManager](http://hubingforever.blog.163.com/blog/static/17104057920115132032250/ "阅读全文")》，《**[Android电源管理简介](http://hubingforever.blog.163.com/blog/static/17104057920115532523699/ "阅读全文")》**

源文件：PowerManager.java

package android.os;

import android.util.Log;

import com.android.internal.os.RuntimeInit;

public classPowerManager

{

private static final String TAG = "PowerManager";

private static final int WAKE\_BIT\_CPU\_STRONG = 1;

private static final int WAKE\_BIT\_CPU\_WEAK = 2;

private static final int WAKE\_BIT\_SCREEN\_DIM = 4;

private static final int WAKE\_BIT\_SCREEN\_BRIGHT = 8;

private static final int WAKE\_BIT\_KEYBOARD\_BRIGHT = 16;

private static final int WAKE\_BIT\_PROXIMITY\_SCREEN\_OFF = 32;

private static final int LOCK\_MASK = WAKE\_BIT\_CPU\_STRONG

| WAKE\_BIT\_CPU\_WEAK

| WAKE\_BIT\_SCREEN\_DIM

| WAKE\_BIT\_SCREEN\_BRIGHT

| WAKE\_BIT\_KEYBOARD\_BRIGHT

| WAKE\_BIT\_PROXIMITY\_SCREEN\_OFF;

public static final int PARTIAL\_WAKE\_LOCK = WAKE\_BIT\_CPU\_STRONG;

public static final int FULL\_WAKE\_LOCK = WAKE\_BIT\_CPU\_WEAK | WAKE\_BIT\_SCREEN\_BRIGHT

| WAKE\_BIT\_KEYBOARD\_BRIGHT;

public static final int SCREEN\_BRIGHT\_WAKE\_LOCK = WAKE\_BIT\_CPU\_WEAK | WAKE\_BIT\_SCREEN\_BRIGHT;

public static final int SCREEN\_DIM\_WAKE\_LOCK = WAKE\_BIT\_CPU\_WEAK | WAKE\_BIT\_SCREEN\_DIM;

/\**

\* Wake lock that turns the screen off when the proximity sensor activates.

\* Since not all devices have proximity sensors, use

\* {@link #getSupportedWakeLockFlags() getSupportedWakeLockFlags()} to determine if

\* this wake lock mode is supported.

\*

\* {@hide}

\*/

public static final int PROXIMITY\_SCREEN\_OFF\_WAKE\_LOCK = WAKE\_BIT\_PROXIMITY\_SCREEN\_OFF;

/\**

\* Flag for {@link WakeLock#release release(int)} to defer releasing a

\* {@link #WAKE\_BIT\_PROXIMITY\_SCREEN\_OFF} wakelock until the proximity sensor returns

\* a negative value.

\*

\* {@hide}

\*/

public static final int WAIT\_FOR\_PROXIMITY\_NEGATIVE = 1;

public static final int ACQUIRE\_CAUSES\_WAKEUP = 0x10000000;

public static final int ON\_AFTER\_RELEASE = 0x20000000;

/\**

\* Class lets you say that you need to have the device on.

\*

\* &lt;p&gt;Call release when you are done and don't need the lock anymore.

\*/

public class WakeLock

{

static final int RELEASE\_WAKE\_LOCK = 1;

Runnable mReleaser = new Runnable() {

public void run() {

release();

}

};

int mFlags;

String mTag;

IBinder mToken;

int mCount = 0;

boolean mRefCounted = true;

boolean mHeld = false;

WorkSource mWorkSource;

WakeLock(int flags, String tag)

{

switch (flags & LOCK\_MASK) {

case PARTIAL\_WAKE\_LOCK:

case SCREEN\_DIM\_WAKE\_LOCK:

case SCREEN\_BRIGHT\_WAKE\_LOCK:

case FULL\_WAKE\_LOCK:

case PROXIMITY\_SCREEN\_OFF\_WAKE\_LOCK:

break;

default:

throw new IllegalArgumentException();

}

mFlags = flags;

mTag = tag;

mToken = new Binder();

}

/\**

\* Sets whether this WakeLock is ref counted.

\*

\* &lt;p&gt;Wake locks are reference counted by default.

\*

\* @param value true for ref counted, false for not ref counted.

\*/

public void setReferenceCounted(boolean value)

{

mRefCounted = value;

}

/\**

\* Makes sure the device is on at the level you asked when you created

\* the wake lock.

\*/

public void acquire()

{

synchronized (mToken) {

if (!mRefCounted || mCount++ == 0) {

try {

mService.acquireWakeLock(mFlags, mToken, mTag, mWorkSource);

} catch (RemoteException e) {

}

mHeld = true;

}

}

}

/\**

\* Makes sure the device is on at the level you asked when you created

\* the wake lock. The lock will be released after the given timeout.

\*

\* @param timeout Release the lock after the give timeout in milliseconds.

\*/

public void acquire(long timeout) {

acquire();

mHandler.postDelayed(mReleaser, timeout);

}

/\**

\* Release your claim to the CPU or screen being on.

\*

\* &lt;p&gt;

\* It may turn off shortly after you release it, or it may not if there

\* are other wake locks held.

\*/

public void release()

{

release(0);

}

/\**

\* Release your claim to the CPU or screen being on.

\* @param flags Combination of flag values to modify the release behavior.

\* Currently only {@link #WAIT\_FOR\_PROXIMITY\_NEGATIVE} is supported.

\*

\* &lt;p&gt;

\* It may turn off shortly after you release it, or it may not if there

\* are other wake locks held.

\*

\* {@hide}

\*/

public void release(int flags)

{

synchronized (mToken) {

if (!mRefCounted || --mCount == 0) {

try {

mService.releaseWakeLock(mToken, flags);

} catch (RemoteException e) {

}

mHeld = false;

}

if (mCount &lt; 0) {

throw new RuntimeException("WakeLock under-locked " + mTag);

}

}

}

public boolean isHeld()

{

synchronized (mToken) {

return mHeld;

}

}

public void setWorkSource(WorkSource ws) {

synchronized (mToken) {

if (ws != null && ws.size() == 0) {

ws = null;

}

boolean changed = true;

if (ws == null) {

mWorkSource = null;

} else if (mWorkSource == null) {

changed = mWorkSource != null;

mWorkSource = new WorkSource(ws);

} else {

changed = mWorkSource.diff(ws);

if (changed) {

mWorkSource.set(ws);

}

}

if (changed && mHeld) {

try {

mService.updateWakeLockWorkSource(mToken, mWorkSource);

} catch (RemoteException e) {

}

}

}

}

public String toString() {

synchronized (mToken) {

return "WakeLock{"

\+ Integer.toHexString(System.identityHashCode(this))

\+ " held=" + mHeld + ", refCount=" + mCount + "}";

}

}

@Override

protected void finalize() throws Throwable

{

synchronized (mToken) {

if (mHeld) {

Log.wtf(TAG, "WakeLock finalized while still held: " + mTag);

try {

mService.releaseWakeLock(mToken, 0);

} catch (RemoteException e) {

}

}

}

}

}

public WakeLock newWakeLock(int flags, String tag)

{

if (tag == null) {

throw new NullPointerException("tag is null in PowerManager.newWakeLock");

}

return new WakeLock(flags, tag);

}

public void userActivity(long when, boolean noChangeLights)

{

try {

mService.userActivity(when, noChangeLights);

} catch (RemoteException e) {

}

}

public void goToSleep(long time)

{

try {

mService.goToSleep(time);

} catch (RemoteException e) {

}

}

/\**

\* sets the brightness of the backlights (screen, keyboard, button).

\*

\* @param brightness value from 0 to 255

\*

\* {@hide}

\*/

public void setBacklightBrightness(int brightness)

{

try {

mService.setBacklightBrightness(brightness);

} catch (RemoteException e) {

}

}

/\**

\* Returns the set of flags for {@link #newWakeLock(int, String) newWakeLock()}

\* that are supported on the device.

\* For example, to test to see if the {@link #PROXIMITY\_SCREEN\_OFF\_WAKE\_LOCK}

\* is supported:

\*

\* {@samplecode

\* PowerManager pm = (PowerManager) getSystemService(Context.POWER\_SERVICE);

\* int supportedFlags = pm.getSupportedWakeLockFlags();

\* boolean proximitySupported = ((supportedFlags & PowerManager.PROXIMITY\_SCREEN\_OFF\_WAKE\_LOCK)

\* == PowerManager.PROXIMITY\_SCREEN\_OFF\_WAKE\_LOCK);

\* }

\*

\* @return the set of supported WakeLock flags.

\*

\* {@hide}

\*/

public int getSupportedWakeLockFlags()

{

try {

return mService.getSupportedWakeLockFlags();

} catch (RemoteException e) {

return 0;

}

}

public boolean isScreenOn()

{

try {

return mService.isScreenOn();

} catch (RemoteException e) {

return false;

}

}

public void reboot(String reason)

{

try {

mService.reboot(reason);

} catch (RemoteException e) {

}

}

private PowerManager()

{

}

public PowerManager(IPowerManager service, Handler handler)

{

mService = service;

mHandler = handler;

}

IPowerManager mService;

Handler mHandler;

}

注意：* {@hide}表示该方法对外部开发用户是隐藏了，Android的对外API中并不提供该方法。

只有在Android系统的framework或应用程序中才能用。