android电源锁

2019-07-13 21:31发布

1.距离感应器用到的电源锁 //距离感应器的电源锁 WakeLock wakeLock = powerManager.newWakeLock(32, "wakeLock");
2.线控耳机插入时,控制屏幕亮或熄灭的电源锁
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP || PowerManager.SCREEN_DIM_WAKE_LOCK, "wakeLock");
3.如设置屏幕2分后锁屏
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "wakeLock");
wakeLock.acquire(2*60*1000); 4,设备电源锁的两个方法 /** * 获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行 */ private void acquireWakeLock() { if (null == wakeLock) { PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, getClass() .getCanonicalName()); if (null != wakeLock) { Log.i("ysl", "call acquireWakeLock"); wakeLock.acquire(); } } } /** 释放设备电源锁*/ private void releaseWakeLock() { if (null != wakeLock && wakeLock.isHeld()) { Log.i("ysl", "call releaseWakeLock"); wakeLock.release(); wakeLock = null; } }