0

I need a background service which runs continuously for my app. I looked online for different ways like foreground service which continuously shows notification for versions above android 8, Work Manager and Job Scheduler. I think Work Manager suits my requirements in terms of background running but not sure whether it can be triggered based on the location distance change.

https://developer.android.com/topic/libraries/architecture/workmanager

I see that we can have periodic updates like every 15 min but wanted to know if we can configure based on location distance change itself.

https://medium.com/@prithvibhola08/location-all-the-time-with-workmanager-8f8b58ae4bbc

Example if I set setSmallestDisplacement(10m) for location request, the worker manager should be triggered every 10m distance change and send location to server.

https://developers.google.com/android/reference/com/google/android/gms/location/LocationRequest

Nikhil
  • 91
  • 1
  • 7
  • LocationRequests already work in the background, even on Android 8+. That's why the accept a `PendingIntent`. – ianhanniballake Mar 27 '19 at 15:43
  • So is it possible to get location updates without running a service? Can you provide me good example or documentation which can help me out?@ianhanniballake – Nikhil Mar 27 '19 at 16:06
  • Do not expect `setSmallestDisplacement` to be sensitive to 10m changes. For solid results work with 100m+. Also consider this: https://developers.google.com/awareness/android-api/fence-api-overview Last thing - you don't need a service, it's not a PC/Server. Services are kind of useless in Android nowdays. Any background tasks mechanism (jobscheduler, workmanager, etc...) will give all you need. – Arseny Levin Mar 28 '19 at 13:23
  • Hope it will be helpful to you : https://stackoverflow.com/a/56593800/1318946 – Pratik Butani Jun 14 '19 at 13:00

1 Answers1

1

For my app, I'll let the location services run with no schedule. But I'll specify the minimum distance changed in my location manager to call an update like this.

private void onStartListening() {  
   //init location manager.
   LocationManager mLm = (LocationManager) getSystemService(LOCATION_SERVICE);

   //binding listener to manager with network provider.    
   mLm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 10, mNetworkListener);
}

The third argument for my requestLocationUpdates() is the minimum distance between location updates in meters.

Also, the second one is the minimum time interval between location updates in milliseconds. If you want to specify time criteria for your location updates too.