Recipe Using Implicit Intents for Creating an Activity

Implicit intents do not specify an exact component to use. Instead, they specify the functionality required through a filter, and the Android system must determine the best component to utilize. An intent filter can be either an action, data, or a category.

The most commonly used intent filter is an action, and the most common action is ACTION_VIEW.This mode requires a uniform resource identifier (URI) to be specified and then displays the data to the user. It does the most reasonable action for the given URI. For example, the implicit intents in case 0, 1, and 2 in the following example have the same syntax but produce different results.

The steps for launching an activity using an implicit intent are

1. Declare the intent with the appropriate filter specified (ACTION_VIEW, ACTION_WEB_SEARCH, and so on).

2. Attach any extra information to the intent required to run the activity.

3. Pass this intent to startActivity().

This is shown for multiple intents in Listing 2.15.

Listing 2.15 src/com/cookbook/impMcitJntents/ListActivityExamplejava package com.cookbook.implicit_intents;

import; import; import android.content.Intent; import; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter;

import android.widget.ListView;

import android.widget.AdapterView.OnltemClickListener;

public class ListActivityExample extends ListActivity {

static final String[] ACTIVITY_CHOICES = new String[] { "Open Website Example", "Open Contacts", "Open Phone Dialer Example", "Search Google Example", "Start Voice Command"

final String searchTerms = "superman";

protected void onCreate(Bundle savedlnstanceState) { super.onCreate(savedlnstanceState);

setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ACTIVITY_CHOICES)); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE); getListView().setTextFilterEnabled(true);

getListView().setOnItemClickListener(new OnItemClickListener() {

@Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { switch(arg2) {

case 0: //opens web browser and navigates to given website startActivity(new Intent(Intent.ACTION_VIEW,



case 1: //opens contacts application to browse contacts startActivity(new Intent(Intent.ACTION_VIEW,



case 2: //opens phone dialer and fills in the given number startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("tel:12125551212")));


case 3: //search Google for the string

Intent intent= new Intent(Intent.ACTION_WEB_SEARCH ); intent.putExtra(SearchManager.QUERY, searchTerms); startActivity(intent);


case 4: //starts the voice command startActivity(new



default: break; }

SEO Made Easy

SEO Made Easy

Discover How To Get Your Sites On Top of The Search Engines So You Can Generate More Leads and Sales, Introducing... SEO Made Easy'. You'll discover why search engine optimization is important and why you should be optimizing are your pages so people can find your site.

Get My Free Ebook

Post a comment