2012-11-17 11 views
18

Nella mia app, è in esecuzione un servizio in background. Voglio avvisare l'utente che il servizio è in esecuzione. Ma ho bisogno che l'utente non può eliminare la notifica - premendo il tasto chiaro oppure strisciare fuori, nella barra di notificanotifica non rimovibile

enter image description here

Significa che ho bisogno di mostrare la mia notifica di sopra dell'area di notifica

risposta

53

Questo è possibile ma il modo in cui lo si implementa dipende dal livello API per il quale si sviluppa.

Per i livelli API inferiori a 11, è possibile impostare Notification.FLAG_NO_CLEAR. Ciò può essere implementato in questo modo:

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

Per livelli di API superiore a 11, o quando si utilizza il Android Support Library, si può implementare in questo modo:

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

In combitation con il post di antew, io ero esattamente cercando Notification.FLAG_ONGOING_EVENT. molte grazie – Kelib

9

Per creare la notifica non rimovibile basta usare setOngoing (vero);

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

         .setSmallIcon(R.drawable.ic_service_launcher) 

         .setContentTitle("My title") 

         .setOngoing(true) 

         .setContentText("Small text with details"); 
Problemi correlati