PendingIntent 不发送 Intent extras [英] PendingIntent does not send Intent extras

查看:29
本文介绍了PendingIntent 不发送 Intent extras的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

My MainActicityIntent 启动 RefreshService,其中有一个 boolean 额外的名为 isNextWeek.

My MainActicity starts RefreshService with a Intent which has a boolean extra called isNextWeek.

我的 RefreshService 制作了一个 Notification,当用户点击它时它会启动我的 MainActivity.

My RefreshService makes a Notification which starts my MainActivity when the user clicks on it.

看起来像这样:

    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.

As you can see the notificationIntent should have the booleanextra IS_NEXT_WEEK with the value of isNextWeek which is put in the PendingIntent.

当我现在点击这个 Notification 我总是得到 false 作为 isNextWeek

When I click now this Notification I always get false as value of isNextWeek

这是我在MainActivity中获取值的方式:

This is the way I get the value in the 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

当我直接使用 Intent 和 ìsNextValue` 启动 MainActivity 时:

When I directly start the MainActivity with an Intent with the ìsNextValue` like this:

    Intent i = new Intent(this, MainActivity.class);
    i.putExtra(IS_NEXT_WEEK, isNextWeek);
    finish();
    startActivity(i);

一切正常,当 isNextWeektrue 时,我得到 true.

everything works fine and I get true when isNextWeek is true.

总是有一个 false 值,我有什么错?

What do I make wrong that there is always a false value?

这解决了问题:https://stackoverflow.com/a/18049676/2180161

引用:

我的怀疑是,因为 Intent 中唯一改变的是额外的, PendingIntent.getActivity(...) 工厂方法是只需重新使用旧意图作为优化.

My suspicion is that, since the only thing changing in the Intent is the extras, the PendingIntent.getActivity(...) factory method is simply re-using the old intent as an optimization.

在 RefreshService 中,尝试:

In RefreshService, try:

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.

推荐答案

使用 PendingIntent.FLAG_CANCEL_CURRENT 不是一个好的解决方案,因为内存使用效率低下.而是使用 PendingIntent.FLAG_UPDATE_CURRENT.

Using PendingIntent.FLAG_CANCEL_CURRENT not a good solution because of inefficient use of memory. Instead use PendingIntent.FLAG_UPDATE_CURRENT.

也使用 Intent.FLAG_ACTIVITY_SINGLE_TOP(如果活动已经在历史堆栈的顶部运行,则不会启动它).

Use also Intent.FLAG_ACTIVITY_SINGLE_TOP (the activity will not be launched if it is already running at the top of the history stack).

Intent resultIntent = new Intent(this, FragmentPagerSupportActivity.class).
                    addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);  
resultIntent.putExtra(FragmentPagerSupportActivity.PAGE_NUMBER_KEY, pageNumber);

PendingIntent resultPendingIntent =
                PendingIntent.getActivity(
                        this,
                        0,
                        resultIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );

那么:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        try {
            super.onCreate(savedInstanceState);

            int startPageNumber;
            if ( savedInstanceState != null)
            {
                startPageNumber = savedInstanceState.getInt(PAGE_NUMBER_KEY);
//so on

现在应该可以了.

如果您仍然没有预期的行为,请尝试实现 void onNewIntent(Intent intent) 事件处理程序,这样您就可以访问为该活动调用的新意图(这是不一样的)就像调用 getIntent() 一样,这将始终返回启动您的 Activity 的第一个 Intent.

If you still have not expected behaviour, try to implement void onNewIntent(Intent intent) event handler, that way you can access the new intent that was called for the activity (which is not the same as just calling getIntent(), this will always return the first Intent that launched your activity.

@Override
protected void onNewIntent(Intent intent) {
    int startPageNumber;

    if (intent != null) {
        startPageNumber = intent.getExtras().getInt(PAGE_NUMBER_KEY);
    } else {
        startPageNumber = 0;
    }
}

这篇关于PendingIntent 不发送 Intent extras的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