コンピュータクワガタ

かっぱのかっぱによるコンピュータ関連のサイトです

Androidアプリ入門 No.55 標準アクション ACTION_VIEW

標準アクション

ACTION_VIEW

データを表示するためのACTION。データには以下のようなものが指定できる。色々なデータが表示できる。

URI 説明
http://Webサイト
https://Webサイト
http://www.google.co.jp 指定のWebサイトをブラウザで表示する。
tel:電話番号 tel:117 ダイヤル画面を指定の番号で表示する。
mailto:メールアドレス test@example.com メーラを指定のメールアドレスで起動する。
content://contacts/people/番号 content://contacts/people/1 指定の番号のコンタクトリスト(アドレス帳)を表示する。
geo: latitude,longitute geo:0,0?q=Kanazawa 指定の座標の地図を表示する。

また、mailtoではextraも指定できる。具体的には以下が指定できる。仕様上指定ができるのかはよくわからないが、試してみたところSPモードメールでは以下の設定はすべてできなかった。Gmailでは、SUBJECTとTEXTは反映された。

extra 説明
EXTRA_CC CC。
EXTRA_BCC BCC
EXTRA_SUBJECT メールタイトル。
EXTRA_TEXT メール本文。

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">
    <Button
        android:id="@+id/httpButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="http://" />
    <Button
        android:id="@+id/mailtoButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="mailto:" />
    <Button
        android:id="@+id/telButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="tel:" />
    <Button
        android:id="@+id/contentButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="content:" />
    <Button
        android:id="@+id/geo1Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="geo1:" />
    <Button
        android:id="@+id/geo2Button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="geo2:" />
</LinearLayout>

次に、MainActivity.javaを示す。

package sample.it;

import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity {
    private Button httpButton;
    private Button mailtoButton;
    private Button telButton;
    private Button contentButton;
    private Button geo1Button;
    private Button geo2Button;

    private Map<View, Uri> actionMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        httpButton = (Button) findViewById(R.id.httpButton);
        mailtoButton = (Button) findViewById(R.id.mailtoButton);
        telButton = (Button) findViewById(R.id.telButton);
        contentButton = (Button) findViewById(R.id.contentButton);
        geo1Button = (Button) findViewById(R.id.geo1Button);
        geo2Button = (Button) findViewById(R.id.geo2Button);

        httpButton.setOnClickListener(ocl);
        mailtoButton.setOnClickListener(ocl);
        telButton.setOnClickListener(ocl);
        contentButton.setOnClickListener(ocl);
        geo1Button.setOnClickListener(ocl);
        geo2Button.setOnClickListener(ocl);

        actionMap = new HashMap<View, Uri>();
        actionMap.put(httpButton, Uri.parse("http://www.google.co.jp"));
        actionMap.put(mailtoButton, Uri.parse("mailto:test@example.com"));
        actionMap.put(telButton, Uri.parse("tel:117"));
        actionMap.put(contentButton, Uri.parse("content://contacts/people/1"));
        actionMap.put(geo1Button, Uri.parse("geo:0,0?q=Kanazawa"));
        actionMap.put(geo2Button, Uri.parse("geo:35.41,139.45"));
    }

    OnClickListener ocl = new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.setData(actionMap.get(v));
            if (v == mailtoButton) {
                intent.putExtra(Intent.EXTRA_CC, "testcc1@example.com");
                intent.putExtra(Intent.EXTRA_CC, "testcc2@example.com");
                intent.putExtra(Intent.EXTRA_BCC, "testbcc@example.com");
                intent.putExtra(Intent.EXTRA_SUBJECT, "サブジェクト");
                intent.putExtra(Intent.EXTRA_TEXT, "本文\n本文");
                Toast.makeText(getApplicationContext(), "メール", Toast.LENGTH_SHORT).show();
            }
            startActivity(intent);
        }
    };
}

実行結果は以下。

geo1:ボタンを押して「Kanazawa」を検索した結果は以下のようになる。Google Mapできちんと表示されるのが分かる。

エミュレータだと、メールや電話は見られないため実機で確認した方がいい。