Comment annuler un de premier plan de services de l'aide de la notification (balayez vers la rejeter) ou effacer toutes les notifications?

Je suis en train de créer un premier plan avec un service de notification qui s'affiche dans la barre de notification lorsque le service démarre. Si le service s'arrête, la notification rejette. Ma question est, est-il un moyen d'arrêter le service lorsque "effacer toutes les notifications" ou un rejet de la notification (balayer) se produit?

Mis à jour pour inclure la mise en œuvre de la notification:

public int onStartCommand(Intent intent, int flags, int startId)
{   
    Log.d(CLASS, "onStartCommand service started.");

    if (getString(R.string.service_start_action) == intent.getAction())
    {
        Intent intentForeground = new Intent(this, ServiceMain.class)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);    
        PendingIntent pendIntent = PendingIntent.getActivity(getApplicationContext(), 0, intentForeground, 0);      
        Notification notification;
        Notification.Builder builder = new Notification.Builder(getApplicationContext())
            .setSmallIcon(android.R.drawable.btn_star)
            .setTicker("Service started...")
            .setContentIntent(pendIntent)
            .setDefaults(Notification.DEFAULT_ALL)
            .setOnlyAlertOnce(true)
            .setOngoing(false);
        notification = builder.build();
        notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

        startForeground(SERV_NOTIFY, notification);
        player.start();
    }
    else
    {
        Log.d(CLASS, "onStartCommand unable to identify acition.");
    }

    return START_STICKY;        
}
Jetez un oeil à cet exemple de Google: github.com/googlesamples/android-ActiveNotifications. Il montre comment écouter pour un licencié de notification, ainsi que tout un groupe de notifications.
Comme je sais que vous ne peut pas "créer" un premier plan de service sans la création d'une notification. Android nécessite ce qu'ils ne veulent pas vous en cours d'exécution des services de derrière la scène à l'insu de l'utilisateur. Retrait d'une notification lorsque votre activité a été détruit et le fonctionnement de votre service dans le premier plan de bons de souscription d'une notification, vous ne pouvez pas simplement l'annuler. L'annulation de la notification ne peut être fait que par l'annulation de la de premier plan de services par le biais de l'appel".stopForeground(true)', le paramètre booléen indique si vous souhaitez vous arrêter au premier plan avec la notification.

OriginalL'auteur d.moncada | 2012-10-13