Firebase作业调度程序未调用oreo设备中的onLocation已更改

时间:2019-04-23 08:50:21

标签: android performance android-location android-jobscheduler firebase-job-dispatcher

以下代码在Nougat设备上运行正常,但在oreo设备上,它在定义的时间间隔后调用onStartJob(),但不执行onLocationChanged

Job Schedular的工作部分是在奥利奥设备中完成的,但是有些onLocationChanged从未被调用过。

创建工作分配器

用户授予位置权限后,我将执行以下代码。

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job job = createJob(dispatcher);
dispatcher.mustSchedule(job);

职位生成器

  Job job = dispatcher.newJobBuilder()
                .setLifetime(Lifetime.FOREVER)
                .setService(LocationJobService.class)
                .setTag("Location-Job-package")
                .setReplaceCurrent(false)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(200, 800))
                .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
                .setConstraints(Constraint.ON_ANY_NETWORK, Constraint.DEVICE_CHARGING)
                .build();
        return job;     

后勤服务类

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


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

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

            mGoogleApiClient.connect();
        }
    }

   @SuppressLint("MissingPermission")
    @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 void onLocationChanged(Location location) {

        String storedLat, storedLng;
        //....
    }

0 个答案:

没有答案