1

I have been using the android-job Evernote library. I have job which is running continuously every 30 minutes. I have been using code to schedule the job

new JobRequest.Builder(TrackJob.TAG)
                .setPeriodic(TimeUnit.MINUTES.toMillis(30), TimeUnit.MINUTES.toMillis(5))
                .setUpdateCurrent(true)
                .setPersisted(true)
                .build()
                .schedule();

Now I want to stop this Job when a user clicks on a button. How I will achieve this? Thanks in advance.

Maheshwar Ligade
  • 6,709
  • 4
  • 42
  • 59

2 Answers2

0

You can do it as follows:

JobManager.instance().cancelAllForTag(your-job-tag);

or

JobManager.instance().cancel(your-job-id)

Michel Fortes
  • 819
  • 8
  • 12
0

Getting the Job ID:

int jobId = new JobRequest.Builder(DemoSyncJob.TAG)
            .setExecutionWindow(30_000L, 40_000L)
            .setBackoffCriteria(5_000L, JobRequest.BackoffPolicy.EXPONENTIAL)
            .setRequiresCharging(true)
            .setRequiresDeviceIdle(false)
            .setRequiredNetworkType(JobRequest.NetworkType.CONNECTED)
            .setExtras(extras)
            .setRequirementsEnforced(true)
            .setUpdateCurrent(true)
            .build()
            .schedule();  

Canceling By Button:

Button _button = new Button(this) ;

_button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
      JobManager.instance().cancel(jobId);
    }
});

Note:

You can use SharedPreference to store the jobId for later use. I mean if you want to cancel the job after application re-starts.

According to the Wiki, the job can also be canceled with the Job.TAG.I tried it but wasn't able to cancel it. (V1.2.5)

exploitr
  • 843
  • 1
  • 14
  • 27