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

时间:2015-09-14 08:47:40

标签: android notifications alarmmanager android-pendingintent

我是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)。我在google上搜索过,我得到了我必须再次创建相同的意图但是在那两件事情上缺少id no和time。虽然我已经厌倦了,但不幸的是没有用。

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

任何建议表示赞赏。谢谢。

0 个答案:

没有答案