2013-03-07 84 views
1

您好我新开发Android应用程序,我有一些问题。GCM消息返回填写表格

我GCM正常工作:

protected void onMessage(Context context, Intent intent) { 
     Log.i(TAG, "Receenter code hereived message"); 
     Log.i(TAG, intent.getStringExtra("name")); 
     Log.i(TAG, intent.getStringExtra("fone")); 

     //google example (with this, the "message" ll be append in screen, like a miracle) 
     displayMessage(context, intent.getStringExtra("nome") + " " + intent.getStringExtra("telefone")); 
    } 

是登录电子名称和Fone公司,但我想在我的表单字段这就是把“消息”。

<?xml version="1.0" encoding="utf-8"?> 
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scroll" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_margin="8dip" > 

    <TableLayout 
     android:id="@+id/TableLayout1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:layout_margin="8dp" > 

     <TableRow 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="8dp" > 



     <TextView 
      android:id="@+id/display" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     </TableRow> 

     <EditText 
      android:id="@+id/name" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" > 

      <requestFocus /> 
     </EditText> 

     <EditText 
      android:id="@+id/fone" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:ems="10" /> 
    </TableLayout> 

</ScrollView> 

我该怎么办呢? (获得活力,获得领域,填补领域......)

回答

1

一旦在GCMIntentService中获得“消息”,就可以通过多种方式将其内容传递给其他应用程序组件。

在GCM演示程序中,他们创建了一个通知(请参阅“generateNotification”方法)。 https://code.google.com/p/gcm/source/browse/samples/gcm-demo-client/src/com/google/android/gcm/demo/app/GCMIntentService.java

如果你想显示在Activity的内容,那么你要解雇一个Intent的到Activity与所需的数据为“临时演员”。

您不会在GCM服务中“获取活动”,而是指定“意图”,系统将调用正确的Activity来处理它。欲了解更多信息,请参阅有关intents/intentfilters的文档:http://developer.android.com/guide/components/intents-filters.html

下面是一个过于简单的例子:

protected void onMessage(Context context, Intent intent) { 
    Log.i(TAG, "Receenter code hereived message"); 
    Log.i(TAG, intent.getStringExtra("name")); 
    Log.i(TAG, intent.getStringExtra("fone")); 

    // create ANOTHER intent to fire off the data to YOUR activity 
    Intent i = new Intent(this, MyActivity.class); 
    i.putExtra("nome", intent.getStringExtra("nome")); 
    i.putExtra("fone", intent.getStringExtra("fone")); 
    startActivity(i);  

}

然后在MyActivity做类似如下:

String nome = getIntent().getStringExtra("nome"); 

正如我所说的,这个例子过于简单(你需要在使用它之前检查数据是否真的存在,在你的服务和你的活动中,你可能想要使用其他数据类型并且有默认值,还有其他交换数据的方法取决于需求),但它应该让你开始。

+0

嗯,thx这么多,你帮我很多。 – rcorbellini 2013-03-07 16:32:41

+0

更多一个小问题,如果我的设备没有添加谷歌帐户只有添加coporate accont,我可以在这个设备使用gcm? – rcorbellini 2013-03-07 16:34:15

+0

如果这里的答案对您有帮助,您可能需要将其标记为“已接受”。至于使用Google帐户与否,这将是另一个问题,请继续并为此提交一个新问题。而且,欢迎来到SO! – 2013-03-07 17:29:52