LoginSignup
5
4

More than 5 years have passed since last update.

Android7.0以上のバージョンではPeriodicなJobのintervalを15分以下にすることができない

Last updated at Posted at 2018-01-07

タイトル通りです.

サンプル

例えば,

MainActivity.java

public class MainActivity extends AppCompatActivity {
    private ComponentName mServiceName;
    private final static int JOB_ID = 0x01;

    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistableBundle) {
        super.onCreate(savedInstanceState, persistableBundle);
        setContentView(R.layout.activity_main);

        mServiceName = new ComponentName(this, MyJobService.class);
        Intent intent = new Intent(this, MyobService.class);
        startService(intent);

        JobScheduler scheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE);
        JobInfo jobInfo = new JobInfo.Builder(JOB_ID, mServiceName)
                .setPeriodic(10000)
                .build();
        scheduler.schedule(jobInfo);
    }

という感じでJobInfo.BuildersetPeriodic関数を使って,10秒後にJobが実行されるように設定すると,Logcatに

W/JobInfo: Specified interval for 1 is +10s0ms. Clamped to +15m0s0ms
W/JobInfo: Specified flex for 1 is +10s0ms. Clamped to +5m0s0ms

と出てきて,勝手にintervalを15分・flexを5分にされてしまう.

# が,手元で試したら秒の単位でちょうど10分後に発火しました.
# まぁinterval ± flexなんでしょう.

感想

酷いのは,公式のリファレンスに何も書いてくれていないということですな..

今回に限っては本番では15分以上にしてもいいんやけど,いかんせんデバッグがしにくいというか,ちょっと動くか試したいときに面倒すぎる.
下記参考の2では,毎回Jobが終わるたびに同じJobIDでsetMinimumLatencyのJobを作りなおすことで,15分以下のPeriodicなJobを無理やり作ってるけど感覚的にはちょっと気持ち悪いしなぁ..

参考

  1. https://stackoverflow.com/questions/38344220/job-scheduler-not-running-on-android-n
  2. https://stackoverflow.com/questions/39641278/job-scheduler-in-android-n-with-less-then-15-minutes-interval
5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4