Dans ce tuto je vais mettre une alerte dans ma bar de modification quand je clique sur mon bouton
D’abord Mon fichier xml de mon interface.
Je crée deux objets de type NotificationManager(objet gérant les notifications) et Notification(objet de la notification)
private NotificationManager mNotificationManager;
Notification notification;
Ensuite j’instancie ces objets et avec la méthode setOnClickListener de mon bouton je lance une notification.
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//icone qui s'affichera dans la bar de notification
int icon = R.drawable.icon;
//texte dans la bar de notification
CharSequence tickerText = "Notification android-france";
long when = System.currentTimeMillis();
notification = new Notification(icon, tickerText, when);
Context context = getApplicationContext();
CharSequence contentTitle = "Notification Android-france";
CharSequence contentText = "Tuto Bar de notification";
Intent notificationIntent = new Intent(this, Appnotif.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Button btsauvegarde = (Button) findViewById(R.id.btn_showsample);
btsauvegarde.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
mNotificationManager.notify(1,notification);
}
});