定期作业在触发Firebase JobScheduler后不久就会停止工作?

时间:2017-03-04 11:53:42

标签: android android-service android-backup-service periodic-task firebase-job-dispatcher

我使用firebase Job Dispatcher创建由可用网络触发的定期作业。问题是服务运行大约5分钟,有时甚至更少,然后完全停止。我尝试连接和断开网络,但结果是一样的。

根据我之前收集的内容,如果有可用的网络(wifi),以下代码应该触发周期性任务,由于后台服务的优化,时间段不准确,但它应该仍然是如果我经常连接,就会触发。但它并没有,我仍然连接到wifi几个小时并在电话上工作,但服务运行时间不超过5分钟。

这是我按照官方文档

实施代码的方式

我已尝试添加compile 'com.firebase:firebase-jobdispatcher:0.5.2'和。{ 在gradle文件中compile 'com.firebase:firebase-jobdispatcher-with-gcm-dep:0.5.2'(不是在同一时间)。

JobService

    import com.firebase.jobdispatcher.JobParameters;
    import com.firebase.jobdispatcher.JobService;

    public class MyJobService extends JobService {
        @Override
        public boolean onStartJob(JobParameters job) {
            // Do some work here
    Toast.makeText(this, "The service is triggered now!",
   Toast.LENGTH_LONG).show();
            return false; // Answers the question: "Is there still work going on?"
        }

        @Override
        public boolean onStopJob(JobParameters job) {
            return false; // Answers the question: "Should this job be retried?"
        }
    }

清单

<service
    android:exported="false"
    android:name=".MyJobService">
    <intent-filter>
        <action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
    </intent-filter>
</service>

我已将此添加到活动中

// Create a new dispatcher using the Google Play driver.
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

Bundle myExtrasBundle = new Bundle();
myExtrasBundle.putString("some_key", "some_value");

Job myJob = dispatcher.newJobBuilder()
    // the JobService that will be called
    .setService(MyJobService.class)
    // uniquely identifies the job
    .setTag("my-unique-tag")
    // this is a periodic job
    .setRecurring(true)
    // persist after device reboot
    .setLifetime(Lifetime.FOREVER)
    // start between 0 and 10 seconds from now
    .setTrigger(Trigger.executionWindow(0, 10))
    // don't overwrite an existing job with the same tag
    .setReplaceCurrent(false)
    // retry with exponential backoff
    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
    // constraints that need to be satisfied for the job to run
    .setConstraints(
        // only run on an unmetered network
        Constraint.ON_UNMETERED_NETWORK,
    )
    .setExtras(myExtrasBundle)
    .build();

dispatcher.mustSchedule(myJob);

我已经将此添加到清单以便继续工作:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

0 个答案:

没有答案