当应用程序从后台终止后再次启动时,警报未决意图 returns null

Alarm Pending intent returns null when app started again after killing from background

我正在尝试检查重复警报是否已经存在。我正在 MIUI 11 (OS v8.1) 设备上进行测试,我在其中设置了警报,然后从后台删除了应用程序。如果我再次打开该应用程序,则会再次创建一个新闹钟

Here is my code to set a repeating alarm

   private fun startAlarm() {
    val CUSTOM_INTENT = "com.test.intent.action.ALARM"
    val intent = Intent(this, AlarmHelper::class.java)
    intent.action = CUSTOM_INTENT
    val pendingIntent: PendingIntent =
        getBroadcast(this, 101, intent, PendingIntent.FLAG_UPDATE_CURRENT)
    val alarmManager = getSystemService(ALARM_SERVICE) as AlarmManager

    alarmManager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),
        System.currentTimeMillis()+60*60*1000, pendingIntent)
}

This checks if an alarm already exist then i will not create a new one.

 private fun isAlarmExist() :Boolean =
        PendingIntent.getBroadcast(this, 101,
               Intent(this, AlarmHelper::class.java),
               PendingIntent.FLAG_NO_CREATE) != null

Already tried this

private fun isAlarmExist() :Boolean =
        PendingIntent.getBroadcast(this, 101,
               Intent("com.example.dozemodepoc.MY_UNIQUE_ACTION"),
               PendingIntent.FLAG_NO_CREATE) != null

// this didnot work either

已经通过 How to check if AlarmManager already has an alarm set?

强制停止应用程序并再次启动应用程序时,会再次创建新实例。任何形式的帮助将不胜感激!!

我在 isAlarmExist() 中犯了一个愚蠢的错误

  private fun isAlarmExist() :Boolean {

    val CUSTOM_INTENT = "com.test.intent.action.ALARM"
    val intent = Intent(this@MainActivity, AlarmHelper::class.java)
    intent.action = CUSTOM_INTENT

    return getBroadcast(this@MainActivity, 101, intent, PendingIntent.FLAG_NO_CREATE)!=null
}

此待定意图应与设置警报时创建的待定意图完全一致。