LoginSignup
1
2

More than 5 years have passed since last update.

暗黙的Intent(既存のアプリケーションの実行)

Posted at

あるアプリケーションからAndroid端末にインストールされている既存のアプリケーションを実行することができる。
一般的に、インストールされているアプリケーションではクラス名などの情報は不明なので、暗黙的Intentを利用する。

Webブラウザの実行

ここでは例として、Webブラウザを使って指定したURLのページを表示するアプリケーションを作成する。

以下は、メインとなるActivityのコードである。

        //ボタンを取得
        Button button = (Button)findViewById(R.id.button);

        //EditTextから文字列を取得
        EditText editText = (EditText)findViewById(R.id.editText);
        final String text = editText.getText().toString();

        //ボタンがクリックされた時の処理
        button.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                //文字列からUri型の変数を作成
                Uri uri = Uri.parse(text);

                //実行するActivityとuriをIntentに設定
                //URIを使って何かを表示させたい場合、Intent.ACTION_VIEWを使う
                Intent intent = new Intent(Intent.ACTION_VIEW,uri);

                //Activityを実行
                startActivity(intent);
            }
        });

ポイントはintentのコンストラクタで、
第一引数に実行するActivityを決めるための文字列を、
第二引数にuriを設定している。
今回使ったACTION_VIEWは、uriの種類に応じて次のアプリケーションを起動する。

URIの種類 実行されるアプリケーション
http: Webブラウザ
https: Webブラウザ
tel 電話をかけるアプリケーション
mailto: メール用アプリケーション

GitHubリンク

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2