Android Nougat中的Job Scheduler未运行 [英] Job scheduler in android nougat is not running
问题描述
我正在尝试使Job Scheduler每3秒运行一次,但在android noughat上不起作用。这是我的代码。
I am trying to make job scheduler run after every 3 seconds but it doesnt work on android noughat. Here is my code.
mJobScheduler = (JobScheduler) getSystemService( Context.JOB_SCHEDULER_SERVICE );
JobInfo.Builder builder = new JobInfo.Builder( 1,
new ComponentName( getPackageName(), JobSchedulerService.class.getName() ) );
builder.setPeriodic(3000);
builder.setRequiresDeviceIdle(false);
builder.setPersisted(true);
JobInfo j=builder.build();
mJobScheduler.schedule(j);
作业计划服务,其中包含要在作业上执行的代码。
Job Scheduling service that contains the code to be executed on jobs.
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 ) {
// Toast.makeText( getApplicationContext(), "kutta", Toast.LENGTH_SHORT ).show();
mJobHandler.sendMessage( Message.obtain( mJobHandler, 1, params ) );
return true;
}
@Override
public boolean onStopJob( JobParameters params ) {
mJobHandler.removeMessages( 1 );
return false;
}
}
推荐答案
可以安排作业的最短时间为15分钟。这始于牛轧糖。如果将其设置为小于15分钟的值,则作业将使用15分钟。
The minimum period a job can be scheduled is 15 minutes. This started with Nougat. If it is set to a value less than 15 minutes, the job will use 15 minutes.
getMinPeriodMillis()复制并添加;下面粘贴功能描述
getMinPeriodMillis() copy & paste of function description below
查询周期性计划作业的最小间隔。尝试声明一个较小的时间段,以便在安排作业时这将导致该作业仍然是周期性的,但将在此有效期内运行。
Query the minimum interval allowed for periodic scheduled jobs. Attempting to declare a smaller period that this when scheduling a job will result in a job that is still periodic, but will run with this effective period.
这篇关于Android Nougat中的Job Scheduler未运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!