LoginSignup
62
63

More than 5 years have passed since last update.

Androidでちゃんと明示的にブラウザでIntentを呼ぶ方法

Last updated at Posted at 2013-11-29

例えばアプリ内で他アプリを呼び出す方法としてスキーマを使って呼び出す方法がある。

Lineアプリでメッセージを送る
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("line://msg/text/" + shareText)); 
try {
    ((Activity) ct).startActivityForResult(intent, 0);  
} catch (Exception e) {
    // Lineが入ってない場合
}

みたいな感じ。これにはいろいろな使い方があって

マップを起動
intent = new Intent(Intent.ACTION_VIEW,Uri.parse("geo:0,0?q=Osaka"));
マーケットでアプリを検索
intent = new Intent(Intent.ACTION_VIEW, Uri.parse("market://search?q=pname:goodMorningTweet"));
アプリを削除
intent = new Intent(Intent.ACTION_DELETE, Uri.fromParts("package", <削除したいアプリケーションのパッケージ名>, null));

などなど
Intent連携をまとめてみる

よく知られているところのインテントの扱いは2種類に分けて解釈されていて、明示的・暗黙的と呼ばれている。

明示的

こちらで指定した特定のアクティビティを起動

明示的インテント
Intent intent = new Intent(this, NanikashiraActivity.class);
startActivity(intent);

暗黙的

「処理」だけしていして、どのアクティビティにやらせるかはユーザーに指定させる。
例えばここではアプリ内でTweetをすると、つぶやき機能を持つアプリの選択画面が出て
どのアプリでつぶやくか選べる。

暗黙的インテント
Uri uri = Uri.parse(shareTweetUrl);
Intent i = new Intent(Intent.ACTION_VIEW,uri);
startActivity(intent);

アプリでこれを受けるようにするにはManufestにごにょごにょ書く
Intent - Androidの肝

でも他のアプリではなく、明示的に標準のブラウザで開いてほしい時とかがある。

これは以下のようにする。

標準ブラウザを明示的に呼び出しリンクを表示

これは結構簡単でClassNameを指定するといい

標準のブラウザでつぶやく
Intent i = new Intent(Intent.ACTION_VIEW,uri);
i.setClassName("com.android.browser", 
                "com.android.browser.BrowserActivity"); 

これにより明示的に標準のブラウザで起動できる〜知らなかった!!

62
63
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
62
63