如何update/remove老报警?
How to update/remove old alarms?
我的应用程序中有一个功能,可以在从外部 API 获取数据时创建警报。当 API 数据刷新时,它会再次根据新数据创建警报。每次用户访问该特定视图时,都会从服务器中提取当前数据并创建警报。如何确保删除使用旧数据创建的警报?该功能是一种提醒服务。
如果您使用 PendingIntent.FLAG_UPDATE_CURRENT
或 PendingIntent.FLAG_CANCEL_CURRENT
创建其他警报,则可以使用最初为警报创建 PendingIntent 时使用的相同 requestCode
,这将取消首先报警并在 PendingIntent.FLAG_CANCEL_CURRENT
的情况下创建一个新的,或者在 PendingIntent.FLAG_UPDATE_CURRENT
的情况下它将用新数据更新现有数据
来自Docs
FLAG_CANCEL_CURRENT
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int).
This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
否则,如果未指定这些标志,您可以自己手动取消它们。您只需要请求代码
PendingIntent pendingIntent =
PendingIntent.getBroadcast(mContext, requestCode, alarmReceiverClassIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
if (am != null) {
am.cancel(pendingIntent);
}
我的应用程序中有一个功能,可以在从外部 API 获取数据时创建警报。当 API 数据刷新时,它会再次根据新数据创建警报。每次用户访问该特定视图时,都会从服务器中提取当前数据并创建警报。如何确保删除使用旧数据创建的警报?该功能是一种提醒服务。
如果您使用 PendingIntent.FLAG_UPDATE_CURRENT
或 PendingIntent.FLAG_CANCEL_CURRENT
创建其他警报,则可以使用最初为警报创建 PendingIntent 时使用的相同 requestCode
,这将取消首先报警并在 PendingIntent.FLAG_CANCEL_CURRENT
的情况下创建一个新的,或者在 PendingIntent.FLAG_UPDATE_CURRENT
来自Docs
FLAG_CANCEL_CURRENT
Flag indicating that if the described PendingIntent already exists, the current one should be canceled before generating a new one. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int). You can use this to retrieve a new PendingIntent when you are only changing the extra data in the Intent; by canceling the previous pending intent, this ensures that only entities given the new data will be able to launch it. If this assurance is not an issue, consider FLAG_UPDATE_CURRENT.
FLAG_UPDATE_CURRENT
Flag indicating that if the described PendingIntent already exists, then keep it but replace its extra data with what is in this new Intent. For use with getActivity(Context, int, Intent, int), getBroadcast(Context, int, Intent, int), and getService(Context, int, Intent, int). This can be used if you are creating intents where only the extras change, and don't care that any entities that received your previous PendingIntent will be able to launch it with your new extras even if they are not explicitly given to it.
否则,如果未指定这些标志,您可以自己手动取消它们。您只需要请求代码
PendingIntent pendingIntent =
PendingIntent.getBroadcast(mContext, requestCode, alarmReceiverClassIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) mContext.getSystemService(ALARM_SERVICE);
if (am != null) {
am.cancel(pendingIntent);
}