Intent Extras of GCMIntentService is not passed

混江龙づ霸主 提交于 2019-12-22 10:16:26

问题


I coded GCMIntentService but called Activity cannot get the Extras.

@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);
}

and called MainActivity is coded as below.

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

    final Intent receivedIntent = getIntent();

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

I made breakpoint at the receivedIntent and mExtras inside of it shows null.

Additionally, my called MainActivity manifest is coded as below.

        <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>

I changed called intent to another Activity and it successfully loads Extras. The manifest of substitute activity (which successfully contains Extras) was as below.

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

What did I wrong?


回答1:


It looks like you need to override onNewIntent(Intent intent) method and inside onNewIntent() call setIntent(intent). Then onResume() will pick up the new intent. See onNewIntent() for more detail.



来源:https://stackoverflow.com/questions/17061190/intent-extras-of-gcmintentservice-is-not-passed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!