少于15分钟间隔的android N中的Job Scheduler [英] job scheduler in android N with less then 15 minutes interval

查看:105
本文介绍了少于15分钟间隔的android N中的Job Scheduler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

暴雪"在他的回答中回答了我的部分问题,即如何在牛轧糖"中以不到15分钟的间隔设置工作:
作业调度程序未在Android N上运行
他解释了该问题,并建议使用以下解决方法:

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();
}    

但是,使用建议的

  .setMinimumLatency(REFRESH_INTERVAL)    

只开始一次工作;
但是如何在android牛轧糖设备上(不使用处理程序或警报管理器)在30秒左右的时间内定期获取它?

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)?

推荐答案

如果有人仍在努力克服这种情况,

If someone is still trying to overcome the situation,

这是> = Android N的解决方法(如果您想将定期作业设置为少于15分钟)

检查是否仅使用setMinimumLatency.另外,如果您正在运行耗时较长的任务,则下一个作业将安排为当前作业完成时间+ PROVIDED_TIME_INTERVAL

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)对于Android N以下的API级别效果很好

.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);
  }

更新: 如果您考虑将重复作业在中运行,并考虑JobScheduler,请仅供参考:JobSchedulers不允许在打ze模式下运行.

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.

由于我们在谈论JobScheduler,所以我没有讨论打Do睡.感谢 @Elletlar ,指出某些人可能会认为即使应用处于休眠模式也可以运行,事实并非如此.

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.

对于打ze模式,AlarmManager仍然提供最佳解决方案.如果要在准确的时间段运行定期作业或使用,可以使用 setExactAndAllowWhileIdle() > setAndAllowWhileIdle()(如果您很灵活).

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.

您也可以使用 setAlarmClock(),因为设备始终从闹钟的do睡模式中退出,并再次返回到ze睡模式.另一种方法是使用FCM.

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.

参考:打ze限制

https://developer.android.com/training/monitoring-设备状态/待命待机状态

这篇关于少于15分钟间隔的android N中的Job Scheduler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