我正在尝试为服务实现唤醒锁定和wifi锁定。有关我的方案的更多信息,请参阅:Wakelock implementation in a service。我在启动时获取并在服务被破坏时释放。我想监控我的项目的电池消耗,虽然我明白,这种方法并不好。但是当我的手机屏幕关闭时,它无法正常工作。我启用了选项:在睡眠期间保持Wifi开启。一旦我的屏幕关闭,我的服务就会停止。我正在使用片段:
PowerManager powerManager = (PowerManager)getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "My Lock");
if(!wakeLock.isHeld())
{
wakeLock.acquire();
}
Log.d(TAG,"Acquired Wake lock successfully");
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
wifiLock = wifiManager.createWifiLock(WifiManager.WIFI_MODE_FULL_HIGH_PERF , "WiFi LockTag");
if(!wifiLock.isHeld())
{
wifiLock.acquire();
}
Log.d(TAG,"Acquired Wifi lock successfully");
//发布
try{
wifiLock.release();
}catch(Exception e){
e.printStackTrace();
Log.d(TAG,"Exception while releasing WakeLock :: Can be ignoredd");
}
知道为什么会这样吗?提前谢谢!
答案 0 :(得分:0)
保护屏幕免受锁定使用此Spinet
public static void unlockTheScreen(Context context) {
KeyguardManager keyguardManager = (KeyguardManager) context.getSystemService(Context.KEYGUARD_SERVICE);
KeyguardLock keyguardLock = keyguardManager.newKeyguardLock("TAG");
keyguardLock.disableKeyguard();
}
在唤醒锁中添加以下内容
wakeLock = powermanager.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP ), "TAG");