1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| package com.tutomobile.android.notification;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Tutoriel16_Android extends Activity {
// On définit une variable global qui sera
// l'id unique correspondant à notre notification (bon moi j'ai choisi ma date de naissance :P)
public static final int ID_NOTIFICATION = 1988;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// On récupère nos deux boutons créer en XML grâce à leur id
Button boutonCreateNotif = (Button) findViewById(R.id.CreateNotif);
Button boutonClearNotif = (Button) findViewById(R.id.ClearNotif);
//On applique un écouteur d'évènement à notre bouton "Créer une notification"
boutonCreateNotif.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//on lance la méthode createNotify (qui comme son nom l'indique créera la notification)
createNotify();
}
});
//On applique un écouteur d'évènement à notre bouton "Supprimer la notification"
boutonClearNotif.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//on lance la méthode cancelNotify (qui supprimera la notification de la liste des notifications)
cancelNotify();
}
});
}
//Méthode qui créer la notification
private void createNotify(){
//On créer un "gestionnaire de notification"
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//On créer la notification
//Avec son icône et son texte défilant (optionel si l'on veut pas de texte défilant on met cet argument à null)
Notification notification = new Notification(R.drawable.icon, "Toc toc, c'est une notification !", System.currentTimeMillis());
//Le PendingIntent c'est ce qui va nous permettre d'atteindre notre deuxième Activity
//ActivityNotification sera donc le nom de notre seconde Activity
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, ActivityNotification.class), 0);
//On définit le titre de la notif
String titreNotification = "C'est moi la notification !";
//On définit le texte qui caractérise la notif
String texteNotification = "Je suis une belle notification...";
//On configure notre notification avec tous les paramètres que l'on vient de créer
notification.setLatestEventInfo(this, titreNotification, texteNotification, pendingIntent);
//On ajoute un style de vibration à notre notification
//L'utilisateur est donc également averti par les vibrations de son téléphone
//Ici les chiffres correspondent à 0sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration, 0.1sec de pause, 0.2sec de vibration
//Vous pouvez bien entendu modifier ces valeurs à votre convenance
notification.vibrate = new long[] {0,200,100,200,100,200};
//Enfin on ajoute notre notification et son ID à notre gestionnaire de notification
notificationManager.notify(ID_NOTIFICATION, notification);
}
//Méthode pour supprimer de la liste de notification la notification que l'on vient de créer
private void cancelNotify(){
//On créé notre gestionnaire de notfication
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//on supprime la notification grâce à son ID
notificationManager.cancelAll();
}
} |
Partager