如果调试过程中有一段时间没有操作android机,那么它会自动关闭屏幕。这时需要按一下电源按钮才能唤醒。
怎样才能去掉这个烦人的按下电源按钮的操作呢?
经过搜索与尝试,发现了下面的方案:
“利用adb shell am start命令启动一个能点亮屏幕的app。”
1. 构造app
这个能强制点亮屏幕的app使用了PowerManager的 ACQUIRE_CAUSES_WAKEUP标记。
api reference对该标记的说明:“Normal wake locks don't actually turn on the illumination. Instead, they cause the illumination to remain on once it turns on (e.g. from user activity). This flag will force the screen and/or keyboard to turn on immediately, when the WakeLock is acquired. A typical use would be for notifications which are important for the user to see immediately.”
关键代码如下:
powerManager = (PowerManager)this.getSystemService(Context.POWER_SERVICE);
@SuppressWarnings("deprecation")
int levelAndFlags = PowerManager.ACQUIRE_CAUSES_WAKEUP | PowerManager.FULL_WAKE_LOCK;
wakeLock = powerManager.newWakeLock(levelAndFlags, "com.hoxily.wakeup");
wakeLock.acquire(5000);
点亮屏幕后app即可退出了。
2. 添加外部工具按钮
在Eclipse IDE的外部工具按钮上进行配置,执行adb命令:
adb shell am start -n com.hoxily.wakeup/.MainActivity
如图所示
参考文档
-
把外部工具和构建器集成到 Eclipse 中,http://www.ibm.com/developerworks/cn/education/opensource/os-eclipse-tools/index.html
附件
-
Wakeup.apk, http://pan.baidu.com/s/1ntE9PlJ
-
Wakeup-src.zip,http://pan.baidu.com/s/1dD2ETo9