Comment utiliser NotificationCompat.Constructeur et startForeground?

Petite question:

Je suis en train d'utiliser le NotificationCompat.Constructeur de la classe afin de créer une notification qui sera utilisé pour le service, mais pour une raison quelconque, je n'en vois pas la notification, ou ne peut pas l'annuler lorsque le service doit être détruit (ou d'arrêter d'être au premier plan) .

mon code:

@Override
public int onStartCommand(final Intent intent, final int flags, final int startId) {
    final String action = intent == null ? null : intent.getAction();
    Log.d("APP", "service action:" + action);
    if (ACTION_ENABLE_STICKING.equals(action)) {
        final NotificationCompat.Builder builder = new Builder(this);
        builder.setSmallIcon(R.drawable.ic_launcher);
        builder.setContentTitle("content title");
        builder.setTicker("ticker");
        builder.setContentText("content text");
        final Intent notificationIntent = new Intent(this, FakeActivity.class);
        final PendingIntent pi = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        builder.setContentIntent(pi);
        final Notification notification = builder.build();
        //notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;
        //notification.flags |= Notification.FLAG_NO_CLEAR;
        //notification.flags |= Notification.FLAG_ONGOING_EVENT;

        startForeground(NOTIFICATION_ID, notification);
        //mNotificationManager.notify(NOTIFICATION_ID, notification);

    } else if (ACTION_DISABLE_STICKING.equals(action)) {
        stopForeground(true);
        stopSelf();
        //mNotificationManager.cancel(NOTIFICATION_ID);
    }
    return super.onStartCommand(intent, flags, startId);
}

Le commentaire commandes sont mes essais pour le faire fonctionner. aucun n'a fonctionné pour une raison quelconque.

J'ai même ajouté un faux activité, car elle voulait un contentIntent , mais cela ne fonctionne toujours pas.

Quelqu'un peut s'il vous plaît aider?

Ce post, avec la accepté de répondre, fixe mon problème après avoir travaillé pendant des jours sur une solution.

OriginalL'auteur android developer | 2013-04-28