[英]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: 需要完成两个步骤:
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.
这是我找到的关于该主题的最佳信息。
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.