在Nougat中关闭应用程序时,FirebaseJobDispatcher不调度服务

时间:2018-03-01 06:31:23

标签: android android-service android-7.0-nougat android-jobscheduler

一旦连接了互联网,无论应用程序处于前台还是后台或关闭,我都可以从SQLite数据库向服务器发送所有条目。并且所有条目必须顺序进入服务器。我使用firebase作业调度程序,如下所示:

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
        Job myJob = dispatcher.newJobBuilder()
                // the JobService that will be called
                .setService(OfflineJobService.class)
                // uniquely identifies the job
                .setTag("offline-job")
                // one-off job
                .setRecurring(false) //to call job after specific interval and check whether job is pending
                // don't persist past a device reboot
                .setLifetime(Lifetime.FOREVER)
                // start between 0 and 10 seconds from now
                .setTrigger(Trigger.executionWindow(0, 10))
                // overwrite an existing job with the same tag
                .setReplaceCurrent(false) //new job should not be created by replacing current job, instead it must continue with already running job
                // retry with exponential backoff
                .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) //without this the schedular will not execute when app is closed from background
                // constraints that need to be satisfied for the job to run
                .setConstraints(
                        // only run on an unmetered network
//                        Constraint.ON_UNMETERED_NETWORK
                        Constraint.ON_ANY_NETWORK
                )
                .build();
        dispatcher.mustSchedule(myJob);

每当连接互联网时,调用此作业,执行一次,然后调用onDestroy我调用onDestroy()时重新调度此作业,如下所示:

@Override
    public void onDestroy() {
        super.onDestroy();
        Log.d(TAG,"onDestroy called");
        FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
        dispatcher.cancel("offline-job");
        if(!isOrderCompleted) {
            Log.d(TAG,"Restarting service");
            Job myJob = dispatcher.newJobBuilder()
                    // the JobService that will be called
                    .setService(OfflineJobService.class)
                    // uniquely identifies the job
                    .setTag("offline-job")
                    // one-off job
                    .setRecurring(false) //to call job after specific interval and check whether job is pending
                    // don't persist past a device reboot
                    .setLifetime(Lifetime.FOREVER)
                    // start between 0 and 30 seconds from now
                    .setTrigger(Trigger.executionWindow(0, 30))
                    // overwrite an existing job with the same tag
                    .setReplaceCurrent(false) //new job should not be created by replacing current job, instead it must continue with already running job
                    // retry with exponential backoff
                    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL) //without this the schedular will not execute when app is closed from background
                    // constraints that need to be satisfied for the job to run
                    .setConstraints(
                            // only run on an unmetered network
//                        Constraint.ON_UNMETERED_NETWORK
                            Constraint.ON_ANY_NETWORK
                    )
                    .build();

            dispatcher.mustSchedule(myJob);
        }

    }

当应用程序处于前台或后台时,此功能完美正常。但是当app关闭时不起作用。 注意:当应用程序关闭直到棉花糖时,它可以正常工作。但牛轧糖及以上都没有。

0 个答案:

没有答案