7

Since this the new Android 8 update came out there are many limitations imposed on how background services work.

So my app needs to fetch in background the user's location regularly and for that, I considered using FusedLocationProviderClient suggested by Android Website. After the location has been fetched I just need to some simple work with the info.

Right now I developed this sample of code:

private fun updateLocation(){
    //Location Request
    val mLocationRequest = LocationRequest();
    mLocationRequest.smallestDisplacement = 1000f;
    mLocationRequest.interval = 5*60*1000;
    mLocationRequest.priority = LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY;

    //Location Provider
    val mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this)

    //Check Location Permission
    if(PermissionChecker.checkSelfPermission(this,ACCESS_FINE_LOCATION)==PackageManager.PERMISSION_GRANTED)

    //Request Location
      mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest,locationCallback,null);
}

private val locationCallback = object:LocationCallback(){
    override fun onLocationResult(locationResult: LocationResult?) {
        doSomeWork()
    }
}

So I have a question:

Q. Should I use a Scheduled Job each time I want to update the location?

If I want my app to get updates even if the activity has been closed, it sounds like a Service's job right? But with this new updates, my service doesn't run forever so it will be killed sooner or later.

Using a regularly scheduled job, the service will be launched when the conditions are met but it seems kinda weird to schedule regular jobs to update the location instead of using a unique service that makes use of the Interval and SmallestDisplacement for new updates.

The requestLocationUpdates() invoke his callback every time the LocationRequest's conditions are met (interval and/or smallestDisplacement in this sample). That's what I'm trying to accomplish, I want a job that requests for location updates, not multiple jobs rescheduled as I need the updates that request for the location.

Is there any other better way to accomplish what I'm trying to say or I need this new approach of scheduled jobs?

Anyways, sorry for this messy post, it's my first question on Stack Overflow Thanks :)

ricardoatds
  • 73
  • 3
  • 5
  • 1
    I have a similar question: https://stackoverflow.com/questions/53573436/how-can-location-updates-from-fusedlocationproviderclient-be-processed-with-work – individual8 Dec 01 '18 at 17:47

2 Answers2

1

Use the Geofencing API for that. That gives you an intent that you can handle in an IntentService. No notification required.

All you need to do is to setup a new geofence with 1000m radius at the current position and wait for the user to leave.

leonardkraemer
  • 6,573
  • 1
  • 31
  • 54
  • I've read the documentation but from the user's point of view, getting notified that the app is fetching the location everytime the app requires isn't kinda disturbing? – ricardoatds Mar 10 '18 at 13:10
  • The documentation states that 'apps' can request location only a few times per hour, when in background. I quess that means you either have to be running (anything you can bind to) in foreground or bite the bullet of the notification. If you have a valid usecase users will appreciate that ;) – leonardkraemer Mar 10 '18 at 13:14
  • Thanks for warning me up, I completely forgot to make the counts on that sample. Guess I'll be using the foreground service for the Android O users ! – ricardoatds Mar 10 '18 at 13:22
  • seeing your changed question I changed the answer. – leonardkraemer Mar 10 '18 at 13:23
0

If you want your service run in small periods you should use Bound Service or Foreground Service. If you want some specific Job triggered depending on some condition or significant amount of time you should use JobScheduler.

Anton Kazakov
  • 2,740
  • 3
  • 23
  • 34
  • I want my service to run let's say everytime the user moves 1000meters, isn't disturbing to notify each time by using Foreground Service? Using JobScheduler I need to call requestLocationUpdates() every time I schedule the job. The method itself does call the LocationCallback everytime the user moves 1000meters but since the activity is killed it won't invoke the callback. It just seems a little incorrect to call a new service every time I want to update cause the requestLocationUpdates() method already "delivers" updates by it's callback. – ricardoatds Mar 10 '18 at 13:17
  • I don't thin JobScheduler can be applied here. Foreground service is the right one. Foreground service will just add notification that your service is running. – Anton Kazakov Mar 10 '18 at 13:19
  • Use the Geofencing API for that. https://developer.android.com/training/location/geofencing.html that gives you an intent that you can handle in an IntentService. No notification required. – leonardkraemer Mar 10 '18 at 13:19