android – AlarmManager setEexact()无效

我有两个AlarmManager计划每天23:59发射一次,另一个同时发射,但每周发射一次.

以下是执行此操作的两个函数:

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)
    }

以下是作为上述待处理意图的一部分应该被解雇的接收者

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()
            }
}

这是我的清单:

        
            
                
            
        

和启动接收器,所以我可以重置alerm管理器

override fun onReceive(context: Context?, intent: Intent?) {
        Log.d("JJJ", "onReceive")

        storageClearSchedular = StorageClearSchedular(context!!, context!!.getSystemService(Context.ALARM_SERVICE) as AlarmManager)
        storageClearSchedular.setAllSchedulars()

当我设置上述警报管理器时间表然后在我睡觉之前从后台杀死应用程序时,我再次加载应用程序以发现它没有执行我的广播接收器(没有关于日志和数据的详细信息未清除).

应该发生的是它应该在我设置的时间或每周一次执行每个数据,然后在它被触发后再次设置它但它似乎根本不起作用.

我首先尝试了setRepeating,但是它确实起到了api 19及以上版本的im定位设备的作用

我错过了什么?

最佳答案 对于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);
点赞