Propriété de Albiri Sigue
customer 27921 at Fri Mar 11 19:19:45 +0100 2011
Chapitre 32
Alerter les utilisateurs avec des notifications 315
– éventuellement par le code de votre application – pour prendre les mesures nécessaires
afin que l’utilisateur puisse gérer l’événement qui a déclenché la notification.
Vous pouvez également fournir un texte qui apparaîtra lorsque l’icône est placée sur la
barre d’état (tickerText).
Pour configurer ces trois composantes, l’approche la plus simple consiste à appeler la
méthode setLatestEventInfo(), qui enveloppe leurs initialisations dans un seul appel.
Les avertissements en action
Examinons le projet Notifications/Notify1, notamment sa classe NotifyDemo :
public class NotifyDemo extends Activity {
private static final int NOTIFY_ME_ID=1337;
private Timer timer=new Timer();
private int count=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn=(Button)findViewById(R.id.notify);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
TimerTask task=new TimerTask() {
public void run() {
notifyMe();
}
};
timer.schedule(task, 5000);
}
});
btn=(Button)findViewById(R.id.cancel);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
NotificationManager mgr=
(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
mgr.cancel(NOTIFY_ME_ID);
}
Livre Android.book Page 315 Dimanche, 8. novembre 2009 12:23 12
Précédent

- 330/391

Suivant