browser.loadUrl('http://code.google.com/android');
}
}
They simply load a different URL into the browser: the CommonsWare home page in one (Figure 24-3), the Android home page in the other (Figure 24-4). The resulting UI shows what tabbed browsing could look like on Android.

Figure 24-3.

Figure 24-4.
Using distinct subclasses for each targeted page is rather wasteful. Instead we could have packaged the URL to open as an “extra” in an Intent
and used that Intent
to spawn a general- purpose BrowserTab
activity, which would read the URL out of the Intent
“extra,” and use that. The proof of this is left as an exercise for the reader.
CHAPTER 25
Finding Available Actions via Introspection
Sometimes you know just what you want to do, such as display one of your other activities. Sometimes, you have a pretty good idea of what you want to do, such as view the content represented by a Uri
, or have the user pick a piece of content of some MIME type. Sometimes you’re lost. All you have is a content Uri
, and you don’t really know what you can do with it.
For example, suppose you were creating a common tagging sub-system for Android, where users could tag pieces of content — contacts, Web URLs, geographic locations, etc. Your sub-system would hold onto the Uri
of the content plus the associated tags, so other sub-systems could, say, ask for all pieces of content referencing some tag.
That’s all well and good. However, you probably need some sort of maintenance activity, where users could view all their tags and the pieces of content so tagged. This might even serve as a quasi-bookmark service for items on their phone. The problem is, the user is going to expect to be able to do useful things with the content they find in your sub-system, such as dial a contact or show a map for a location.
The problem is, you have absolutely no idea what is possible with any given content Uri
. You probably can view any of them, but can you edit them? Can you dial them? Since new applications with new types of content could be added by any user at any time, you can’t even assume you know all possible combinations just by looking at the stock applications shipped on all Android devices.
Fortunately, the Android developers thought of this.
Android offers various means by which you can present to your users a set of likely activities to spawn for a given content Uri
— even if you have no idea what that content Uri
really represents. This chapter explores some of these Uri
action introspection tools.
Pick ’Em
Sometimes you know your content Uri
represents a collection of some type, such as content://contacts/people
representing the list of contacts in the stock Android contacts list. In this case, you can let the user pick a contact that your activity can then use (e.g., tag it, dial it).
To do this, you need to create an Intent for the ACTION_PICK
on the target Uri
, then start a sub-activity (via startActivityForResult()
) to allow the user to pick a piece of content of the specified type. If your onActivityResult()
callback for this request gets a RESULT_OK
result code, your data string can be parsed into a Uri
representing the chosen piece of content.
For example, take a look at Introspection/Pick
in the sample applications in the Source Code section of http://apress.com. This activity gives you a field for a collection Uri
(with content://contacts/people
pre-filled in for your convenience), plus a really big Gimme! 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'
>
<EditText android:id='@+id/type'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:cursorVisible='true'
android:editable='true'
android:singleLine='true'
android:text='content://contacts/people'
/>
<Button
android:id='@+id/pick'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:text='Gimme!'
android:layout_weight='1'
/>
</LinearLayout>
Upon being clicked, the button creates the ACTION_PICK
on the user-supplied collection Uri
and starts the sub-activity. When that sub-activity completes with RESULT_OK
, the ACTION_VIEW
is invoked on the resulting content Uri
.
public class PickDemo extends Activity {
static final int PICK_REQUEST = 1337;
private EditText type;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
type = (EditText)findViewById(R.id.type);
Button btn = (Button)findViewById(R.id.pick);
btn.setOnClickListener(new View.OnClickListener() {