【问题标题】:Intent Extras of GCMIntentService is not passedGCMIntentService 的 Intent Extras 未通过
【发布时间】:2013-06-12 08:42:44
【问题描述】:

我编码GCMIntentService,但调用Activity 无法获得附加功能。

@Override
protected void onMessage(Context context, Intent intent) {

    Intent resultIntent = new Intent(this, MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
            Intent.FLAG_ACTIVITY_SINGLE_TOP);
        resultIntent.putExtra("PushType", "test"); // Put String Extra.

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

并称为MainActivity,编码如下。

@Override
protected void onResume() {
    super.onResume();

    final Intent receivedIntent = getIntent();

    if (receivedIntent != null && receivedIntent.hasExtra("PushType")) {
            // Breakpoint at here. but no Extras are given.
        }       
    }
}

我在receivedIntentmExtras 处设置断点,其中显示为空。

另外,我调用的MainActivity manifest 编码如下。

        <activity
        android:name=".MainActivity"
        android:theme="@android:style/Theme.NoTitleBar"
        android:configChanges="keyboardHidden|orientation|screenSize"
        android:label="@string/app_name">
        <intent-filter>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

我将调用意图更改为另一个 Activity,它成功加载了 Extras。 替代活动的清单(成功包含Extras)如下。

        <activity
        android:name=".NotificationActivity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity"/>
    </activity>

我做错了什么?

【问题讨论】:

    标签: android android-intent push-notification android-manifest


    【解决方案1】:

    看起来您需要重写 onNewIntent(Intent intent) 方法并在 onNewIntent() 内部调用 setIntent(intent)。然后 onResume() 将获取新的意图。详情请参阅onNewIntent()

    【讨论】:

    • 我会强调在 onMessage() 中使用 'PendingIntent.FLAG_UPDATE_CURRENT',因为没有它,现有的 Intent 会传递给 onNewIntent() 并且它让这个新手困惑了一段时间。
    猜你喜欢
    • 1970-01-01
    • 2011-11-14
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多