热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

安卓修改屏幕超时后,系统不进入休眠的方法。

修改系统行为,让屏幕超时后,不去休眠。frameworks\base\services\core\java\com\android\server\pow

修改系统行为,让屏幕超时后,不去休眠。
frameworks\base\services\core\java\com\android\server\power\PowerManagerService,电源管理类,屏蔽休眠在此方法中修改。关键函数如下

@VisibleForTesting
void setWakefulnessLocked(int wakefulness, int reason) {if (mWakefulness != wakefulness) {mWakefulness = wakefulness;mWakefulnessChanging = true;mDirty |= DIRTY_WAKEFULNESS;if (mNotifier != null) {mNotifier.onWakefulnessChangeStarted(wakefulness, reason);}}
}


重点屏蔽调用这个函数的方法,即在调用这几个函数的方法开头处,如果不需要休眠,则直接返回false。
setWakefulnessLocked(WAKEFULNESS_AWAKE, 0);//这个不用屏蔽
setWakefulnessLocked(WAKEFULNESS_DOZING, reason);
setWakefulnessLocked(WAKEFULNESS_DREAMING, 0);
setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);

比方说调用setWakefulnessLocked(WAKEFULNESS_ASLEEP, PowerManager.GO_TO_SLEEP_REASON_TIMEOUT);方法的方法是reallyGoToSleepNoUpdateLocked(),则在方法开头处就根据情况直接返回,不去执行休眠方法。

// Done dozing, drop everything and go to sleep.private boolean reallyGoToSleepNoUpdateLocked(long eventTime, int uid) {if (DEBUG_SPEW) {Slog.d(TAG, "reallyGoToSleepNoUpdateLocked: eventTime=" + eventTime+ ", uid=" + uid);}if (eventTime

以上修改完成后,我们仍然可以知道屏幕什么时候超时,这样我们可以在超时后做一些自己的逻辑,比如通知app层,去做灭屏(只是单纯将屏幕亮度设为0)。屏幕超时后具体执行休眠代码的位置:

/*** Handler for asynchronous operations performed by the power manager.*/private final class PowerManagerHandler extends Handler {public PowerManagerHandler(Looper looper) {super(looper, null, true /*async*/);}@Overridepublic void handleMessage(Message msg) {switch (msg.what) {case MSG_USER_ACTIVITY_TIMEOUT:// add by zzh @{boolean needSleep = SystemProperties.getBoolean("persist.sys.powersave.open", true);// @}// condition and else block add by zzhif (needSleep)handleUserActivityTimeout();elsesendTimeoutBroadcast();break;case MSG_SANDMAN:handleSandman();break;case MSG_SCREEN_BRIGHTNESS_BOOST_TIMEOUT:handleScreenBrightnessBoostTimeout();break;case MSG_CHECK_FOR_LONG_WAKELOCKS:checkForLongWakeLocks();break;}}}// add by zzhpublic void sendTimeoutBroadcast() {Log.i("xxx zzh", "sendTimeoutBroadcast execute"); mContext.sendBroadcast(new Intent("company.power.save.timout"));}

 


推荐阅读
author-avatar
落花飞雪277590089
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有