Intent演習

1.actioncategory、コンテントプロバイダ対応でないdata及びtype

これらは、同じ基準で判断されますので同じ組にまとめます。

intentとintent-filterに指定する項目を次の表に示します。

 

#

指定項目

action

cat

urig

typeg

urig

_typeg

partial_urig

urig_then

_typeg

typeg_

then_urig

1

action

 

 

2

category

 

 

 

 

 

 

 

3

data

 

 

 

 

4

type

 

 

 

 

5

data(部分)

 

 

 

 

 

 

 

DEFAULT categoryはどのActivityのintent-filterにも記述します。

これはstartActivity()メソッドの処理対象にするためです。上記のcategoryを指定する旨のデータはDEFAULTとは別のcategoryを加えます。

intent-filterにactionは指定しないと、Intentの条件は決して満足しないのですべてのデータに設定します。△はIntentには指定しないことを意味しています。

また、categoryは副の情報なので、それだけでは相手を特定できないので、actionとcategoryの情報を使います。

data(部分)はintent-filterにスキーマだけを指定し、Intentにスキーマ+オーソリティを指定します。

urig_then_typeg及びurig_then_typegはsetData()とsetType()メソッドを使ってIntentにdataとtypeを設定します。urig_then_typeg及びurig_then_typegはこの二つのメソッドの呼び出す順序が異なります。intent-filterは、urigかtypeg、uri_typegと共用なので、urig_then_typegとurig_then_typegの単独のActivityはありません。urig_typegはsetDataAndType()のメソッドを使ってIntentにdataとtypeを設定します。

 

具体的なデータ

#

設定項目

値(""は含みません)

1

action

"INTENT_TEST_1_ACTION"

2

category

"INTENT_TEST_1_CATEGORY"

3

data

"my_scheme://jp.co.ichi/intent/test"

4

type

"text/plain"

5

data(部分)

"our_scheme://jp.co.ni "

注catで使うactionは"INTENT_TEST_1_CAT_ACTION"

   △に対応するactionはINTENT_TEST_1_FAKE_ACTION

 

作成手順

①filter名に対応するintent-filterを作成し、そのintent-filterを持つActivityをAndroidManifestに登録します。クラス名は、Activity#_filter名(#のところは2桁の番号)にすると便利です。

②mainのActivityを作ります。これは、上記のintent-filterを持つActivityとは別です。mainに必要なactionとcategoryを付けます。EclipseのAndroidプロジェクトで自動生成されるActivityをベースにするといいです。

③mainのActivityの画面に必要な数のボタンを配置して、それをクリックするとintent-filterを持つActivityを起動する(startActivity()呼び出し)処理を追加します。ボタンのid名はfilter名と同じにします。

