Ravi Kiran
Ravi Kiran

Reputation: 61

Check location every 15min even if app in foreground or killed state

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.

  1. Runnable function with a foreground service. Below is the example
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.

  1. Work manager with Periodic Work Request: This is also not triggering when device goes to doze mode / screen turned off.

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

Answers (1)

snachmsm
snachmsm

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

Related Questions