2012-07-27 49 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