I have two AlarmManagers that should run once a day at 23:59 and one that works simultaneously, but once a week.
Below are two functions that do this:
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)
}
The following is a receiver that should be started as part of the above pending intentions
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()
}
}
here is my manifest:
And a boot receiver so that I can reset the alerm manager
override fun onReceive(context: Context?, intent: Intent?) {
Log.d("JJJ", "onReceive")
storageClearSchedular = StorageClearSchedular(context!!, context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager)
storageClearSchedular.setAllSchedulars()
When I set the above schedules of the alert manager and then remove the application from the background before going to bed, I download the application again to find that it diddnt run my broadcast receiver (no details about the logs and data are cleared).
, , dat , , .
setRepeating, diddnt im targeting api 19
?