④intent-filterを持つActivity(Activity#_filter名)の画面はすべて同じとします。EclipseのAndroidプロジェクトで自動生成されるActivityをベースにして、onCreateの中でTextViewに自身のクラス名を書き出すようにします。クラス名は、getClass().getSimpleName()で取得できますので、画面を定義するlayoutのxmlはすべて共通にできます。

 

具体的なソースコードは付録に掲載します。

実行結果は、指定したIntentと同じ内容を記述したIntent-filterを持つActivityが起動されるはずです。

 

2.コンテントプロバイダ対応のdatatype

指定したIntentと、そのIntentに対応するActivityのintent-filterの関係がactionのように簡単ではありません。Intentとintent-filterを記述するために、表ではなく次の記述法を用います。

(x,y):dataがx、typeがyのIntent。(x, )と( ,y)はそれぞれ、yとxが指定されないことを意味します。

[x,y]:dataがx、typeがyのintent-filter。y又はxがない場合の記法はIntentと同じです。

u:対応するコンテントプロバイダが実在するときの、そのコンテントプロバイダのuri

f(u):uから演繹されるtype。uに対応するコンテントプロバイダが実在するので、f(u)は意味のある値をとります。

v:対応するコンテントプロバイダが実在しないときの、uri。形式はcontent://...です。

t: 対応するコンテントプロバイダが実在しないときの、type。形式はvnd.X.cursor.dir/Yです。dirがitemの形式もありますがテストの中では使っていません。

dataについては、uかvか指定なしの3通りの値があります。typeもf(u)かtか指定無しの3通りがあります。dataとtypeの両方が無指定の場合は、テストの対象からはずします。すると、次の8通りのIntenが考えられます。

 

#

データ名

データ

通過するfilter(*1)

uric

(u, )

[ ,f(u)]、[u,f(u)]、M、C

typec

( ,f(u))

[ ,f(u)]、M、C

uric_typec

(u,f(u))

[ ,f(u)]、[u,f(u)]、M、C

uric_wrong_typec

(u,t)

[ ,t]、[u,t]、M

wrong_uric_typec

(v,f(u))

[ ,f(u)]、[v,f(u)]、M、C

wrong_uric

(v, )

[v, ]

wrong_typec

( ,t)

[ ,t] 、M

wrong_uric_wrong_typec

(v,t)

[ ,t]、[u,t]、M

注*1 実際のテスト結果。一意に決まらないので選択するためのダイアログが表示されます。その内容をまとめたものです。MはMessaging、CはCreate contactやContactsが選択項目に含まれます。contacの関係はContactsCotractコンテントプロバイダをテストに使っているためです。Messagingは、偶然filterが合致するためでしょう。Intentでアクション名を設定するとMやCは除去できますので、実際のアプリでは問題はありません。

 

intent-filterについても同様に8通り考えられます。

テストアプリを、前の「actionとcategory、コンテントプロバイダ対応でないdata及びtype」と同じ手順で作成します。

 

具体的なデータ

action   "INTENT_TEST_1_FAKE_ACTION"

category   "INTENT_TEST_1_CATEGORY"

#

記号

1

u

content://com.android.contacts/contacts

2

v

content://com.android.contacts/wrong/contacts

3

f(u)

vnd.android.cursor.dir/contact

4

t

vnd.android.cursor.dir/wrong.contact

注u,f(u)はそれぞれ、ContactsContract.ContactsのCONTENT_URI、及びCONTENT_TYPEの値です。

   actionはINTENT_TEST_1_FAKE_ACTIONです。categoryはDEFAULTだけです。

 

①filter名に対応するintent-filterを作成し、そのintent-filterを持つActivityをAndroidManifestに登録します。クラス名は、Activity#_filter名(#のところは2桁の番号)にすると便利です。

②mainのActivityを作ります。これは、上記のintent-filterを持つActivityとは別です。mainに必要なactionとcategoryを付けます。EclipseのAndroidプロジェクトで自動生成されるActivityをベースにするといいです。

③mainのActivityの画面に必要な数のボタンを配置して、それをクリックするとintent-filterを持つActivityを起動する(startActivity()呼び出し)処理を追加します。ボタンのid名はfilter名と同じにします。

④intent-filterを持つActivity(Activity#_filter名)の画面はすべて同じとします。EclipseのAndroidプロジェクトで自動生成されるActivityをベースにして、onCreateの中でTextViewに自身のクラス名を書き出すようにします。クラス名は、getClass().getSimpleName()で取得できますので、画面を定義するlayoutのxmlはすべて共通にできます。

 

付録

付録 ソースコード

Intentの演習は、Intentの仕様の確認を兼ねていますので、テストに使ったソースコードを掲載します。

本文中では、演習を二つに分けていますが、一つのアプリとして実装しています。二つの演習の各々に対して一つの主Activityを作成し、そこからサブActivityを起動しています。サブActivityが本文で説明したintent-filterを持つActivityです。その選択は主Activityのボタンで行います。なお、ここの主ActivityはactionがMAINであることを意味していません。

 

 

 

 

 

 

 

 


AndroidManifest.xml

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

      package="com.example.intent"

      android:versionCode="1"

      android:versionName="1.0">

    <application android:icon="@drawable/icon" android:label="@string/app_name">

        <activity android:name=".HelloIntent_0_1"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name=".HelloIntent_0_1a"

                  android:label="@string/app_name">

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <!-- 1 -->

        <activity android:name=".Activity01_action"

                  android:label="action">

            <intent-filter>

                <action android:name="INTENT_TEST_1_ACTION" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- 2 -->

        <activity android:name=".Activity02_cat"

                  android:label="category">

            <intent-filter>

                <action android:name="INTENT_TEST_1_CAT_ACTION" />

                <category android:name="INTENT_TEST_1_CATEGORY" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- 3 -->

        <activity android:name=".Activity03_urig"

                  android:label="uri for general contents">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="my_scheme" android:host="jp.co.ichi" android:path="/intent/test"/>

                <!--  path MUST starts with /. the other MUST NOT start with /. -->

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- 4 -->

        <activity android:name=".Activity04_typeg"

                  android:label="MIME type for general contents">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:mimeType="text/plain" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- 5 -->

        <activity android:name=".Activity05_urig_typeg"

                  android:label="uri and MIME type for general contents">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="my_scheme" android:host="jp.co.ichi" android:path="/intent/test"

                 android:mimeType="text/plain" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- 6 -->

        <activity android:name=".Activity06_partial_urig"

                  android:label="partial_uri for general contents">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="our_scheme" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

<!-- =======================

      cp: content provider

     ======================= -->

        <!-- cp1 -->

        <activity android:name=".ActivityCP01_uric"

                  android:label="uri for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/contacts"/>

                <!--  path MUST starts with /. the other MUST NOT start with /. -->

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp2 -->

        <activity android:name=".ActivityCP02_typec"

                  android:label="MIME type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:mimeType="vnd.android.cursor.dir/contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp3 -->

        <activity android:name=".ActivityCP03_uric_typec"

                  android:label="uri and MIME type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/contacts"

                 android:mimeType="vnd.android.cursor.dir/contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp4 -->

        <activity android:name=".ActivityCP04_uric_wrong_typec"

                  android:label="uri and wrong MIME type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/contacts"

                 android:mimeType="vnd.android.cursor.dir/wrong.contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp5 -->

        <activity android:name=".ActivityCP05_wrong_uric_typec"

                  android:label="wrong uri and MIME type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/wrong/contacts"

                 android:mimeType="vnd.android.cursor.dir/contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp6 -->

        <activity android:name=".ActivityCP06_wrong_uric"

                  android:label="wrong uri for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/wrong/contacts" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp7 -->

        <activity android:name=".ActivityCP07_wrong_typec"

                  android:label="wrong MIME type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:mimeType="vnd.android.cursor.dir/wrong.contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

        <!-- cp8 -->

        <activity android:name=".ActivityCP08_wrong_uric_wrong_typec"

                  android:label="wrong uri and wrong type for provider">

            <intent-filter>

                <action android:name="INTENT_TEST_1_FAKE_ACTION" />

                <data android:scheme="content" android:host="com.android.contacts" android:path="/wrong/contacts"

                 android:mimeType="vnd.android.cursor.dir/wrong.contact" />

                <category android:name="android.intent.category.DEFAULT" />

            </intent-filter>

        </activity>

    </application>

</manifest>

 

main.xml

<?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"

    >

<TextView 

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

<!-- 1 -->

<Button

    android:id="@+id/btn_action"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:action"

    />

<!-- 2 -->

<Button

    android:id="@+id/btn_cat"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:category"

    />

<!-- 3 -->

<Button

    android:id="@+id/btn_urig"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:uri for general contents"

    />

<!-- 4 -->

<Button

    android:id="@+id/btn_typeg"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:type for general contents"

    />

<!-- 5 -->

<Button

    android:id="@+id/btn_urig_typeg"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:uri and type for general contents"

    />

<!-- 6 -->

<Button

    android:id="@+id/btn_partial_urig"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:partial uri for general contents"

    />

<!-- 7 -->

<!-- setData()、setType()の順でIntentに設定 -->

<Button

    android:id="@+id/btn_urig_then_typeg"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:uri then type for general contents"

    />

<!-- 8 -->

<!-- setType()、setData()の順でIntentに設定 -->

<Button

    android:id="@+id/btn_typeg_then_urig"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:type then uri for general contents"

    />

<!-- to next -->

<Button

    android:id="@+id/btn_to_next"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="go to next!"

    />

</LinearLayout>

 

main_a.xml

<?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"

    >

<!-- =======================

      cp: content provider

     ======================= -->

<!-- cp 1 -->

<Button

    android:id="@+id/btn_uric"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:uri for provider"

    />

<!-- cp 2 -->

<Button

    android:id="@+id/btn_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:type for provider"

    />

<!-- cp 3 -->

<Button

    android:id="@+id/btn_uric_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:uri and type for provider"

    />

<!-- cp 4 -->

<!-- 指定したuriから演繹されるtypeと異なるtypeを指定する。-->

<Button

    android:id="@+id/btn_uric_wrong_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:right uri and wrong type for provider"

    />

<!-- cp 5 -->

<!-- 対応するコンテントプロバイザが存在しないuriと対応するコンテントプロバイザが存在するtype -->

<Button

    android:id="@+id/btn_wrong_uric_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:wrong uri and right type for provider"

    />

<!-- cp 6 -->

<!-- 対応するコンテントプロバイザが存在しないuriだけ -->

<Button

    android:id="@+id/btn_wrong_uric"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:wrong uri for provider"

    />

<!-- cp 7 -->

<!-- 対応するコンテントプロバイザが存在しないtypeだけ -->

<Button

    android:id="@+id/btn_wrong_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:wrong type for provider"

    />

<!-- cp 8 -->

<!-- 共に対応するコンテントプロバイザが存在しないuriとtype -->

<Button

    android:id="@+id/btn_wrong_uric_wrong_typec"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="filter:wrong uri and wrong type for provider"

    />

 

<!-- to prev -->

<Button

    android:id="@+id/btn_to_prev"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="go back to prev!"

    />

</LinearLayout>

 

sub.xml

<?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"

    >

<TextView 

    android:id="@+id/tv"

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="@string/hello"

    />

</LinearLayout>

 

Activity_0_1

package com.example.intent;

 

import android.app.Activity;

import android.content.Context;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class HelloIntent_0_1 extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        

        View.OnClickListener listener = new OnClickListener();

        // 1 action

        Button btnAction = (Button)findViewById(R.id.btn_action);

        btnAction.setOnClickListener(listener);

        // 2 catalog

        Button btnCat = (Button)findViewById(R.id.btn_cat);

        btnCat.setOnClickListener(listener);

        // 3 uri for general contents

        Button btnUriG = (Button)findViewById(R.id.btn_urig);

        btnUriG.setOnClickListener(listener);

        // 4 type for general contents

        Button btnTypeG = (Button)findViewById(R.id.btn_typeg);

        btnTypeG.setOnClickListener(listener);

        // 5 uri and type for general contents

        Button btnUriTypeG = (Button)findViewById(R.id.btn_urig_typeg);

        btnUriTypeG.setOnClickListener(listener);

        // 6 partial uri for general contents

        Button btnPartialUriG = (Button)findViewById(R.id.btn_partial_urig);

        btnPartialUriG.setOnClickListener(listener);

        // 7 uri and type for general contents

        Button btnUriThenTypeG = (Button)findViewById(R.id.btn_urig_then_typeg);

        btnUriThenTypeG.setOnClickListener(listener);

        // 7 uri and type for general contents

        Button btnTypeThenUriG = (Button)findViewById(R.id.btn_typeg_then_urig);

        btnTypeThenUriG.setOnClickListener(listener);

 

        Button btnToNext = (Button)findViewById(R.id.btn_to_next);//  goto prev

        btnToNext.setOnClickListener(listener);

    }

    private class OnClickListener implements View.OnClickListener{

        @Override

        public void onClick(View v) {

            Log.d("HelloIntent_0_1","********onClick starts.");

            int id = v.getId();

            Intent intent;

            switch(id){

            case R.id.btn_action:{ // 1 action

                Log.d("HelloIntent_0_1","btn_action is clicked.");

                intent = new Intent();

                intent.setAction("INTENT_TEST_1_ACTION");

                startActivity(intent);

                break;

            }

            case R.id.btn_cat:{// 2 category

                Log.d("HelloIntent_0_1","btn_cat is clicked.");

                intent = new Intent();

                intent.setAction("INTENT_TEST_1_CAT_ACTION");

                intent.addCategory("INTENT_TEST_1_CATEGORY");

                startActivity(intent);

                break;

            }

            case R.id.btn_urig:{ // 3 content providerでないuriの両方を指定した場合

                Log.d("HelloIntent_0_1","btn_urig is clicked.");

                String urlString = "my_scheme://jp.co.ichi/intent/test";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setData(uri);

                startActivity(intent);

                break;

            }

            case R.id.btn_typeg:{ // 4 content providerでないMIME type指定した場合

                Log.d("HelloIntent_0_1","btn_typeg is clicked.");

                intent = new Intent();

                intent.setType("text/plain");

                startActivity(intent);

                break;

            }

            case R.id.btn_urig_typeg:{ // 5 content providerでないMIME typeとuriの両方を指定した場合

                Log.d("HelloIntent_0_1","btn_urig_typeg is clicked.");

                String urlString = "my_scheme://jp.co.ichi/intent/test";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setDataAndType(uri, "text/plain");

                startActivity(intent);

                break;

            }

            case R.id.btn_partial_urig:{ // 6 content providerでないuriのスキーマだけを指定した場合

                Log.d("HelloIntent_0_1","btn_partial_urig is clicked.");

                String urlString = "our_scheme://jp.co.ichi";

                //スキーマだけ指定する場合は://まで記述しなければならない

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setData(uri);

                startActivity(intent);

                break;

            }

            case R.id.btn_urig_then_typeg:{ // 7 setData()、setType()の順でIntentに設定

                Log.d("HelloIntent_0_1","btn_urig_typeg_separately is clicked.");

                String urlString = "my_scheme://jp.co.ichi/intent/test";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setData(uri);

                intent.setType("text/plain");

                startActivity(intent);

                break;

            }

            case R.id.btn_typeg_then_urig:{ // 8 setType()、setData()の順でIntentに設定

                Log.d("HelloIntent_0_1","btn_urig_typeg_separately is clicked.");

                String urlString = "my_scheme://jp.co.ichi/intent/test";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setType("text/plain");

                intent.setData(uri);

                startActivity(intent);

                break;

            }

            case R.id.btn_to_next:{ // ボタンが多いので2分割

                intent = new Intent(HelloIntent_0_1.this,HelloIntent_0_1a.class);

                startActivity(intent);

                break;

            }

            default:{

                Log.d("HelloIntent_0_1","nothing matches to what is clicked.");

                Toast.makeText(HelloIntent_0_1.this,"Not Matched.", Toast.LENGTH_LONG).show();

            }

            }// end of switch

        }

    }

}

 

