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 android.app.ListActivity; import android.app.SearchManager; import android.content.Intent; import android.net.Uri; 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,

Uri.parse("http://www.android.com/")));

break;

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

Uri.parse("content://contacts/people/")));

break;

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

break;

case 3: //search Google for the string

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

break;

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

Intent(Intent.ACTION_VOICE_COMMAND));

break;

default: break; }

Corner The Local Search Engine Market

Corner The Local Search Engine Market

Four Steps to Ensure your Business will Capitalize from Local Google Search Exposure. We live in an age where everyday more and more people are connecting online. The internet is more of a household necessity than passing fad.

Get My Free Ebook


Post a comment