How to keep application awake in background

痴心易碎 提交于 2019-12-08 06:36:01

问题


I program an application, which is connected to bluetooth device and used in a car (in the background) and navigation (or something else) in the foreground.

But on android 7+ (maybe also 6), application go to sleep after some time. When I'm trying to take picture from camera the "sleep mode" is immediately and my app is sleeping now (no bluetooth connection, no notifications) - just dead app.

I must go to recent apps -> click on my app and this make the "wake up". Bluetooth is now connecting to device again.

But I can't still check if the app is sleeping or not. So, how to keep app awake also in background?

I read some about WakeLock, but it looks like it's not working, cause app still sleeping. BUút maybe I'm using it wrong.

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

and onDestroy

wk.release();

Thanks


回答1:


From Android API 23 onward, Google has introduced Doze mode and App Standby in order to conserve battery power. When a device enters doze mode or an app enters App Standby, all tasks done by an app is deferred. We had an issue with an Alarm which was not firing because of the same. If you read upon docs, you will find that using a Wake Lock is not gonna help either.

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

So if you want to bypass all these, your app must have a process in the foreground. Once again from the same doc -

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).

In your case run a service and raise it's priority to foreground.




回答2:


for wakelock to work, u should add this line to your androidmanifest file at the beginning. but as Ajay said, u should use a service so that the service works in background and do the job but if you want to keep the screen on that's something else which i think it is not the case here.

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



回答3:


why don't you use service? WakeLock basically keeps the screen on which is not an efficient mechanism

https://developer.android.com/training/scheduling/wakelock.html




回答4:


xml layout file under layout type:

android:keepScreenOn="true">



来源:https://stackoverflow.com/questions/42057958/how-to-keep-application-awake-in-background

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!