我有两个AlarmManager计划每天23:59发射一次,另一个同时发射,但每周发射一次。
以下是执行此操作的两个函数:
file_data
以下是应作为上述待处理意图
的一部分被解雇的接收者private fun midnightTimeClearCache(dataType: DataType, requestCode: Int) {
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
val intent = Intent(context, ClearDataReceiver::class.java)
intent.putExtra(ClearDataReceiver.DATA_TYPE_EXTRA, dataType.name)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
}
private fun weekTimeClearCache(dataType: DataType, requestCode: Int) {
val calendar = Calendar.getInstance()
calendar.timeInMillis = System.currentTimeMillis()
calendar.set(Calendar.HOUR_OF_DAY, 23)
calendar.set(Calendar.MINUTE, 59)
var i: Int = calendar.get(Calendar.WEEK_OF_MONTH)
calendar.set(Calendar.WEEK_OF_MONTH, i++)
val intent = Intent(context, ClearDataReceiver::class.java)
intent.putExtra(ClearDataReceiver.DATA_TYPE_EXTRA, dataType.name)
val pendingIntent = PendingIntent.getBroadcast(context, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT)
alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.timeInMillis, pendingIntent)
}
这是我的清单:
启动接收器让我可以重置alerm管理器
class ClearDataReceiver : DaggerBroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
super.onReceive(context, intent)
var bundle = intent!!.extras
val dataTypeString = bundle.getString(DATA_TYPE_EXTRA)
val dataType = DataType.valueOf(dataTypeString)
Log.d("JJJ", "clearing data for " + dataTypeString)
when (dataType) {
DataType.CUSTOMER_DETAILS -> {
storage.clearDetails()
storageClearSchedular.setCustomerDetailsClear()
}
DataType.CUSTOMER_PIC -> {
storage.clearPic()
storageClearSchedular.setPicClear()
}
}
当我设置上述警报管理器时间表然后在我睡觉之前从后台终止应用程序时,我再次加载应用程序以发现它没有执行我的广播接收器(没有关于日志和数据的详细信息)清除)。
应该发生的是它应该在我设置的时间或每周一次执行每个数据,然后在它被触发后再次设置它但它似乎根本不起作用。
我首先尝试了setRepeating,但是它不能用作api 19及以上版本的im目标设备
我缺少什么?
答案 0 :(得分:1)
对于19以上的api版本,请使用此代码
if (Build.VERSION.SDK_INT < 19) {
alarmManager.set(AlarmManager.RTC,
calendar.getTimeInMillis(), pendingIntent);
}
} else if (Build.VERSION.SDK_INT >= 19 && Build.VERSION.SDK_INT <= 22) {
alarmManager.setExact(AlarmManager.RTC,
calendar.timeInMillis, pendingIntent)
} else if (Build.VERSION.SDK_INT >= 23) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC,
calendar.timeInMillis, pendingIntent)
}
对于每日警报,您可以检查状况。此示例将在每日上午9点设置警报。此外,您可能需要在设备重新启动或启动时再次设置警报,因为一旦设备关闭或重新启动,所有警报都会被取消。
Calendar calendar = Calendar.getInstance()
// set selected time from timepicker to calendar
calendar.setTimeInMillis(System.currentTimeMillis());
// if it's after or equal 9 am schedule for next day
if (Calendar.getInstance().get(Calendar.HOUR_OF_DAY) >= 9) {
Log.e("", "Alarm will schedule for next day!");
calendar.add(Calendar.DAY_OF_YEAR, 1); // add, not set!
}
else{
Log.e("", "Alarm will schedule for today!");
}
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);