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

Cancel all pendingIntent which using alarm service in android

我是 android.I 的新手,已经像这样使用 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);

现在我想取消我在 code.I 上使用上面设置的所有通知 (PendingIntent) 已经在 google 上搜索过并且我知道我必须再次创建相同的意图但是那两件事是缺少身份证号码和时间。虽然我已经厌倦了这个但不幸的是没有工作。

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);
}

任何建议appreciated.Thank你。

要取消 Intent,您必须将 alarmManager.cancel() 的 Intent 传递给 等于 您传递给 alarmManager.set() 的 Intent。按照为 Intent.filterEquals():

指定的规则,意图必须相等

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.

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