gpt4 book ai didi

android-service - 安卓唤醒锁问题

转载 作者:行者123 更新时间:2023-12-04 04:13:11 25 4
gpt4 key购买 nike

我有前台服务。

list 包含此权限:

<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />

在 Splash 事件中,我请求此权限一次:

Intent intent = new Intent();
String packageName = context.getPackageName();
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
if (!pm.isIgnoringBatteryOptimizations(packageName)) {
intent.setAction(Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS);
intent.setData(Uri.parse("package:" + packageName));
}
context.startActivity(intent);

在 onCreate 服务方法中调用 createAlarm 方法:

private void createAlarm() {
wakeupIntent = PendingIntent.getBroadcast(getBaseContext(), 0, new Intent("com.android.internal.location.ALARM_WAKEUP"), 0);
powerManager = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, getPackageName() + ":TrackService");
wakeLock.acquire();

dozeHandler = new Handler();
Runnable heartbeat = new Runnable() {
@Override
public void run() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
try {
if (powerManager != null && powerManager.isDeviceIdleMode()) {
try {
Log.d("MainService", "In IDLE MODE");
wakeupIntent.send();
} catch (SecurityException | PendingIntent.CanceledException e) {
Log.d("MainService", "Heartbeat location manager keep-alive failed", e);
}
} else if (!powerManager.isDeviceIdleMode()) {
Log.d("MainService", "Device alive");
}
} finally {
if (dozeHandler != null)
dozeHandler.postDelayed(this, 5000);
}
}
};
heartbeat.run();

AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, MyWakefulReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, 30000, pendingIntent);
} else {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, 30000, pendingIntent);
}
}

在调用 setWakeLock 的所有初始化之后:

@SuppressLint("WakelockTimeout")
public void setWakeMode(Context context, int mode) {
boolean wasHeld = false;
if (wakeLock != null) {
if (wakeLock.isHeld()) {
wasHeld = true;
wakeLock.release();
}
wakeLock = null;
}
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(mode | PowerManager.ON_AFTER_RELEASE, MainService.class.getName());
wakeLock.setReferenceCounted(false);
if (wasHeld) {
wakeLock.acquire();
}
}

我对这些功能感到困惑,因此没有觉醒,请帮助我。

最佳答案

boolean wasHeld = false;
if (wakeLock != null) {
if (wakeLock.isHeld()) {
wasHeld = true;
wakeLock.release();
}
wakeLock = null;
}

只有在 'wakeLock.isHeld()' 时才将 'wasHeld' 设置为 true。只有在 'wasHeld' 为真时才调用 acquire:

if (wasHeld) {
wakeLock.acquire();
}

取出“if (wasHeld)...”并调用 acquire 无论如何,我猜 wasHeld 总是假的,所以 acquire 永远不会被调用。

如果仍然无法正常工作,根据 AndroidStudio 文档,您可以:

PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,
"MyApp::MyWakelockTag");
wakeLock.acquire();

然后打电话

wakeLock.release();

完成时。

关于android-service - 安卓唤醒锁问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61315787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com