Agrégateur de particules RSS compatible Android et iPhone!

Utiliser la bar de notification dans vos applications android

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.

?Download download.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Application alerte notification"
    />
    <Button  
     android:id="@+id/btn_showsample"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Montrer une alert." />
</LinearLayout>

Je crée deux objets de type NotificationManager(objet gérant les notifications) et Notification(objet de la notification)

?Download download.txt
1
2
 private NotificationManager mNotificationManager;
 Notification notification;

Ensuite j’instancie ces objets et avec la méthode setOnClickListener de mon bouton je lance une notification.

?Download download.txt
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
  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);
            }
        });

Résutat

Devenez développeur Android grâce à une formation en vidéo, investissez quelques dizaines d’euros et vendez vos applications sur Android Market comme un pro. Rendez-vous sur la boutique de formation en vidéo Android France de video2brain.

A lire aussi:

  1. Utiliser en développement le service vibreur de votre téléphone android dans vos applications
  2. Utiliser la camera dans vos applications android
  3. Tuto utiliser le capteur GPS de son téléphone android dans ses applications
  4. Utiliser du code HTML dans ses applications android
  5. Utiliser des onglets dans ses applications android

Une nouvelle version de notre Widget est disponible sur Android Market, Cliquez ICI ou scannez le code barre pour ne plus rater aucunes news.


Soutenez nous, devenez fan de la page Facebook Android France. Il suffit de cliquer ICI et de cliquer sur le bouton devenir Fan.


© Guy pour Android-France, 2009. |
Permalien |
Soyez le premier à réagir |
Add to
del.icio.us


Pas de commentaire

Pas encore de commentaire.

Comments RSS

Désolé, les commentaires sont fermés pour le moment.