PowerManager源码

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

关于如何通过PowerManager类来对Android电源进行管理请参考《PowerManager》,《Android电源管理简介

源文件:PowerManager.java

package android.os;

import android.util.Log;

import com.android.internal.os.RuntimeInit;

public class PowerManager

{

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.

*

* <p>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.

*

* <p>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.

*

* <p>

* 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.

*

* <p>

* 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 < 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或应用程序中才能用。

PowerManager源码,布布扣,bubuko.com

时间: 2024-10-07 16:57:05

PowerManager源码的相关文章

UiAutomator源码分析之UiAutomatorBridge框架

上一篇文章<UIAutomator源码分析之启动和运行>我们描述了uitautomator从命令行运行到加载测试用例运行测试的整个流程,过程中我们也描述了UiAutomatorBridge这个类的重要性 ,说它相当于UiAutomation的代理(我们都知道UiAutomator是通过UiAutomation和AccessibilityService进行连接然后获取界面空间信息和注入事件的).那么今天开始我们就围绕这个类以及跟它有关系的类进行进一步的分析. 1. UiAutomatorBrid

Android内核开发:源码的版本与分支详解

我想很多初学者或许跟我一样,看完Android源码下载相关的文章以后,就开始兴致勃勃地去下载Android源码了,但是下载完了源码后,有没有像我一样产生如下几个困惑呢? (1) Android版本有哪些分支可用?每个分支的TAG是什么? (2) Android源码下载完了怎么没有看到Linux内核代码?Android源码对应Linux内核是否可以从kernel.org官网去下载?Android对标准的Linux内核做了哪些修改? (3) Android源码分支与Linux版本分支的对应关系是什么

Android 手电筒源码

最近由于公司需要,做了一个手电筒,其实手电筒原理很简单,就是调用照相机的闪光灯,控制闪光灯的开关,就可以实现手电筒的效果, 强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致使用照相机的时候出现--无法连接到相机 这个问题 手电筒APK下载地址:点击打开下载链接 手电筒项目源码下载:点击打开下载链接 主要代码如下: package com.techainsh.flashlight; import java.util.List; import android.hardware.C

老李推荐:第6章5节《MonkeyRunner源码剖析》Monkey原理分析-事件源-事件源概览-事件

老李推荐:第6章5节<MonkeyRunner源码剖析>Monkey原理分析-事件源-事件源概览-事件 从网络过来的命令字串需要解析翻译出来,有些命令会在翻译好后直接执行然后返回,但有一大部分命令在翻译后需要转换成对应的事件,然后放入到命令队列里面等待执行.Monkey在取出一个事件执行的时候主要是执行其injectEvent方法来注入事件,而注入事件根据是否需要往系统注入事件分为两种: 需要通过系统服务往系统注入事件:如MonkeyKeyEvent事件会通过系统的InputManager往系

android-音乐播放器实现及源码下载(三)

本系列博文,详细讲述一个音乐播放器的实现,以及从网络解析数据获取最新推荐歌曲以及歌曲下载的功能. 功能介绍如下: 1.获取本地歌曲列表,实现歌曲播放功能. 2.利用硬件加速感应器,摇动手机实现切换歌曲的功能 3.利用jsoup解析网页数据,从网络获取歌曲列表,同时实现歌曲和歌词下载到手机本地的功能. 4.通知栏提醒,实现仿QQ音乐播放器的通知栏功能. 涉及的技术有: 1.jsoup解析网络网页,从而获取需要的数据 2.android中访问网络,获取文件到本地的网络请求技术,以及下载文件到本地实现

Android FM模块学习之四源码解析(二)

上一章我们了解了FM主activity:FMRadio.java,若没查看的,请打开链接Android FM模块学习之四源码解析(一) 查看fmradio.java源码注释.接下来我们来看看FM重要的一个类:FMRadioService.java 由上一章我们已经知道,打开FM时,在OnStart函数中会bindToService来开启服务, public boolean bindToService(Context context, ServiceConnection callback) { L

Android6.0源码分析之录音功能(一)【转】

本文转载自:http://blog.csdn.net/zrf1335348191/article/details/54949549 从现在开始一周时间研究录音,下周出来一个完整的博客,监督,激励!!! 2017-02-09--------2017-02-17 ------------------------------------------------------------------------------------------------------------------------

从源码角度看Android系统SystemServer进程启动过程

copy frome :https://blog.csdn.net/salmon_zhang/article/details/93208135 SystemServer进程是由Zygote进程fork生成,进程名为system_server,主要用于创建系统服务. 备注:本文将结合Android8.0的源码看SystemServer进程的启动过程以及SystemServer进程做了哪些重要工作. 1. SystemServer进程启动的起点从<从源码角度看Android系统Zygote进程启动过

小说分销系统,微信小说分销,类掌中云小说系统,类818tu系统源码

[演示站参数][][][][][][][][][][][] [后 台 地 址]     http://xiaoshuo.qqsiot.cn/manager          [] [管理员账号]     admin                                                     [] [渠道商账号]     channel                                                  [] [代理商账号]     age