gpt4 book ai didi

java - Firebase JobDispatcher 在 1 小时后停止工作

转载 作者:行者123 更新时间:2023-12-02 10:09:19 27 4
gpt4 key购买 nike

在 API > 21 上,当我的手机锁定时,此服务将停止工作(当我唤醒手机时,服务将开始工作)。这就是我所做的:

public class JobDispacherService extends JobService {

private Preferences prefs = null;
public static final String GCM_ONEOFF_TAG = "oneoff|[0,0]";
public static final String GCM_REPEAT_TAG = "komunalRepeat|[7200,1800]";
private static final String TAG = JobDispacherService.class.getSimpleName();
private UplaudPossitionTask uplaudPossitionTask;

@Override
public boolean onStartJob(@NonNull JobParameters job) {

uplaudPossitionTask = new UplaudPossitionTask() {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
jobFinished(job, false);
}
};
uplaudPossitionTask.execute();
return false; // Answers the question: "Is there still work going on?"
}


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

private class UplaudPossitionTask extends AsyncTask<Void, Void, Void> {

@Override
protected Void doInBackground(Void... params) {

uploadPosition();
return null;
}
}

}

我这样称呼这个服务:

  FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));

Job myJob = dispatcher.newJobBuilder()
.setService(JobDispacherService.class)
.setTag("my-unique-tag")
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(10, (int) 15))
.setReplaceCurrent(false)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();

dispatcher.mustSchedule(myJob);

我想一直重复这个时间,但我不知道为什么当我的手机锁定超过 1 小时时此服务停止工作

工作管理器:

public static void refreshCouponPeriodicWork() {

//define constraints
Constraints myConstraints = new Constraints.Builder()
.setRequiresDeviceIdle(false)
.setRequiresCharging(false)
.setRequiredNetworkType(NetworkType.CONNECTED)
.setRequiresBatteryNotLow(true)
.setRequiresStorageNotLow(true)
.build();

Data source = new Data.Builder()
.putString("workType", "PeriodicTime")
.build();

PeriodicWorkRequest refreshCpnWork =
new PeriodicWorkRequest.Builder(RefreshLatestCouponWorker.class, 10, TimeUnit.HOURS,30, TimeUnit.SECONDS)
.setConstraints(myConstraints)
.setInputData(source)
.build();

WorkManager.getInstance().enqueue(refreshCpnWork);
}


public class RefreshLatestCouponWorker extends Worker {

private Preferences prefs = null;


public RefreshLatestCouponWorker(@NonNull Context context, @NonNull WorkerParameters workerParams) {
super(context, workerParams);
}

@NonNull
@Override
public Worker.Result doWork() {
//read input argument
String workType = getInputData().getString("workType");
Log.i("refresh cpn work", "type of work request: " + workType);

uploadPosition();
//sending work status to caller
return success();
}

}

我为工作经理执行此操作,但任务仅运行一次并且从不重复

最佳答案

我建议您阅读有关 WorkManager 的内容,这将是适合您的正确解决方案。 WorkManager API 可以轻松安排可延迟的异步任务,即使应用程序退出或设备重新启动,这些任务也将运行。您可以定义具有指定时间间隔和设备状态的定期请求。如果您发现实现困难,请恢复,我可以帮助您。

关于java - Firebase JobDispatcher 在 1 小时后停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55116299/

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