web-dev-qa-db-fra.com

Icône de service persistant dans la barre de notification

J'essaie de comprendre comment montrer que mon service fonctionne avec une icône de notification persistante. De nombreux exemples que j'ai trouvés n'envoient qu'un message rejetable à la barre de notification. Je voulais une icône persistante indiquant que lorsque vous abaissez la barre de notification, elle indique que le service est en cours d'exécution et que vous pouvez cliquer dessus pour ouvrir l'application et fermer le service.

Quelqu'un peut-il m'indiquer des ressources ou un tutoriel sur la façon d'accomplir cela, tout APK convient, mais aimerait travailler avec 4.0 et supérieur.

Merci.

26
Nick

il doit être identique à un message licenciable, sauf que vous modifiez l'indicateur.

Notification.FLAG_ONGOING_EVENT

au lieu de

Notification.FLAG_AUTO_CANCEL

Lorsqu'une notification est cliquée, l'intention que vous envoyez est exécutée, vous vous assurez donc que l'activité effectue la tâche que vous souhaitez.

private void showRecordingNotification(){
    Notification not = new Notification(R.drawable.icon, "Application started", System.currentTimeMillis());
    PendingIntent contentIntent = PendingIntent.getActivity(this, 0, new Intent(this, main.class), Notification.FLAG_ONGOING_EVENT);        
    not.flags = Notification.FLAG_ONGOING_EVENT;
    not.setLatestEventInfo(this, "Application Name", "Application Description", contentIntent);
    mNotificationManager.notify(1, not);
}
36
Martin Sykes

Je sais que c'est une vieille question, mais c'était d'abord sur une page de résultats Google, donc je vais ajouter des informations pour aider les autres.

Notifications persistantes

L'astuce consiste à ajouter . SetOngoing à votre NotificationCompat.Builder

Bouton de fermeture

Un bouton qui ouvre l'application et ferme le service nécessite un PendingIntent

Un exemple

Cet exemple montre une notification persistante avec un bouton de fermeture qui quitte l'application.

MyService:

private static final int NOTIFICATION = 1;
public static final String CLOSE_ACTION = "close";
@Nullable
private NotificationManager mNotificationManager = null;
private final NotificationCompat.Builder mNotificationBuilder = new NotificationCompat.Builder(this);

private void setupNotifications() { //called in onCreate()
    if (mNotificationManager == null) {
        mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP),
            0);
    PendingIntent pendingCloseIntent = PendingIntent.getActivity(this, 0,
            new Intent(this, MainActivity.class)
                    .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP)
                    .setAction(CLOSE_ACTION),
            0);
    mNotificationBuilder
            .setSmallIcon(R.drawable.ic_notification)
            .setCategory(NotificationCompat.CATEGORY_SERVICE)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setContentTitle(getText(R.string.app_name))
            .setWhen(System.currentTimeMillis())
            .setContentIntent(pendingIntent)
            .addAction(Android.R.drawable.ic_menu_close_clear_cancel,
                    getString(R.string.action_exit), pendingCloseIntent)
            .setOngoing(true);
}

private void showNotification() {
    mNotificationBuilder
            .setTicker(getText(R.string.service_connected))
            .setContentText(getText(R.string.service_connected));
    if (mNotificationManager != null) {
        mNotificationManager.notify(NOTIFICATION, mNotificationBuilder.build());
    }
}

MainActivity doit gérer les intentions proches.

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    switch (action) {
        case MyService.CLOSE_ACTION:
            exit();
            break;
    }
}    

private void exit() {
    stopService(new Intent(this, MyService.class));
    finish();
}

AnotherActivity doit être terminé et envoyer une intention de sortie à MainActivity

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    String action = intent.getAction();
    if (action == null) {
        return;
    }
    switch (action) {
        case MyService.CLOSE_ACTION:
            exit();
            break;
    }
}

/**
 * Stops started services and exits the application.
 */
private void exit() {
    Intent intent = new Intent(getApplicationContext(), MainActivity.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setAction(Stn1110Service.CLOSE_ACTION);
    startActivity(intent);
}

Quelqu'un peut-il me diriger vers des ressources ou un tutoriel

http://developer.Android.com/training/notify-user/build-notification.html

21
Jon