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());
}
démarrer le service de premier plan –