2012-11-17 48 views
18

在我的應用程序中,有服務在後臺運行。我想通知用戶服務正在運行。但我需要的是用戶無法刪除通知 - 按清除按鈕或刷卡出來,在通知欄不可移動的通知

enter image description here

這意味着我需要證明我的通知區域

回答

53

這是可能的,但實現它的方式取決於您開發的API級別。

對於低於11的API級別,您可以設置Notification.FLAG_NO_CLEAR。這可以實現這樣的:

// 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; 

對於API等級11以上,或使用Android Support Library時,可以這樣實現:

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

在與antew的帖子合作時,我正在尋找Notification.FLAG_ONGOING_EVENT。非常感謝 – Kelib

9

創建非可拆卸的通知只是用setOngoing(真正);

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

         .setSmallIcon(R.drawable.ic_service_launcher) 

         .setContentTitle("My title") 

         .setOngoing(true) 

         .setContentText("Small text with details");