Search code examples
androidandroid-activityfillgoogle-cloud-messaging

GCM message return fill form


Hi i m new in develop android application and i have some questions.

my GCM works fine:

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

be loging name and fone but i wanna put thats "message" in my form fields.

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

how i can do it? (get the actvity, get field, fill the field...)


Solution

  • Once you get the "message" in your GCMIntentService there are several ways you can deliver the contents of it to other application components.

    In the GCM demo app they create a notification (see the "generateNotification" method). https://code.google.com/p/gcm/source/browse/samples/gcm-demo-client/src/com/google/android/gcm/demo/app/GCMIntentService.java

    If you want to show the contents in an Activity, then you'll want to fire of an Intent to that Activity with the required data as "extras."

    You don't "get the activity" in the GCM service, rather you specify the "intent" and the system will call the correct Activity to handle it. For more info see the docs on intents/intentfilters: http://developer.android.com/guide/components/intents-filters.html.

    Here's an oversimplified example:

    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);      
    

    }

    Then in MyActivity do something like the following:

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

    As I said, this example is oversimplified (you'll need to check that data is actually there before using it, in both your service and your activity, you may want to use other data types and have defaults, there are other ways to exchange data depending on requirements), but it should get you started nonetheless.