AlarmManager setEexact () not working

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

?

+4
1

api 19

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 AM . , , , .

Calendar calendar = Calendar.getInstance()
        // set selected time from timepicker to calendar
        calendar.setTimeInMillis(System.currentTimeMillis());

        // if it 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);
+1

Source: https://habr.com/ru/post/1695196/


All Articles