Reputation: 61
I have a requirement, where my android app needs to check for location every 15 min and update it to SQLite DB. This process should happen even if app removed from background. I tried all the following ways, but none of them are working when app running for long time.
mHandlerTask = new Runnable() {
@Override
public void run() {
doCheckLocation();
mHandler.postDelayed(mHandlerTask, 60000 * 15);
}
};
mHandler.postDelayed(mHandlerTask, 60000 * 15);
This is working fine, but if screen turned off that runnable is not triggering. I think it's because of doze mode.
Work manager with Periodic Work Request: This is also not triggering when device goes to doze mode / screen turned off.
Alarm Manager: This has few functions setExactAndAllowWhileIdle()
, setAlarmClock()
for triggering at exact time. But the battery consumption is very high and phone gets heated up if I'm using setAlarmClock()
.
So I wanna try with a wake lock. When screen getting turned off I acquire it. I'll release it when screen is on. But wake lock is going to drain the battery, and most likely OS can kill my app.
Can anyone suggest a better way to solve this problem?
Upvotes: 0
Views: 226
Reputation: 19243
WorkManager
and AlarmManager
are in fact most efficient ways for scheduling some tasks in your app, but you just have requirements that aren't battery-efficient and won't be ever... both location tracking and "waking up" device every 15 minutes will cause high battery drain and I doubt you can prevent this
Upvotes: 0