Android FusedLocationProvider stops giving location when phone is idle

I'm working on location tracking application using FusedLocationProvider. I have a background service which tracks location of phone in every 5 minutes.

All works well with it, but once the phone goes idle then after 3 to 4 hours of time, the background service stops to take location. When user unlocks the phone the tracking start again.

Can someone please guide me what could be causing the issue?

Answers


One possibility could be Android M Doze Mode. When the device is unplugged and stationary for a period of time, the system attempts to conserve battery by restricting apps access to CPU-intensive services. Doze mode starts after about 1h of inactivity, periodic tasks etc. are then scheduled to maintenance windows. When the user unlocks the device, doze mode is turned off again.

You find more information about Doze Mode in the developer docs: http://developer.android.com/training/monitoring-device-state/doze-standby.html


Maybe your service is being stopped because the phone needs to free up memory so it kills your service. Make sure your service is set as a foreground service.

A foreground service is a service that's considered to be something the user is actively aware of and thus not a candidate for the system to kill when low on memory. A foreground service must provide a notification for the status bar, which is placed under the "Ongoing" heading, which means that the notification cannot be dismissed unless the service is either stopped or removed from the foreground. http://developer.android.com/guide/components/services.html

Notification notification = new Notification(R.drawable.icon, getText(R.string.ticker_text),
        System.currentTimeMillis());

Intent notificationIntent = new Intent(this, ExampleActivity.class);

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);

notification.setLatestEventInfo(this, getText(R.string.notification_title),
        getText(R.string.notification_message), pendingIntent);

startForeground(ONGOING_NOTIFICATION_ID, notification);

Android will put your service to sleep after being idle for a while. You can use WakeLock to prevent that from happening.

public int onStartCommand (Intent intent, int flags, int startId)
{
    PowerManager mgr = (PowerManager)getSystemService(Context.POWER_SERVICE);
    mWakeLock = mgr.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyWakeLock");
    mWakeLock.acquire();
    ...

    return START_STICKY;
}

public void onDestroy(){
    ...
    mWakeLock.release();
}

Need Your Help

Python - Return synonyms for a word in a list

python list api

I want to be able to use a function:

Bitmap constructor with scan0 and unmanaged resources

c# .net bitmap garbage-collection

I have written some code to retrieve frames from a camera, along with information obtained from these frames, and to display them on a form.