Task Scheduler does not work within the specified interval

I am trying to use the android task scheduler API, and all I am trying to do is start the task scheduler every 5 seconds. However, when I start it, the corresponding service hits every two minutes. I have a journal that documents every time a service hits. I do not know why this is happening. Can the task scheduler have a minimum interval time. My code is just ...

JobInfo jobInfo = new JobInfo.Builder(1, new ComponentName(this, UpdateDatabaseService.class)) .setPeriodic(5000) .build(); JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); jobScheduler.schedule(jobInfo); 

The problem arose when I tried to perform a daily task, but it would call the service several times during this day and would not follow the guidelines of the time.

Let me know what you think.

+10
source share
6 answers

I had this problem, and after looking at some blogs and official documentation, I realized that JobScheduler has different behavior on Android N (24 and 25). JobScheduler runs with a minimum frequency of 15 minutes.

 @TargetApi(Build.VERSION_CODES.LOLLIPOP) @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public static void setJobScheduler(Context context){ Log.v(TAG, "Job Scheduler is starting"); JobScheduler jobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE); ComponentName serviceName = new ComponentName(context, JobService.class); JobInfo jobInfo; if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){ jobInfo = new JobInfo.Builder(JOB_ID, serviceName) .setPeriodic(900000) .build(); }else{ jobInfo = new JobInfo.Builder(JOB_ID, serviceName) .setPeriodic(Constants.TIME_INTERVAL) .build(); } jobScheduler.schedule(jobInfo); } 
+19
source

Instead of setPeriodic(int) use setMinimumLatency(int) with setOverrideDeadline(int) . These two methods will adjust the spacing of your JobScheduler .

+3
source

You must call jobFinished(jobParams,needResheduleBoolean); , after this call only the task will be executed, this means that the task knows that it is completed, so you can run it again.

+2
source

JobInfo.Builder builder = new JobInfo.Builder(1,new ComponentName(getPackageName(), JobSchedulerService.class.getName()));

builder.setPeriodic(3000);


Edited

MainActivity.java

 public class MainActivity extends Activity { private JobScheduler mJobScheduler; private Button mScheduleJobButton; private Button mCancelAllJobsButton; @Override protected void onCreate( Bundle savedInstanceState ) { super.onCreate( savedInstanceState ); setContentView( R.layout.activity_main ); mJobScheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE ); mScheduleJobButton = (Button) findViewById( R.id.schedule_job ); mCancelAllJobsButton = (Button) findViewById( R.id.cancel_all ); mScheduleJobButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { JobInfo.Builder builder = new JobInfo.Builder( 1, new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) ); builder.setPeriodic( 3000 ); if( mJobScheduler.schedule( builder.build() ) <= 0 ) { //If something goes wrong } } }); mCancelAllJobsButton.setOnClickListener( new View.OnClickListener() { @Override public void onClick( View v ) { mJobScheduler.cancelAll(); } }); } } 

just need to change

 new JobInfo.Builder(1, new ComponentName(this, UpdateDatabaseService.class)) 

to

 new JobInfo.Builder( 1, new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) ) 

builder.setPeriodic( 3000 ); install JobInfo on a schedule of 3000 ms and is called every 3 seconds.

JobSchedulerService.java

 public class JobSchedulerService extends JobService { private Handler mJobHandler = new Handler( new Handler.Callback() { @Override public boolean handleMessage( Message msg ) { Toast.makeText( getApplicationContext(), "JobService task running", Toast.LENGTH_SHORT ).show(); jobFinished( (JobParameters) msg.obj, false ); return true; } } ); @Override public boolean onStartJob(JobParameters params ) { mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) ); return true; } @Override public boolean onStopJob( JobParameters params ) { mJobHandler.removeMessages( 1 ); return false; } } 

AndroidManifest.xml

 <service android:name=".JobSchedulerService" android:permission="android.permission.BIND_JOB_SERVICE" /> 

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/schedule_job" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Schedule Job"/> <Button android:id="@+id/cancel_all" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel All"/> </LinearLayout> 
+2
source

From android documentation setPeriodic ,

setPeriodic (long intervalMillis): Specify that this task should be repeated at the specified interval, no more than once per period. You do not control when this task will be completed during this interval, but only guarantee that it will be completed no more than once during this interval.

therefore, it is clearly stated that the task is not required to be completed during this time. If you want to run a task at a certain periodic time, it is better to use AlarmManager Api with the setRepeating () method. Follow this link below for your requirements.

https://developer.android.com/training/scheduling/alarms.html#set

+2
source

You need at least 30 seconds to wait for the next job.

0
source

Source: https://habr.com/ru/post/984788/


All Articles