2012-11-17 26 views
18

Trong ứng dụng của tôi, có dịch vụ đang chạy trên nền. Tôi muốn thông báo cho người dùng rằng dịch vụ đang chạy. Nhưng tôi cần mà người dùng không thể xóa các thông báo - bằng cách nhấn nút rõ ràng hoặc bởi thao tác vuốt nó ra, trong thanh thông báothông báo không thể tháo rời

enter image description here

Nó có nghĩa là tôi cần phải hiển thị thông báo của tôi ở trên khu vực Notification

Trả lời

53

Điều này là có thể nhưng cách bạn triển khai tùy thuộc vào cấp API bạn phát triển.

Đối với các cấp API dưới 11, bạn có thể đặt Notification.FLAG_NO_CLEAR. Điều này có thể được thực hiện như thế này:

// Create notification 
Notification note = new Notification(R.drawable.your_icon, "Example notification", System.currentTimeMillis()); 

// Set notification message 
note.setLatestEventInfo(context, "Some text", "Some more text", clickIntent); 

// THIS LINE IS THE IMPORTANT ONE    
// This notification will not be cleared by swiping or by pressing "Clear all" 
note.flags |= Notification.FLAG_NO_CLEAR; 

Đối với các cấp API trên 11, hoặc khi sử dụng Android Support Library, người ta có thể thực hiện nó như thế này:

Notification noti = new Notification.Builder(mContext) 
    .setContentTitle("Notification title") 
    .setContentText("Notification content") 
    .setSmallIcon(R.drawable.yourIcon) 
    .setLargeIcon(R.drawable.yourBigIcon) 
    .setOngoing(true) // Again, THIS is the important line 
    .build(); 
+0

Khi kết hợp với bài đăng của antew, tôi đã tìm kiếm thông báo chính xác.FLAG_ONGOING_EVENT. Cảm ơn rất nhiều – Kelib

9

Để Tạo thông báo không tháo chỉ cần sử dụng setOngoing (thật);

NotificationCompat.Builder mBuilder = 
         new NotificationCompat.Builder(this) 

         .setSmallIcon(R.drawable.ic_service_launcher) 

         .setContentTitle("My title") 

         .setOngoing(true) 

         .setContentText("Small text with details"); 
Các vấn đề liên quan