android - GCM 消息返回填写表单

标签 android android-activity fill google-cloud-messaging

大家好,我是开发 Android 应用程序的新手,我有一些问题。

我的 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"));
    }

正在登录姓名和电话,但我想在我的表单字段中输入“消息”。

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

我该怎么做? (获取actvity,获取字段,填充字段...)

最佳答案

一旦您在 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 中显示内容,那么你需要向该 Activity 触发一个 Intent所需数据作为“额外”。

您不是在 GCM 服务中“获取 Activity ”,而是指定“Intent ”,系统将调用正确的 Activity 来处理它。有关更多信息,请参阅有关 Intent/Intent 过滤器的文档: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");

正如我所说,这个示例过于简单化(您需要在使用之前检查数据是否确实存在,在您的服务和 Activity 中,您可能希望使用其他数据类型并具有默认值,还有其他根据要求交换数据的方法),但它应该可以帮助您入门。

关于android - GCM 消息返回填写表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15273461/

相关文章:

Android Studio 电视 Remote 按钮

excel - 使用宏突出显示具有背景颜色的单元格

python - numpy 前向填充条件

java - 从 ArrayList 中删除元素

php - 使用 json rereiver php mysql 填充 fragment 中的 ListView

android - 将监听器放在 Activity 之间的过渡动​​画上

java - 如何将 currentLocation ActivityOne 传递给 ActivityTwo?

android - 在 Activity 的生命周期变化期间接收广播

c++ - 将另一个数组中的随机元素复制到数组中的最快方法 C++

android - 稍后在 ViewModel 中初始化 LiveData