2017-05-12 2 views
0

Mon application est en cours d'exécution en tant que service, j'ai besoin d'informer l'utilisateur que l'application est en cours d'exécution. J'essaie d'ajouter une notification sur la barre de notification. Comme il s'agit d'une application de service, je dois toujours afficher la notification. Comment puis-je empêcher l'utilisateur de supprimer la notification même lorsque l'application est fermée. Je en mesure d'empêcher l'utilisateur de supprimer l'application de swipe à l'aide,Prévenir pour supprimer la notification - Android

mBuilder.setOngoing(true); 

Comment afficher la notification même l'application quitter.

J'ai utilisé le code ci-dessous,

private void addNotificaiononNotificationBar(){ 

     NotificationCompat.Builder mBuilder = 
       new NotificationCompat.Builder(this) 
         .setSmallIcon(R.mipmap.ic_launcher) 
         .setContentTitle("My notification") 
         .setContentText("Hello World!"); 

     Intent resultIntent = new Intent(this, MainActivity.class); 
// Because clicking the notification opens a new ("special") activity, there's 
// no need to create an artificial back stack. 
     PendingIntent resultPendingIntent = 
       PendingIntent.getActivity(
         this, 
         0, 
         resultIntent, 
         PendingIntent.FLAG_UPDATE_CURRENT 
       ); 

     mBuilder.setContentIntent(resultPendingIntent); 
     mBuilder.setOngoing(true); 
     // Sets an ID for the notification 
     int mNotificationId = 001; 
     // Gets an instance of the NotificationManager service 
     NotificationManager mNotifyMgr = 
       (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
     // Builds the notification and issues it. 
     mNotifyMgr.notify(mNotificationId, mBuilder.build()); 

    } 
+0

démarrer le service de premier plan –

Répondre

2

Si vous souhaitez limiter la suppression de notification, vous pouvez créer ForegroundService si la notification existera lorsque le service est en cours d'exécution.

+0

Mon application est une application de fond? I s il possible d'ajouter une notification sur la barre de notification –

+0

Peu importe l'application de fond ou non. ** Premier plan ** signifie même ** priorité ** comme application de premier plan, rien de plus. En d'autres termes, c'est possible. – DEADMC