2

i want to create something like a reminder app which notify user at given times , i want to use job scheduler api to achieve this let's say i want to run the service at 9 am and 12 am what should be added in the following code to achieve this.

public void startJobService(){

    GooglePlayDriver driver = new GooglePlayDriver(this);
    FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(driver);

    Bundle myExtrasBundle = new Bundle();
    myExtrasBundle.putString("some_key", "some_value");

    Job myJob = dispatcher.newJobBuilder()
            .setService(jobservice.class)
            .setTag("unique-tag")
             .setExtras(myExtrasBundle)
            .build();

    dispatcher.mustSchedule(myJob);

}

//this is the JobService class

public class jobservice extends JobService{

private static final String TAG = "jobservice";

@Override
public boolean onStartJob(JobParameters job) {

    Log.d(TAG, "onStartJob: "+job.getExtras());

    return true;
}

@Override
public boolean onStopJob(JobParameters job) {
    return false;
}
}
Priyamal
  • 2,919
  • 2
  • 25
  • 52
  • 3
    You don't really control when the job runs. That's the point behind `JobScheduler`: give Android the freedom to slide the job forwards or backwards in time to try to minimize power use. With standard `JobScheduler`, you are welcome to use `setMinimumLatency()` and `setOverrideDeadline()` on the `JobInfo.Builder` to provide general guidance, but do not assume that your job will run at a specific wall-clock time. – CommonsWare May 06 '17 at 15:30
  • what apis should i use if i want to run a service at given time – Priyamal May 06 '17 at 15:41
  • Generally, you can't run something at a given time. The closest you will get is with `AlarmManager`. If you are writing an alarm clock app, use `setAlarmClock()`. Otherwise, use `setExactAndAllowWhileIdle()` (or `setExact()`, or `set()`, depending upon API level). However, those latter three will still be time-shifted if the device is in Doze mode on Android 6.0+, though perhaps only by 10 minutes or so. – CommonsWare May 06 '17 at 15:49
  • i found out about alarm manager but every doc said it is deprecated so i thought job scheduler might do this. thanks man. found my answer – Priyamal May 06 '17 at 16:02
  • 2
    "but every doc said it is deprecated" -- it is not deprecated. Certain methods are deprecated, though they are necessary on older devices as the newer replacement methods do not exist. Any "doc" that claims that `AlarmManager` is deprecated entirely is very incorrect. – CommonsWare May 06 '17 at 16:54

1 Answers1

1

Unfortunately it seems that the JobService does not provide this api. Two things you can do:

  • Use Alarm Manager to trigger the job service when you want or
  • Run you job service frequent (let's say once per hour). Then check if the current time is on your desired interval. If yes proceed, if no abort the job.

For example:

final Job job = dispatcher.newJobBuilder()
            .setService(MyService.class)
            .setTag(MyService.class.getName())
            .setRecurring(true)
            .setLifetime(Lifetime.FOREVER)
            .setTrigger(Trigger.executionWindow(HOUR, 2 * HOUR))
            .setReplaceCurrent(false)
            .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
            .build();

And then inside your Job Service:

@Override
public boolean onStartJob(JobParameters job) {
    if (!nowIsInDesiredInterval()) {
        Timber.i("Now is not inside the desired interval. Aborting.");
        return false;
    }
    // else execute your job
    // .....
    return false;
}
mspapant
  • 1,860
  • 1
  • 22
  • 31