gpt4 book ai didi

android - 取消所有在android中使用报警服务的pendingIntent

转载 作者:行者123 更新时间:2023-11-29 00:07:49 25 4
gpt4 key购买 nike

我是 android 的新手。我已经像这样使用 PendingIntent 设置了通知

        Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
myIntent.putExtra("Msg", "Title");
myIntent.putExtra("Msgdetail", "Description of notification");
myIntent.putExtra("username","name");

//for uniquely broadcast request

Random random = new Random();
int m = random.nextInt(9999 - 1000) + 1000;
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this, m, myIntent, 0);

AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, c.getTimeInMillis(),pendingIntent);

现在我想取消我使用上面的代码设置的所有通知 (PendingIntent)。我在谷歌上搜索过,我知道我必须再次创建相同的 Intent ,但是有两件事缺少 ID 号和时间。虽然我已经厌倦了这个但不幸的是没有工作。

private void removenotifications() {
Intent myIntent = new Intent(MainActivity.this, MyReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(MainActivity.this,PendingIntent.FLAG_ONE_SHOT, myIntent, Intent.FILL_IN_DATA);
AlarmManager alarmManager = (AlarmManager) MainActivity.this.getSystemService(Context.ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
}

感谢任何建议。谢谢。

最佳答案

要取消一个 Intent,您必须将一个 Intent 传递给 alarmManager.cancel(),该 Intent 等于您传递给 alarmManager.set() 的 Intent 。按照为 Intent.filterEquals() 指定的规则, Intent 必须相同:

Determine if two intents are the same for the purposes of intent resolution (filtering). That is, if their action, data, type, class, and categories are the same. This does not compare any extra data included in the intents.

因此,在您的情况下,问题在于您在设置警报时使用的随机值。您将不得不以某种方式存储这些随机值,并使用它们来创建相同的取消 Intent 。

关于android - 取消所有在android中使用报警服务的pendingIntent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32560614/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com