2013-02-12 64 views
20

tôi đã tạo ra một thông báo trên thanh trạng thái ở android lập trình sử dụng mã đưa ra dưới đây với một id riêngLàm thế nào để bỏ/Hủy bỏ thông báo trên thanh trạng thái ở android trình

Notification notification; 
    public static NotificationManager myNotificationManager; 
    public static final int NOTIFICATION_ID = 1; 

private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) { 
      myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
      long when = System.currentTimeMillis(); 
      notification = new Notification(icon, NotificationTicket, when); 
      Intent notificationIntent = new Intent(context, MyActivity.class); 
      PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0); 
      notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent); 
      notification.flags |= Notification.FLAG_ONGOING_EVENT; 
      myNotificationManager.notify(NOTIFICATION_ID, notification); 

     } 

Bây giờ những gì tôi muốn là loại bỏ thông báo này trên nhấp vào một nút trong ứng dụng tha lập trình.

Làm cách nào tôi có thể đạt được điều này?

Trả lời

52

Bạn có thể sử dụng này:

public void clearNotification() { 
    NotificationManager notificationManager = (NotificationManager) mContext 
      .getSystemService(Context.NOTIFICATION_SERVICE); 
    notificationManager.cancel(NOTIFICATION_ID); 
} 
4

Sử dụng ID tương tự như sử dụng trong khi thông báo

myNotificationManager.cancel(NOTIFICATION_ID) 
7

myNotificationManager.cancelAll();

+1

Đây là những gì tôi muốn :) – styler1972

Các vấn đề liên quan