Make the Call

Once you have your Intent, you need to pass it to Android and get the child activity to launch. You have four choices:

• The simplest option is to call startActivity() with the Intent—this will cause Android to find the best-match activity and pass the Intent to it for handling. Your activity will not be informed when the "child" activity is complete.

• You can call startActivityForResult(), passing it the Intent and a number (unique to the calling activity). Android will find the best-match activity and pass the Intent over to it. However, your activity will be notified when the child activity is complete via the onActivityResult() callback (see the text following this list).

• You can call sendBroadcast(). In this case, Android will pass the Intent to all registered BroadcastReceivers that could possibly want this Intent, not just the best match.

• You can call sendOrderedBroadcast(). Here Android will pass the Intent to all candidate BroadcastReceivers one at a time—if any one "consumes" the Intent, the rest of the candidates are not notified.

Most of the time, you will wind up using startActivity() or startActivityForResult()— broadcast Intents are more typically raised by the Android system itself.

With startActivityForResult(), as noted, you can implement the onActivityResult() callback to be notified when the child activity has completed its work. The callback receives the unique number supplied to startActivityForResult(), so you can determine which child activity is the one that has completed. You also get the following:

• A result code from the child activity calling setResult(). Typically this is RESULT OK or RESULT CANCELLED, though you can create your own return codes (pick a number starting with RESULT FIRST USER).

• An optional String containing some result data, possibly a URL to some internal or external resource—for example, an ACTION PICK Intent typically returns the selected bit of content via this data string.

• An optional Bundle containing additional information beyond the result code and data string.

To demonstrate launching a peer activity, take a peek at the Activities/Launch sample application in the Source Code section at http://apress.com. The XML layout is fairly straightforward: two fields for the latitude and longitude, plus a button:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout width="fill parent" android:layout height="fill parent"

<TableLayout android:layout width="fill parent"

android:layout height="wrap content"

android:stretchColumns="1,2"

<TableRow> <TextView android:layout width="wrap content" android:layout height="wrap content" android:paddingLeft="2dip" android:paddingRight="4dip" android:text="Location:"

<EditText android:id="@+id/lat" android:layout width="fill parent" android:layout height="wrap content" android:cursorVisible="true" android:editable="true" android:singleLine="true" android:layout weight="1"

<EditText android:id="@+id/lon" android:layout width="fill parent" android:layout height="wrap content" android:cursorVisible="true" android:editable="true" android:singleLine="true" android:layout weight="1"

</TableRow> </TableLayout>

<Button android:id="@+id/map" android:layout width="fill parent" android:layout height="wrap content" android:text="Show Me!"

</LinearLayout>

The button's OnClickListener simply takes the latitude and longitude, pours them into a geo scheme Uri, then starts the activity.

package com.commonsware.android.activities;

import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText;

public class LaunchDemo extends Activity { private EditText lat; private EditText lon;

@Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main);

Button btn=(Button)findViewById(R.id.map); lat=(EditText)findViewById(R.id.lat); lon=(EditText)findViewById(R.id.lon);

btn.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { String lat=lat.getText().toString(); String lon=lon.getText().toString(); Uri uri=Uri.parse("geo:"+ lat+","+ lon);

startActivity(new Intent(Intent.ACTION VIEW, uri));

The activity is not much to look at (Figure 24-1).

Figure 24-1. The LaunchDemo sample application, with a location filled in

If you fill in a location (e.g., 38.8891 latitude and -77.0492 longitude) and click the button, the resulting map is more interesting (Figure 24-2). Note that this is the built-in Android map activity—we did not create our own activity to display this map.

In a Chapter 34, you will see how you can create maps in your own activities, in case you need greater control over how the map is displayed.

SHDe 2:11 PM

■ «

£

n /

\ el

■ Lincoln National Memorial

i

- Indent

1 \

Figure 24-2. The map launched by LaunchDemo, showing the Lincoln Memorial in Washington DC

Was this article helpful?

0 0

Post a comment