2012-11-30 7 views
16

通知バーでいくつかの通知を送信しましたが、通知の1つがクリックされたときに通知をすべて消してしまいました。今のところ、私はFlagを使ってひとつずつクリアします。私はnotificationManager.cancelAll()がすべての通知をクリアできることを知っていますが、通知の1つがクリックされると私はトリガーできるようにどこに置くべきですか?一度クリックするとすべての通知を正しくクリアする方法はありますか?

private static void generateNotification(Context context, String message) { 
    int icon = R.drawable.ic_launcher; 
    long when = System.currentTimeMillis(); 
    NotificationManager notificationManager = (NotificationManager) 
      context.getSystemService(Context.NOTIFICATION_SERVICE); 
    Notification notification = new Notification(icon, message, when); 
    String title = context.getString(R.string.app_name); 
    Intent notificationIntent = new Intent(context, MainActivity.class); 

    // set intent so it does not start a new activity 
    notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | 
      Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    PendingIntent intent = 
      PendingIntent.getActivity(context, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, title, message, intent); 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notificationManager.notify(msgid, notification); 
    //notificationManager.cancelAll(); //i wan to clear all when the notification is clicked, where should i put this line? 
} 

答えて

58

私の解決方法はonResume()です。

@Override 
protected void onResume() { 
super.onResume(); 

// Clear all notification 
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
nMgr.cancelAll(); 
} 
+0

私の場合は機能しません。 –

+0

私のために働いていない –

+0

働く魅力 –

1

ブロードキャストを送信し、すべての通知をキャンセルするブロードキャスト受信者を配置する保留中のインテントを使用する必要があります。すべての通知IDを覚えて、1つずつ削除するのが最善です。

関連する問題