作业计划设置如下
ComponentName mServiceComponent = new ComponentName(context, TestJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(jobId, mServiceComponent);
builder.setPeriodic(3 * 60 * 1000);
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE);
builder.setRequiresDeviceIdle(false);
builder.setRequiresCharging(false);
builder.setPersisted(true);
JobScheduler jobScheduler = (JobScheduler) ChaseForceApplication.getAppContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
jobScheduler.schedule(builder.build());
TestJobService类是这样的:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public class TestJobService extends JobService {
@Override
public boolean onStartJob(JobParameters params) {
Utility.writeToTheFile(ChaseForceApplication.getAppContext(), "\n\n Job Scheduler StartJob with jobid="+params.getJobId()+" set at " + new Date().toString());
sendBroadcast(new Intent(this, OnSingleAlarmReceiver.class));
return false;
}
@Override
public boolean onStopJob(JobParameters params) {
Log.i(ChaseForceApplication.TAG, "on stop job: " + params.getJobId());
Utility.writeToTheFile(this, "on stop job: " + new Date().toString());
return false;
}
}
它在大多数设备上工作,即使在其他小米手机中,但是在小米Redmi 3S中,它无法正常工作。
Job Schedular是否需要进行任何设置才能使其在该设备上运行?
答案 0 :(得分:1)
似乎小米MIUI操作系统不允许JobScheduler运行http://c.mi.com/thread-8779-1-1.html
答案 1 :(得分:0)
从您的应用程序设置/信息页面,尝试启用自动启动,然后使用作业计划程序重试。它会工作。您需要为您的应用启用自动启动。