3

我编码GCMIntentService但叫Activity无法获得额外。意图其他GCMIntentService不通过

@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. 
     }  
    } 
} 

我在receivedIntent做出断点和mExtras它里面显示为空。

另外,我的MainActivity清单编码如下。

 <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)如下。

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

我错了什么?

回答

2

它看起来像你需要重写onNewIntent(Intent intent)方法和内部onNewIntent()调用setIntent(intent)。然后onResume()将选择新的意图。有关更多详细信息,请参阅onNewIntent()

+0

作品像魅力。谢谢:) – Youngjae

+0

没问题,很高兴它现在可以工作:) – Y2i

+0

我会在onMessage()中突出使用'PendingIntent.FLAG_UPDATE_CURRENT',因为没有它,现有的Intent会传递给onNewIntent(),并且它让这个新手感到困惑时间。 – markdwhite