2012-07-27 32 views
4

Android文檔here狀態的Android GCM - 爲GCM「處理接收到的數據」

該密鑰對數據中的值參數,它們可在此意圖 羣衆演員,用鑰匙是多餘的名字。

private void handleMessage(Intent intent) { 
    // server sent 2 key-value pairs, score and time 
    String score = intent.getExtra("score"); 
    String time = intent.getExtra("time"); 
    // generates a system notification to display the score and time 
} 

但intent.getExtra()方法不接受參數

public Bundle getExtras() 

Since: API Level 1 
Retrieves a map of extended data from the intent. 

Returns 
the map of all extras previously added with putExtra(), or null if none have been added. 

MyQuestion

如何從GCM消息onMessage()方法檢索 '字符串'?

P.S onMessage(Context context, Intent intent): Called when your server sends a message to GCM, and GCM delivers it to the device. If the message has a payload, its contents are available as extras in the intent.

回答

10

你應該使用:

intent.getExtras().getString("score"); 
intent.getExtras().getString("time"); 

要小心的類型,也可以是:

intent.getExtras().getInt("myvar"); 

或者一些其他類型。看看Bundle

+0

+1,爲什麼你指示我走向捆綁。你的答案是否來自可信來源? – 2012-07-28 22:29:40

+2

因爲getExtras()爲您提供了一個Bundle。 – thomasg 2012-07-30 16:16:40