Afficher une notification
De FrAndroid - Android docs.
Voyons comment afficher une notification dans votre application
Sommaire |
Dans la barre de notification
Nous appelons barre de notification, la barre qui se situe tout en haut de votre téléphone
Voilà ce que ça donne quand une notification est affichée
Il faut tout d'abord créer une instance du système de notification
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Créons ensuite quelques paramètres
int icon = R.drawable.iconNotification;
CharSequence text = "Ma notification";
long duration = System.currentTimeMillis();
Créons ensuite notre notification avec ces paramètres
Notification notification = new Notification(icon, text, time);
Maintenant, nous allons créer la sorte de bouton qui sera affichée dans le "menu déroulant" de la barre de notification
CharSequence contentTitle = "Le titre du bouton";
CharSequence contentText = "Le sous-titre";
Intent notificationIntent = new Intent(this, MonActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Dans cet exemple, MonActivity.class renvoi à l'activity qui devra être affichée lors d'un clique sur le bouton.
Et on termine tout ça en affichant notre notification :
Context context = getApplicationContext();
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
Code final
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE;);
int icon = R.drawable.iconNotification;
CharSequence text = "Ma notification";
long duration = System.currentTimeMillis();
Notification notification = new Notification(icon, text, time);
notification.flags|=Notification.FLAG_NO_CLEAR;
notification.flags|=Notification.FLAG_ONGOING_EVENT;
CharSequence contentTitle = "Call+";
CharSequence contentText = "Cliquez ici pour accéder à la fenêtre principale";
Intent notificationIntent = new Intent(this, Main.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
Context context = getApplicationContext();
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(NOTIFY_ID, notification);
Affichage d'un simple message
Voilà ce que nous voulons faire :
Un simple message qui vient informer l'utilisateur sans le faire changer de fenêtre.
Pour ça c'est extrêmement simple !
On met en place quelques informations
CharSequence text = "Lancement de Call+";
int duration = Toast.LENGTH_SHORT;
On créé un objet Toast, et on l'affiche
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Code final
Celui-ci tient sur 4 lignes !
CharSequence text = "Lancement de Call+";
int duration = Toast.LENGTH_SHORT;
Context context = getApplicationContext();
Toast toast = Toast.makeText(context, text, duration);
toast.show();


