1

I'm trying to create a recurring async task to call a web service with JobScheduler and JobService. But my idea was to try and execute this task once a day, and always at the same hour. Maybe even letting the user update the hour of the date the task is fired up.

Does the JobInfo.Builder supports this posibility? Because I was using "setPeriodic", but I don't think this is posible.

Should I use better the AlarmManager for this functionality?

This is the code that invokes the service right now:

private void launchCheckShowStatusJob(){
        ComponentName serviceComponent = new ComponentName(this, CheckShowsStatusService.class);
        JobInfo.Builder builder = new JobInfo.Builder(jobId++, serviceComponent);
        builder.setMinimumLatency(0);
        builder.setPeriodic(5*1000); //Every 5 minutes, can this be use to set a dayly task?
        builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY); //Require any network
        builder.setRequiresCharging(false);

        JobScheduler jobScheduler = (JobScheduler) getApplication().getSystemService(Context.JOB_SCHEDULER_SERVICE);
        jobScheduler.schedule(builder.build());
    }

1 Answers1

1

you can check the device time when your job scheduler wakes up and then inside of the OnStartJob get the time like that first

public override bool OnStartJob (JobParameters args)
    { if (devicetime == (that unique time)){ ... } }

and then you can do whatever you want.

However, android 7.0 not guarantee that jobscheduler will work at the exact time. so pick your unique time wider.

Second option is Alarm Manager which works fine for your case too. There is useful solution; Using Alarmmanager to start a service at specific time

Yigit Alp Ciray
  • 163
  • 1
  • 7