Activity_0_1a

package com.example.intent;

 

import android.app.Activity;

import android.content.Intent;

import android.net.Uri;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.Toast;

 

public class HelloIntent_0_1a extends Activity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main_a);

       

        View.OnClickListener listener = new OnClickListener();

        // cp 1 uri for content provider

        Button btnUriC = (Button)findViewById(R.id.btn_uric);

        btnUriC.setOnClickListener(listener);

        // cp2 uri for content provider

        Button btnTypeC = (Button)findViewById(R.id.btn_typec);

        btnTypeC.setOnClickListener(listener);

        // cp3 uri and type for content provider

        Button btnUriTypeC = (Button)findViewById(R.id.btn_uric_typec);

        btnUriTypeC.setOnClickListener(listener);

        // cp4 uri and type for content provider wrongly

        Button btnUriWrongTypeC = (Button)findViewById(R.id.btn_uric_wrong_typec);

        btnUriWrongTypeC.setOnClickListener(listener);

        // cp5 uri and type for content provider wrongly

        Button btnWrongUriTypeC = (Button)findViewById(R.id.btn_wrong_uric_typec);

        btnWrongUriTypeC.setOnClickListener(listener);

        // cp6 対応するcontent providerが存在しないuriだけ指定した場合

        Button btnWrongUri = (Button)findViewById(R.id.btn_wrong_uric);

        btnWrongUri.setOnClickListener(listener);

        // cp7 対応するcontent providerが存在しないMIME typeだけ指定した場合

        Button btnWrongTypeC = (Button)findViewById(R.id.btn_wrong_typec);

        btnWrongTypeC.setOnClickListener(listener);

        // cp8 対応するcontent providerが存在しないuriとMIME typeの両方を指定した場合

        Button btnWrongUriWrongTypeC = (Button)findViewById(R.id.btn_wrong_uric_wrong_typec);

        btnWrongUriWrongTypeC.setOnClickListener(listener);

 

        Button btnToPrev = (Button)findViewById(R.id.btn_to_prev);//  goto prev

        btnToPrev.setOnClickListener(listener);

    }

    private class OnClickListener implements View.OnClickListener{

        @Override

        public void onClick(View v) {

            Log.d("HelloIntent_0_1a","********onClick starts.");

            int id = v.getId();

            Intent intent;

            switch(id){

            case R.id.btn_uric:{ // cp1 content providerのuriだけ指定した場合

                Log.d("HelloIntent_0_1","btn_uric is clicked.");

                String urlString = "content://com.android.contacts/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setData(uri);

                startActivity(intent);

                break;

            }

            case R.id.btn_typec:{ // cp2 content providerのMIME typeだけ指定した場合

                Log.d("HelloIntent_0_1","btn_typec is clicked.");

                intent = new Intent();

                intent.setType("vnd.android.cursor.dir/contact");

                startActivity(intent);

                break;

            }

            case R.id.btn_uric_typec:{ // cp3 content providerのuriとMIME typeの両方を指定した場合

                Log.d("HelloIntent_0_1","btn_uri_typec is clicked.");

                String urlString = "content://com.android.contacts/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setDataAndType(uri, "vnd.android.cursor.dir/contact");

                startActivity(intent);

                break;

            }

            case R.id.btn_uric_wrong_typec:{ // cp4 content providerのuriとMIME typeの両方を指定するが、MIME typeはuriから演繹できる値と異なる場合

                Log.d("HelloIntent_0_1a","btn_uri_wrong_typec is clicked.");

                String urlString = "content://com.android.contacts/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setDataAndType(uri, "vnd.android.cursor.dir/wrong.contact");

                startActivity(intent);

                break;

            }

            case R.id.btn_wrong_uric_typec:{ // cp5 content providerのuriとMIME typeの両方を指定するが、uriに対応するコンテントプロバイザが存在しない場合

                Log.d("HelloIntent_0_1a","btn_wrong_uri_typec is clicked.");

                String urlString = "content://com.android.contacts/wrong/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setDataAndType(uri, "vnd.android.cursor.dir/contact");

                startActivity(intent);

                break;

            }

            case R.id.btn_wrong_uric:{ // cp6 対応するcontent providerが存在しないuriだけ指定した場合

                Log.d("HelloIntent_0_1","btn_wrong_uric is clicked.");

                String urlString = "content://com.android.contacts/wrong/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setData(uri);

                startActivity(intent);

                break;

            }

            case R.id.btn_wrong_typec:{ // cp7 対応するcontent providerが存在しないMIME typeだけ指定した場合

                Log.d("HelloIntent_0_1","btn_wrong_typec is clicked.");

                intent = new Intent();

                intent.setType("vnd.android.cursor.dir/wrong.contact");

                startActivity(intent);

                break;

            }

            case R.id.btn_wrong_uric_wrong_typec:{ // cp8 対応するcontent providerが存在しないuriとMIME typeの両方を指定した場合

                Log.d("HelloIntent_0_1","btn_wrong_uri_wrong_typec is clicked.");

                String urlString = "content://com.android.contacts/wrong/contacts";

                Uri uri = Uri.parse(urlString);

                intent = new Intent();

                intent.setDataAndType(uri, "vnd.android.cursor.dir/wrong.contact");

                startActivity(intent);

                break;

            }

 

            case R.id.btn_to_prev:{ // ボタンが多いので2分割

                intent = new Intent(HelloIntent_0_1a.this,HelloIntent_0_1.class);

                startActivity(intent);

                break;

            }

            default:{

                Log.d("HelloIntent_0_1a","nothing matches to what is clicked.");

                Toast.makeText(HelloIntent_0_1a.this,"Not Matched.", Toast.LENGTH_LONG).show();

            }

            }// end of switch

        }

    }

}

 

Activity#_filter  (Activity01_actionの例)

package com.example.intent;

 

import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

 

public class Activity01_action extends Activity {

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.sub);

        TextView tv = (TextView)findViewById(R.id.tv);

        tv.setText(getClass().getSimpleName());

    }

}

 

以上