dev90
dev90

Reputation: 7529

Location service not getting called from Oreo

I was starting LocationService with startService() command, but since i learnt that Oreo will not entertain this service when app is in background, I switched over to JobSchedular,

I tested the app in lollipop and JobSchedular worked just fine, but in Oreo, it does not run LocationService

I put break point in onCreate() method of LocationService, and it just does not go there.

This is what I am doing.

MainActivity

It executes following code, but does not react to LocationUpdateService.class

    public void initLocationJob(){

        JobInfo jobInfo;
        JobScheduler jobScheduler;

        ComponentName componentName= new ComponentName(this, LocationUpdateService.class);
        JobInfo.Builder builder= new JobInfo.Builder(11, componentName);

        builder.setPeriodic(5000);
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
        builder.setPersisted(true);

        jobInfo= builder.build();
        jobScheduler= (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);


        jobScheduler.schedule(jobInfo);
}

LocationUpdateService

public class LocationUpdateService extends JobService implements
        LocationListener,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        IServiceResponse {



    @Override
    public void onCreate() {
        super.onCreate();

        if (isGooglePlayServicesAvailable()) {

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                    .addApi(LocationServices.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();

            mGoogleApiClient.connect();
        }
    }


    @Override
    public boolean onStartJob(JobParameters params) {

        Log.i(TAG, "onStartCommand: ");
        mLocationRequest = new LocationRequest();
        mLocationRequest.setInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setFastestInterval(BACKGROUND_INTERVAL);
        mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

        this.params= params;

        return true;
    }


    @Override
    public boolean onStopJob(JobParameters params) {
        Log.d("JobStopped", "JobStopped");
        return true;
    }

 @Override
    public void onLocationChanged(Location location) {
     //Get current Lat/lng and send it to server
  }

Upvotes: 1

Views: 255

Answers (1)

Sagar
Sagar

Reputation: 24907

This problem is with following code:

JobInfo.Builder builder= new JobInfo.Builder(11, componentName);
builder.setPeriodic(5000);

Starting from Android N, JobScheduler works with a minimum periodic of 15 mins. Frequency of 5 seconds is too frequent and is inappropriate.

Upvotes: 3

Related Questions