简体   繁体   English

当用户从最近的应用程序列表中删除应用程序时,如何取消警报管理器警报?

[英]How to cancel alarm manager alarms when user removes the app from recent apps list?

As per a comment in this question : "Standard Android only allows you to remove things from that list on Android 3.0+, and doing so does not affect your AlarmManager events". 按照在评论这个问题:“标准的Android只允许你从名单上的Android 3.0+删除的东西,这样做不会影响您的AlarmManager事件”。

I would like to cancel the alarms if the user swipes the app from list of recent apps, but there seems to be no way we can handle the swiping of app from recent apps list where the alarm could be canceled. 如果用户从最近的应用程序列表中滑动应用程序,我想取消警报,但是似乎无法处理可以从中取消警报的最近的应用程序列表中滑动应用程序的方法。 I have option in the app for the user to cancel the alarm which executes this code : 我在应用程序中有选项供用户取消执行此代码的警报:

public static void cancelAlarm() {
        if (alarmUp())
        {
            alarmMgr.cancel(pendingIntent);
        }
  }
  public static boolean alarmUp() {
      return (pendingIntent != null);
  }

But how to cancel the alarm when the app removed forcibly ? 但是,当应用强制删除时,如何取消警报? I do not want to cancel the alarm when app is closed (by pressing back or home button). 我不想在应用关闭时(通过按“后退”或“主页”按钮)取消警报。

Two steps needed to accomplish this: 需要完成两个步骤:

  1. Detect the app removal from the recents list. 从“最近”列表中检测到该应用已删除。 See What Exactly Happens When You Swipe An Android App From the Recent Apps List? 查看从最近的应用程序列表中滑动Android应用程序时究竟会发生什么? . That's the best info I found on the subject. 这是我找到的关于该主题的最佳信息。

  2. Cancel the alarm. 取消警报。 Your question assumes that the pendingIntent variable still holds an intent constructed earlier. 您的问题假设, pendingIntent变量仍然包含先前构造的intent。 This won't work in an activity that was unloaded. 这在已卸载的活动中不起作用。 Instead, construct an intent that's "equal to" the original intent ("their action, data, type, class, and categories are the same"), and cancel that one. 而是构造一个与原始意图“相等”的意图(“其动作,数据,类型,类和类别相同”),然后取消该意图。

这可以通过在Activity OnDestroy()中调用alarm.cancel()轻松实现。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Android 中的最近应用程序列表中删除应用程序是否会导致它禁用警报管理器在 Android 应用程序中设置的任何警报? - Does removing an app from the recent apps list in Android cause it to disable any alarms that have been set by the alarm manager in an Android app? 当应用程序从android中的最新应用程序列表中删除时,服务停止 - Service stopped when the app removes from recent apps list in android 活动会从最近的应用列表中删除该应用 - An Activity removes the app from recent apps list 无法取消Android Alarm Manager的警报 - Can't cancel alarms for Android Alarm Manager 当我从最近的应用程序中删除应用程序时,为什么没有触发警报? - Why Alarm not fired when I remove app from recent apps? 即使在用户从最近的应用程序中删除应用程序后,如何安排和发送通知? - How To schedule and a send Notification even after user removes app from recent apps? Android,从“最近的应用”中删除应用时取消通知 - Android, cancel notification when app is removed from “Recent apps” Android:当用户从最近的任务列表中删除应用程序时,停止工作小工具 - Android : Stopping works Widget when user removes app from recent tasks list 从KITKAT的最新应用列表中清除后,Android警报将被取消 - Android alarm is cancelled when cleared from recent app list in KITKAT Android 警报管理器不工作,而应用程序被最近的应用程序杀死 - Android Alarm manager not working while appliction is killed from recent apps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM