How to keep application awake in background

一世执手 提交于 2019-12-06 15:56:47

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.

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" />

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

xml layout file under layout type:

android:keepScreenOn="true">

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