2012-11-17 19 views
18

मेरे ऐप में, पृष्ठभूमि पर सेवा चल रही है। मैं उपयोगकर्ता को सूचित करना चाहता हूं कि सेवा चल रही है। लेकिन मैं की जरूरत है कि उपयोगकर्ता अधिसूचना नहीं हटा सकते हैं - सूचना पट्टी में, स्पष्ट बटन दबाकर या यह कड़ी चोट बाहर सेगैर हटाने योग्य अधिसूचना

enter image description here

इसका मतलब है कि मैं अधिसूचना क्षेत्र

उत्तर

53

यह संभव है लेकिन जिस तरह से आप इसे लागू करते हैं, उस एपीआई स्तर पर निर्भर करता है जिसके लिए आप विकसित होते हैं।

11 से नीचे एपीआई स्तरों के लिए, आप Notification.FLAG_NO_CLEAR सेट कर सकते हैं। 11 से ऊपर

// 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 स्तरों के लिए, या जब 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 पद के साथ combitation में, मैं वास्तव में 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"); 
संबंधित मुद्दे