Myは、と呼ばれるエクストラを持つaでMainActicity
始まります。RefreshService
Intent
boolean
isNextWeek
MyRefreshService
は、ユーザーがクリックしたときにNotification
my を開始する を作成します。MainActivity
これは次のようになります。
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);
ご覧のとおり、には、 に入れられる値を持つ余分なnotificationIntent
ものが必要です。boolean
IS_NEXT_WEEK
isNextWeek
PendingIntent
今クリックすると、Notification
常に次false
の値として取得されますisNextWeek
これは、次の値を取得する方法ですMainActivity
。
isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);
ログ:
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
次のように `sNextValue`で直接開始するMainActivity
と:Intent
Intent i = new Intent(this, MainActivity.class);
i.putExtra(IS_NEXT_WEEK, isNextWeek);
finish();
startActivity(i);
すべてが正常に動作し、 がtrue
いつになるかisNextWeek
がわかりtrue
ます。
常にfalse
値があるというのは、どこが間違っているのでしょうか?
アップデート
これは問題を解決します: https://stackoverflow.com/a/18049676/2180161
見積もり:
インテントで変更されるのはエクストラのみであるため、
PendingIntent.getActivity(...)
ファクトリ メソッドは単純に古いインテントを最適化として再利用していると思われます。RefreshService で、次のことを試してください。
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
見る:
http://developer.android.com/reference/android/app/PendingIntent.html#FLAG_CANCEL_CURRENT
更新 2
を使用する方が良い理由については、以下の回答を参照してくださいPendingIntent.FLAG_UPDATE_CURRENT
。