gpt4 book ai didi

android - 如何让应用程序在后台保持清醒

转载 作者:行者123 更新时间:2023-11-29 17:04:48 24 4
gpt4 key购买 nike

我编写了一个应用程序,它连接到蓝牙设备并用于汽车(在后台)和导航(或其他东西)在前台。

但是在 android 7+(也可能是 6)上,应用程序会在一段时间后进入休眠状态。当我试图从相机拍照时,“ sleep 模式”立即出现,我的应用程序现在正在 sleep (没有蓝牙连接,没有通知)——只是死应用程序。

我必须转到最近的应用程序 -> 单击我的应用程序,这会“唤醒”。蓝牙现在再次连接到设备。

但我仍然无法检查该应用程序是否正在休眠。那么,如何在后台也保持应用程序唤醒?

我阅读了一些有关 WakeLock 的信息,但它似乎无法正常工作,因为应用程序仍在休眠。但也许我用错了。

PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wk = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyWakeLock");
wk.acquire();

和onDestroy

wk.release();

谢谢

最佳答案

从 Android API 23 开始,Google 引入了Doze modeApp Standby 以节省电池电量。当设备进入休眠模式或应用进入 App Standby 时,应用完成的所有任务都会延迟。我们遇到了一个问题,因为同样的原因,警报没有触发。如果您阅读文档,您会发现使用唤醒锁也无济于事。

Doze restrictions The following restrictions apply to your apps while in Doze:

Network access is suspended.

The system ignores wake locks.

Standard AlarmManager alarms (including setExact() and setWindow()) are deferred to the next maintenance window. If you need to set alarms that fire while in Doze, use setAndAllowWhileIdle() or setExactAndAllowWhileIdle(). Alarms set with setAlarmClock() continue to fire normally — the system exits Doze shortly before those alarms fire.

The system does not perform Wi-Fi scans.

The system does not allow sync adapters to run. The system does not allow JobScheduler to run

所以如果你想绕过所有这些,你的应用程序必须在前台有一个进程。再次来自同一个文档 -

The app has a process currently in the foreground (either as an activity or foreground service, or in use by another activity or foreground service).

在您的情况下运行服务并将其优先级提高到前台。

关于android - 如何让应用程序在后台保持清醒,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42057958/

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