關(guān)于使用 AlarmManager 的注意事項
AlarmManager 和 Jobscheduler
Android后臺任務(wù)開發(fā)的發(fā)展和實踐(二)
Android 進程币诒猓活系列:(三)利用定時器和系統(tǒng)廣播
JobService 7.0 定時任務(wù)不生效
JobService 總結(jié)
場景:周期性的啟動Service,執(zhí)行任務(wù)
4.4
AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent i = new Intent(this, MessageService.class);
PendingIntent pi = PendingIntent.getService(this, 0, i, 0);
//4.4以上夹囚,6.0以下
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
manager.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pi);
}
//6.0以上
else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
manager.setExactAndAllowWhileIdle(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pi);
}
//4.4以下
else {
// manager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), triggerAtTime, pi);
manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime(), pi);
}