PendingIntent sendet keine Intent-Extras
MeineMainActicity
beginntRefreshService
mit einerIntent
welches hat eineboolean
extra angerufenisNextWeek
.
MeineRefreshService
macht einNotification
das beginnt meinMainActivity
wenn der Benutzer darauf klickt.
das sieht so aus:
Log.d("Refresh", "RefreshService got: isNextWeek: " + String.valueOf(isNextWeek));
Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra(MainActivity.IS_NEXT_WEEK, isNextWeek);
Log.d("Refresh", "RefreshService put in Intent: isNextWeek: " + String.valueOf(notificationIntent.getBooleanExtra(MainActivity.IS_NEXT_WEEK,false)));
pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
builder = new NotificationCompat.Builder(this).setContentTitle("Title").setContentText("ContentText").setSmallIcon(R.drawable.ic_notification).setContentIntent(pendingIntent);
notification = builder.build();
// Hide the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(NOTIFICATION_REFRESH, notification);
Wie Sie sehen könnennotificationIntent
sollte das habenboolean
extraIS_NEXT_WEEK
mit dem Wert vonisNextWeek
das wird in diePendingIntent
.
Wenn ich jetzt darauf klickeNotification
Ich bekomme immerfalse
als Wert vonisNextWeek
Auf diese Weise erhalte ich den Wert in derMainActivity
:
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
Log:
08-04 00:19:32.500 13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity sent: isNextWeek: true
08-04 00:19:32.510 13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService got: isNextWeek: true
08-04 00:19:32.510 13367-13573/de.MayerhoferSimon.Vertretungsplan D/Refresh: RefreshService put in Intent: isNextWeek: true
08-04 00:19:41.990 13367-13367/de.MayerhoferSimon.Vertretungsplan D/Refresh: MainActivity.onCreate got: isNextWeek: false
Wenn ich das direkt starteMainActivity
mit einemIntent
mit dem ìsNextValue` wie folgt:
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
Alles funktioniert gut und ich verstehetrue
wannisNextWeek
isttrue
.
Was mache ich falsch, dass es immer eine gibt?false
Wert?
Dies löst das Problem:https://stackoverflow.com/a/18049676/2180161
Zitat:
Mein Verdacht ist, dass das einzige, was sich in der Absicht ändert, die Extras sind, diePendingIntent.getActivity(...)
Die Factory-Methode verwendet einfach die alte Absicht als Optimierung.
Versuchen Sie in RefreshService Folgendes:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Sehen:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
UPDATE 2SehenAntwort unten warum ist es besser zu benutzenPendingIntent.FLAG_UPDATE_CURRENT
.