我努力使每3秒后,作业调度运行,但它并不在Android noughat工作。 这里是我的代码。
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);
要在工作执行包含代码作业调度服务。
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;
}
}