139

Myは、と呼ばれるエクストラを持つaでMainActicity 始まります。RefreshServiceIntentbooleanisNextWeek

MyRefreshServiceは、ユーザーがクリックしたときにNotificationmy を開始する を作成します。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ものが必要です。booleanIS_NEXT_WEEKisNextWeekPendingIntent

今クリックすると、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

4

3 に答える 3

21

アクティビティでIntentオーバーライドして、新しいものを受け取ったら更新する必要があると思います。onNewIntent(Intent)以下をアクティビティに追加します。

@Override
public void onNewIntent(Intent newIntent) {
    this.setIntent(newIntent);

    // Now getIntent() returns the updated Intent
    isNextWeek = getIntent().getBooleanExtra(IS_NEXT_WEEK, false);        
}

編集:

これは、インテントを受け取ったときにアクティビティがすでに開始されている場合にのみ必要です。インテントによってアクティビティが開始された (再開されただけではない) 場合、問題は別の場所にあり、私の提案では解決できない可能性があります。

于 2013-08-03T23:31:23.270 に答える
3

次のコードが機能するはずです:-

int icon = R.drawable.icon;
String message = "hello";
long when = System.currentTimeMillis();
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification(icon, message, when);

Intent notificationIntent = new Intent(context, MainActivity.class);
notificationIntent.putExtra("isNexWeek", true);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent pIntent = PendingIntent.getActivity(context, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, title, message, pIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, notification);

MainActivity onCreate では:

if (getIntent().getExtras() != null && getIntent().getExtras().containsKey("isNextWeek")) {
        boolean isNextWeek = getIntent().getExtras().getBoolean("isNextWeek");
}
于 2013-08-03T23:05:04.797 に答える