java Android jobScheduler 不会因 jobFinished(params, false) 而停止
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37488660/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android jobScheduler won't stop with jobFinished(params, false)
提问by Daniel Braun
I'm tring to create a jobService. Here is what onStartJob() looks like.
我正在尝试创建一个 jobService。这是 onStartJob() 的样子。
@Override
public boolean onStartJob(JobParameters params) {
Log.d(TAG, "onStartJob");
Log.d(TAG, "Params= " + params.getJobId());
param = params;
jobFinished(params, false);
//startAsync();
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.d(TAG, "onStopJob");
return false;
}
Here is the code that is supposed to start the job.
这是应该开始工作的代码。
public void startJobScheduler(){
Log.d(TAG, "inside startJobScheduler");
Activity activity = this.cordova.getActivity();
Context context = activity.getApplicationContext();
mJobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE );
JobInfo.Builder job = new JobInfo.Builder(111, new ComponentName(context, JobSchedulerService.class));
job.setPeriodic(60000);
Log.d(TAG, "before mJobScheduler.schedule(job.build())");
if( mJobScheduler.schedule( job.build() ) <= 0 ) {
Log.d(TAG, "job schedule failed");
}
Log.d(TAG, "333");
}
I can not get it to stop. It just keeps firing every 1-5 mins. I put jobFinished(params, false) in onStartJob() and commented out the task to try to kill it off right after it starts, but it just keeps firing. It seems jobFinished() fires something, as onDestroy() is called and my service gets destroyed, but then another job comes in with the same ID and starts it all back up.
我不能让它停下来。它只是每 1-5 分钟持续发射一次。我将 jobFinished(params, false) 放在 onStartJob() 中并注释掉任务,试图在它开始后立即将其杀死,但它一直在发射。似乎 jobFinished() 触发了一些东西,因为 onDestroy() 被调用并且我的服务被破坏,但是另一个具有相同 ID 的作业进入并启动它全部备份。
I have BIND_JOB_SERVICE in the manifest like every example shows.
就像每个示例所示,我在清单中有 BIND_JOB_SERVICE 。
Any ideas on why jobFinished(params, false) doesn't seem to kill the setPeriodic(60000)?
关于为什么 jobFinished(params, false) 似乎没有杀死 setPeriodic(60000) 的任何想法?
回答by Tom
You have specified that a job is run periodically (every 60 seconds), so every 60~ seconds a new job is created and executed. jobFinished()
is specific to a job, and simply indicates that it is done executing. You haven't cancelled anything.
您已指定作业定期运行(每 60 秒),因此每 60 秒创建并执行一个新作业。 jobFinished()
特定于作业,仅表示它已完成执行。你没有取消任何东西。
Your (currently) accepted answer works for cancelling your scheduled job, but if all you want is a job that executes within 60 seconds and then stops, you should ommit setPeriodic()
and use setOverrideDeadline(60000)
instead. The job will run within 60 seconds and no more will be scheduled after it.
您(当前)接受的答案适用于取消您的预定作业,但如果您想要的只是一个在 60 秒内执行然后停止的作业,您应该忽略setPeriodic()
并使用setOverrideDeadline(60000)
。作业将在 60 秒内运行,之后不会再安排其他作业。
回答by Daniel Braun
Well I figured it out if anyone else has this problem.
好吧,我想知道是否还有其他人有这个问题。
jobFinished() won't stop the periodic time you set from continuing. It just tells the job that you are finished, to release the wakelock, so Android doesn't have to kill the job off forcefully.
jobFinished() 不会阻止您设置的定期时间继续。它只是告诉工作你已经完成,释放唤醒锁,所以Android不必强行终止工作。
What I had to do was recreate the jobScheduler in my service and call cancelAll(). You could also apparently call cancel(job_id).
我必须做的是在我的服务中重新创建 jobScheduler 并调用 cancelAll()。您显然也可以调用cancel(job_id)。
jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE );
jobScheduler.cancelAll();
回答by HAXM
I was using multiple JobService, and to stop one specific Job, I used the id of job to cancel the job.
我使用了多个 JobService,为了停止一个特定的 Job,我使用了 Job 的 id 来取消该 Job。
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static void stopMyJob(Context context, int jobId) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
scheduler.cancel(jobId);
}
First I checked if the current job is active by or not, using the following function
首先,我使用以下函数检查当前作业是否处于活动状态
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public static boolean isJobActive(Context context, int jobId) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
boolean hasBeenScheduled = false;
for (JobInfo jobInfo : scheduler.getAllPendingJobs()) {
if (jobInfo.getId() == jobId) {
hasBeenScheduled = true;
break;
}
}
return hasBeenScheduled;
}
While starting the job, I have the id passed to jobInfo as:
在开始工作时,我将 id 传递给 jobInfo 为:
JobInfo jobInfo = new JobInfo.Builder(jobId, componentName);
The same jobid
I used to stop the job by calling my function stopMyJob(context, jobID)
.
同样的jobid
我曾经通过调用我的函数停止作业stopMyJob(context, jobID)
。
In most of cases, it's important to stop the current job and start the job again from beginning, if it's already running, for thais I used my both functions mentioned above as:
在大多数情况下,重要的是停止当前的工作并从头开始重新开始工作,如果它已经在运行,因为我使用了上面提到的两个功能:
if (isJobActive(activity, ANNOUNCEMENT_JOB_ID)) {
Log.d(TAG, "Job will cancel as already exist");
stopMessagingJob(activity, ANNOUNCEMENT_JOB_ID);
}
startMyJob(activity, ANNOUNCEMENT_JOB_ID);
My startMyJob(context, jobId)
looks like:
我的startMyJob(context, jobId)
样子:
private static void startMyJob(final Context context, final int jobId) {
ComponentName componentName = new ComponentName(context, MyJobService.class);
JobInfo jobInfo = new JobInfo.Builder(jobId, componentName)
.setPersisted(true).setPeriodic(TWENTY_MINUTES).build();
JobScheduler scheduler = (JobScheduler) context.getSystemService(JOB_SCHEDULER_SERVICE);
int resultCode = scheduler.schedule(jobInfo);
if (JobScheduler.RESULT_SUCCESS != resultCode) {
Log.d(TAG, "Job failed to start");
}
}