gpt4 book ai didi

android - 为什么 Firebase JobDispatcher 运行 JobService 2 次?

转载 作者:塔克拉玛干 更新时间:2023-11-01 22:09:43 24 4
gpt4 key购买 nike

来自 Firebase JobDispatcher 文档 Firebase JobDispatcher

setTrigger(Trigger.executionWindow(0, 60))
// start between 0 and 60 seconds

但为什么我的服务运行了两次

Firebase JobDispatcher 代码

    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job job = dispatcher.newJobBuilder()
.setTag("testing job")
.setService(TestingJob.class)
.setReplaceCurrent(true)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(0,1))
.setConstraints(Constraint.ON_ANY_NETWORK)
.setLifetime(Lifetime.FOREVER)
.build();

dispatcher.mustSchedule(job);

测试类(Job Service)

public class TestingJob extends JobService {

private static final String TAG = "TestingJob";
private int i =0;
@Override
public boolean onStartJob(JobParameters job) {

Log.d(TAG, "onStartJob Testing Job: "+new Date().toString());
Log.d(TAG, "onStartJob: i = "+String.valueOf(i));
i+=1;
return false;
}

@Override
public boolean onStopJob(JobParameters job) {

Log.d(TAG, "onStopJob Testing Job: Stopped");
return false;
}
}

记录猫

11-28 00:08:57.666 11793-11793: onStartJob Testing Job: Tue Nov 28 00:08:57 GMT+05:00 2017
11-28 00:08:57.666 11793-11793: onStartJob: i = 0
11-28 00:08:57.791 11793-11793: onStartJob Testing Job: Tue Nov 28 00:08:57 GMT+05:00 2017
11-28 00:08:57.791 11793-11793: onStartJob: i = 0

list

   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<service android:name=".services.TestingJob"
android:exported="false">
<intent-filter>
<action android:name="com.firebase.jobdispatcher.ACTION_EXECUTE"/>
</intent-filter>
</service>

Can i use my Job Service Again 意味着 int i 应该每次递增。

感谢您的帮助

最佳答案

我认为我们应该知道一些方法

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job job = dispatcher.newJobBuilder()
.setTag("testing job")
.setService(TestingJob.class)
.setReplaceCurrent(true)
.setRecurring(true)
.setTrigger(Trigger.executionWindow(0,1))
.setConstraints(Constraint.ON_ANY_NETWORK)
.setLifetime(Lifetime.FOREVER)
.build();

dispatcher.mustSchedule(job);

在这你设置
.setRecurring(true) :这意味着不断重复。用开始和结束设置触发器.setTrigger(Trigger.executionWindow(开始, 结束)) :

.setReplaceCurrent(false) :这意味着不要覆盖具有相同标签的现有作业。

关于android - 为什么 Firebase JobDispatcher 运行 JobService 2 次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47518439/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com