获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行

2019-07-14 01:36发布

PowerManager.WakeLock wakeLock = null; //获取电源锁,保持该服务在屏幕熄灭时仍然获取CPU时,保持运行 private void acquireWakeLock() { if (null == wakeLock) { PowerManager pm = (PowerManager)this.getSystemService(Context.POWER_SERVICE); wakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK|PowerManager.ON_AFTER_RELEASE, "PostLocationService"); if (null != wakeLock) { wakeLock.acquire(); } } } //释放设备电源锁 private void releaseWakeLock() { if (null != wakeLock) { wakeLock.release(); wakeLock = null; } }