2010-04-13 30 views
112

Nếu có một số vấn đề với thông báo tôi muốn hiển thị trên thanh thông báo. Mặc dù tôi đặt cờ thông báo thành Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL thông báo sẽ không biến mất sau khi nhấp vào. Bất kỳ ý tưởng những gì tôi đang làm sai?Thông báo của Android không biến mất sau khi nhấp vào thông báo

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 

    int icon = R.drawable.icon; 
    CharSequence tickerText = "Ticker Text"; 
    long time = System.currentTimeMillis(); 

    Notification notification = new Notification(icon, tickerText, time); 
    notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL; 

    Context context = getApplicationContext(); 
    CharSequence contentTitle = "Title"; 
    CharSequence contentText = "Text"; 
    Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class); 
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 
    notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); 
    mNotificationManager.notify(1,notification); 

Trả lời

247

Khi xây dựng Notification bởi NotificationBuilder bạn có thể sử dụng notificationBuilder.setAutoCancel(true);.

+0

Cảm ơn rất nhiều. Nó làm việc cho tôi – Sakthimuthiah

+2

Vì vậy, những gì khác biệt tạo ra thông báo bằng cách sử dụng Notification 'mNotificationManager.notify (1, thông báo);' và sử dụng NotificationBuilder 'mNotificationManager.notify (1, mBuilder.build());'? Cảm ơn. – NPE

+9

Câu trả lời này phải được chấp nhận, nó phù hợp hơn với học thuyết thiết kế Android hiện tại – jmaculate

126
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL 

Từ các tài liệu:

Bit là bitwise- hoặc ed vào cờ lĩnh vực đó nên được đặt nếu thông báo nên bị hủy khi nó được nhấp bởi người dùng

+0

Oh người đàn ông, cảm ơn! Lần tới tôi sẽ đọc tài liệu một cách chính xác hơn. – Flo

+3

Đây không phải là câu trả lời đúng. 'Notification.DEFAULT_LIGHTS' là một phần của lớp' Notification.defaults', không phải là lớp 'Notification.flags'. Xem câu trả lời của tôi cho những người định cư thích hợp. – Darcy

+0

Cảm ơn người đàn ông, nó đã giúp tôi –

27
// Uses the default lighting scheme 
notification.defaults |= Notification.DEFAULT_LIGHTS; 

// Will show lights and make the notification disappear when the presses it 
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS; 
+1

Tôi đã trải qua các tài liệu android. Tôi không hoàn toàn nhận được khi cờ nên được sử dụng. Tại sao không chỉ là thông báo.defaults = notification.DEFAULT_LIGHTS đủ để hiển thị đèn. Bởi vì rung động và âm thanh hoạt động mà không có cờ. – Ashwin

+0

Đang sử dụng các NotificationBuilder, NotificationCompat.Builder mBuilder = NotificationCompat.Builder mới (this) .setSmallIcon (android.R.drawable.ic_popup_sync) .setContentTitle ("New Tweet") .setContentText ("Có" + count + "tweets"); mBuilder.setDefaults (NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); – Joseph

0

Sử dụng cờ Notification.FLAG_AUTO_CANCEL

Notification notification = new Notification(icon, tickerText, when); 

notification.setLatestEventInfo (bối cảnh, contentTitle, contentText, pendingIntent);

// Hủy thông báo sau khi đã chọn thông báo.flags | = Thông báo.FLAG_AUTO_CANCEL; và khởi chạy ứng dụng:

Thông báoThông báo người quản lýManager = (NotificationManager) context.getSystemService (Context.NOTIFICATION_SERVICE);

Intent intent = new Intent(context, App.class); 

PendingIntent pendingIntent = PendingIntent.getActivity (bối cảnh, intent_id, ý định, PendingIntent.FLAG_UPDATE_CURRENT);

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