Wie zu verwenden NotificationCompat.Builder und startForeground?

Kurze Frage:

Ich versuche, die NotificationCompat.Generator-Klasse, um eine Meldung erstellen, die verwendet werden, für den service, aber für einige Grund, dass ich entweder nicht sehen, die Benachrichtigung, oder können nicht Abbrechen, wenn der Dienst sollte zerstört werden (oder zu stoppen, wird in den Vordergrund) .

mein 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);
}

Den auskommentierten Befehle sind meine Prüfungen zu machen, damit es funktioniert. keiner arbeitete für einige Grund.

Ich auch noch ein fake-Aktivität, da wollte ein contentIntent , aber es funktioniert immer noch nicht.

Kann bitte jemand helfen?

  • Dieser Beitrag, zusammen mit der akzeptierten Antwort, fixierte mein problem nach der Arbeit für Tage an einer Lösung.
Schreibe einen Kommentar