I'm working on location tracking application using FusedLocationProviderClient. 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?
LocationUpdatesBroadcastReceiver
public class LocationUpdatesBroadcastReceiver extends BroadcastReceiver {
public static final String TAG = "LUBroadcastReceiver";
public static final String ACTION_PROCESS_UPDATES =
"com.orangeapp.discountnotifier.receiver.action" +
".PROCESS_UPDATES";
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
final String action = intent.getAction();
if (ACTION_PROCESS_UPDATES.equals(action)) {
LocationResult result = LocationResult.extractResult(intent);
if (result != null) {
List<Location> locations = result.getLocations();
Utils.setLocationUpdatesResult(context, locations);
// Utils.checKOffersByLatLng(context, locations);
Utils.sendNotification(context, Utils.getLocationResultTitle(context, locations));
Log.e(TAG, Utils.getLocationUpdatesResult(context));
}
}
}
}
}
MainActivity
private PendingIntent getPendingIntent() {
// Note: for apps targeting API level 25 ("Nougat") or lower, either
// PendingIntent.getService() or PendingIntent.getBroadcast() may be used when requesting
// location updates. For apps targeting API level O, only
// PendingIntent.getBroadcast() should be used. This is due to the limits placed on services
// started in the background in "O".
// TODO(developer): uncomment to use PendingIntent.getService().
// Intent intent = new Intent(this, LocationUpdatesIntentService.class);
// intent.setAction(LocationUpdatesIntentService.ACTION_PROCESS_UPDATES);
// return PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
Intent intent = new Intent(this, LocationUpdatesBroadcastReceiver.class);
intent.setAction(LocationUpdatesBroadcastReceiver.ACTION_PROCESS_UPDATES);
return PendingIntent.getBroadcast(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
protected void startLocationUpdates() {
try {
Log.i(TAG, "Starting location updates");
Utils.setRequestingLocationUpdates(this, true);
mFusedLocationClient.requestLocationUpdates(mLocationRequest, getPendingIntent());
} catch (Exception e) {
Utils.setRequestingLocationUpdates(this, false);
e.printStackTrace();
}
}