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; }

Unslappable SEO

Unslappable SEO

Inside of this special report, you'll discover... The guaranteed, bullet-proof method for ensuring that you NEVER lose rankings again. How to recover from the latest search engine update and reclaim your traffic, instantly. Find out how you can instantly maximize traffic and exposure with social signals. How you can create drop dead easy micro-sites that will bolster your ranking almost immediately and much, much more.

Get My Free Ebook


Post a comment