3

我正在尝试为服务实现唤醒锁和 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");
}

知道为什么会这样吗?提前致谢!

4

1 回答 1

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");
于 2012-12-12T11:33:09.203 回答