我正在尝试为服务实现唤醒锁和 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");
}
知道为什么会这样吗?提前致谢!