Getting a Result via Inter Process Communication

Appportunity

Build your own Android App Dev Empire

Get Instant Access

This section modifies the "Hello World" application from an earlier chapter to show how Android can make separate Activity objects in separate programs seem of-a-piece. This version uses one Activity to enable the user to choose which greeting to put on the screen in another Activity. A copy of the data put into the Intent object in the previous section ends up in an Intent object in the HelloWorldActivity Activity.

To enable a client to find the Intent, the server assigns it a label called an action. In this case, we'll call our action PICK, shown here in Example 13-3.

Example 13-3. HelloWorldActivity.java package example.helloworld;

import android.app.Activity; import android.content.Intent; import android.os.Bundle;

import android.widget.TextView;

public class HelloWorldActivity extends Activity { TextView helloView;

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

// Make a text view and set it to be the content view helloView = new TextView(this); setContentView(helloView);

// Make an Intent instance to fill in Intent helloIntent = new Intent();

// Set the action, and type helloIntent.setAction("android.intent.action.PICK"); helloIntent.setType("vnd.example.greeting/vnd.example.greeting-text");

// Ask an activity that matches our Intent object startActivityForResult(helloIntent, 0);

@Override protected void onActivityResult(int requestCode, int resultCode, Intent result) {

String greeting = result.getStringExtra("result");

helloView.setText(greeting);

The changes we made will start an Activity in a separate application and a separate process to provide the user interface for selecting a greeting. After that greeting is returned by the other Activity, this one uses it to say hello.

Run the program. You will see the user interface presented by the SayHello program, just as in Figure 13-1. But this time, when you press one of the two buttons, the screen will display the greeting you selected (Figure 13-2).

Let's take a closer look at how it's done. Here, again, you may want to follow along using the debugger.

Did you run the SayHello program yet? You need to do that before you run our modified HelloWorldActivity program. The Android emu" v I lator installs programs the first time you run them, so once you run SayHello it will stay around as long as the emulator is running. But if the program hasn't been run yet, the startActivityForResult call in the current example will fail, because Android cannot find SayHello.

1

2

3

4

M

6

M

S

9

0

d

W

E

R

T

Y

u

1

0

P

A

S

0

F

G

H

1

K

L

DEL CsJ

z

X

c

V

B

N

M

<-»

SYM

@

y

!

Figure 13-2. Output of "Hello World" program after user selection

First, we need to start our helper application, which we do using an Intent object:

// Make an Intent instance to fill in Intent helloIntent = new Intent();

Then, we need to specify an Activity that is neither a part of our application nor part of an Activity in any of the programs that come with Android: // Set the action, and type helloIntent.setAction("android.intent.action.PICK"); helloIntent.setType("vnd.example.greeting/vnd.example.greeting-text");

The setType method requires a MIME type. We will use a vendor-specific MIME type unique to our purpose (by vendor here, I mean us). As a result, our SayHello activity is launched because it has an Intent filter that matches the parameters we have set in this Intent object.

Now we call the startActivityForResult method, passing the Intent object we created to hold the information that tells the Android framework to find an Activity matching the specifications in our Intent: the PICK action and the requested MIME type. We don't explicitly request the SayHello Activity—we might want to replace it with something else at some point—but for now, that activity is what Android will find:

// Ask an activity that matches our Intent object startActivityForResult(helloIntent, 0);

The startActivityForResult method navigates to a UI that obtains information and returns it. This is a good illustration of using IPC for a task that could otherwise have required redundant code in all applications that need similar information.

Now SayHello should run and display its user interface for selecting a greeting. When you have selected a greeting and the setResult method is called, Android's inter-process communication system will move the result to this process, and the OnActivityResult method will be called. We've defined it as follows:

protected void onActivityResult(int requestCode, int resultCode, Intent result) { if (resultCode == RESULT_OK) {

String greeting = result.getStringExtra("result");

helloView.setText(greeting);

The method calls getStringExtra to access the greeting we have chosen. It uses the setText method of the TextView class to display our selected greeting.

To summarize, in this example one program (SayHello) acquires some information and supplies it to another program (HelloWorldActivity). We have successfully used interprocess communication.

Android includes a component system based on remote objects and methods, which we'll examine in the next section. This is a powerful feature with many uses, but remote method calls are overkill in many cases. As you design your programs, first consider whether your inter-process communications needs fit what Intents and the Context class's Intent-related methods can do. Particularly when you're using inter-process communication to provide a user interface in an Activity, this high-level form of IPC is easy to use and appropriate to the task.

Was this article helpful?

0 0

Post a comment