Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

job scheduler in android N with less then 15 minutes interval

Tags:

Part of my question, how I can set up a job with less then 15 minutes interval in "Nougat", was answerd by "blizzard" in his answer here:
Job Scheduler not running on Android N
He explained the problem and suggested to use the following workaround:

JobInfo jobInfo; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {   jobInfo = new JobInfo.Builder(JOB_ID, serviceName)       .setMinimumLatency(REFRESH_INTERVAL)       .setExtras(bundle).build(); } else {   jobInfo = new JobInfo.Builder(JOB_ID, serviceName)       .setPeriodic(REFRESH_INTERVAL)       .setExtras(bundle).build(); }     

However, using the suggested

  .setMinimumLatency(REFRESH_INTERVAL)     

just starts the job once;
but how do I get it periodic with a period of around 30 seconds on an android nougat device (not using handler or alarm manager)?

like image 660
tomseitz Avatar asked Sep 22 '16 14:09

tomseitz


People also ask

What is JobScheduler explain how it works?

A job scheduler is a computer application for controlling unattended background program execution of jobs. This is commonly called batch scheduling, as execution of non-interactive jobs is often called batch processing, though traditional job and batch are distinguished and contrasted; see that page for details.

What is the use of JobScheduler in Android?

JobScheduler is introduced while the Android set limitation on background Execution. It means that the system will automatically destroy the background execution to save battery and memory. So if you want to perform some background job like some network operations no matter your app is running or not.

Which API is used to schedule Android jobs?

Modern Android applications should use the JobScheduler API. Apps can schedule jobs while letting the system optimize based on memory, power, and connectivity conditions.

Which of the service attribute need to set while scheduling a job in Android app using job scheduler?

Note: The JobService component needs to be enabled in order to successfully schedule a job. JobInfo : The job you wish to enqueue work for.


1 Answers

If someone is still trying to overcome the situation,

Here is a workaround for >= Android N (If you want to set the periodic job lower than 15 minutes)

Check that only setMinimumLatency is used. Also, If you are running a task that takes a long time, the next job will be scheduled at, Current JOB Finish time + PROVIDED_TIME_INTERVAL

.SetPeriodic(long millis) works well for API Level below Android N

@Override public boolean onStartJob(final JobParameters jobParameters) {     Log.d(TAG,"Running service now..");     //Small or Long Running task with callback      //Reschedule the Service before calling job finished     if(android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)               scheduleRefresh();      //Call Job Finished     jobFinished(jobParameters, false );      return true; }  @Override public boolean onStopJob(JobParameters jobParameters) {     return false; }  private void scheduleRefresh() {   JobScheduler mJobScheduler = (JobScheduler)getApplicationContext()                     .getSystemService(JOB_SCHEDULER_SERVICE);   JobInfo.Builder mJobBuilder =    new JobInfo.Builder(YOUR_JOB_ID,                     new ComponentName(getPackageName(),                      GetSessionService.class.getName()));    /* For Android N and Upper Versions */   if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {       mJobBuilder                 .setMinimumLatency(60*1000) //YOUR_TIME_INTERVAL                 .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);   } 

UPDATE: If you are considering your repeating job to run while in Doze Mode and thinking about JobScheduler, FYI: JobSchedulers are not allowed to run in Doze mode.

I have not discussed about the Dozing because we were talking about JobScheduler. Thanks, @Elletlar, for pointing out that some may think that it will run even when the app is in doze mode which is not the case.

For doze mode, AlarmManager still gives the best solution. You can use setExactAndAllowWhileIdle() if you want to run your periodic job at exact time period or use setAndAllowWhileIdle() if you're flexible.

You can also user setAlarmClock() as device always comes out from doze mode for alarm clock and returns to doze mode again. Another way is to use FCM.

Reference: Doze Restrictions

https://developer.android.com/training/monitoring-device-state/doze-standby

like image 135
MRah Avatar answered Oct 05 '22 21:10

MRah