I have 2 alarms in one alarm I'm starting job scheduler service that sends notifications every 30 minutes and at the second alarm I want to cancel this scheduled job service, also I'm sending notification on main thread I didn't make separate thread in job service, here is my code:
This is my jobinfo object and I'm executing this method when broadcast of start alarm fires off
public static void Scheduler(Context context){
float duration = NotificationUtills.NotificationCounter();
ComponentName componentName =
new ComponentName(context , ClsJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(0, componentName)
.setPeriodic(5000);
JobScheduler jobScheduler =
(JobScheduler) context.getSystemService (Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
}
And this is job service
public class ClsJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
//logic for building and sending notification
NotificationUtills.notificationBuilder(getApplicationContext());
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return false;
}
}
Is it alright not to create separate thread?
Also how can i cancel this scheduled job when second alarm fires off?
Is it alright not to create separate thread?
It is, as long as your Notification
building logic does not do anything time consuming that might hurt the responsiveness of the app.
Also how can i cancel this scheduled job when second alarm fires off?
You can call the cancel()
method of JobScheduler
.
It takes the job ID as a parameter. This has to be the same job ID you passed to the JobInfo.Builder
constructor (0
in your code).