jeudi 3 janvier 2019

How to test periodic JobScheduler which runs once in every 24 hours

I have a JobScheduler scheduled to run once every 24 hours. But how can I make sure it gets executed once every 24 hours ?. I tested with small interval and it seems to work but want to test with my real value i.e 24 hours (24*60*60*1000 ms). How can I do it - looks like JobScheduler is not affected by the device time because even if I change the device time nothing happens.

I am aware of below command. Which would forcefully run the JobScheduler.

adb shell cmd jobscheduler run -f <packagename> <Job Id>

But I would like to test it in a more real world scenario.

Below is my JobScheduler code.

  JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
    JobInfo.Builder builder = new JobInfo.Builder(Constants.JOB_ID, new ComponentName(context,
            MyJobService.class));
    builder.setPeriodic(Constants.JOB_SCHEDULER_INTERVAL);
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    builder.setPersisted(true);
    jobScheduler.schedule(builder.build());

My JobScheduler Service

public class MyJobService extends JobService {

private final String TAG = "JobService";

@Override
public boolean onStartJob(final JobParameters params) {
    LogUtil.i(TAG, "onStartJob");

    Server.fetchRemoteConfig(new JobStatus() {
        @Override
        public void success() {
            LogUtil.i(TAG, "onStartJob - success");
            jobFinished(params, false);
        }

        @Override
        public void failure() {
            LogUtil.i(TAG, "onStartJob - failure");
            jobFinished(params, true);
        }
    });

    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    LogUtil.i(TAG, "onStopJob");
    return true;
}

public interface JobStatus {
    void success();
    void failure();
}

}

Aucun commentaire:

Enregistrer un commentaire