LoginSignup
1
5

More than 5 years have passed since last update.

AndroidアプリからHTMLViewerを呼び出す方法

Last updated at Posted at 2017-08-27

はじめに

Webブラウザを使うアプリで、いちいちレイアウトを作ったり、コードを組むのは、若干無駄と言える。
そのときは、Android内蔵のHTMLビューアを使うと、簡易ブラウザ機能が使える。

コード

まず、HTMLViewerを起動するには、インテントで指定する必要がある。
以下のコードをonCreateの中に挿入しよう。

MainActivity.java
 Intent intent = new Intent(Intent.ACTION_MAIN);
 intent.setAction("android.intent.action.main");
 intent.setClassName("com.android.htmlviewer","com.android.htmlviewer.HTMLViewerActivity");
 intent.setData(Uri.parse("http://qiita.com/"));
 startActivity(intent);

これが起動するコードだ。まず、new Intent()でインテントを作成する。
次に、setActionでメインアクションのみにフィルターを指定する。
setClassNameで、第1パラメータにパッケージ名、次にパッケージ名とアクティビティ名を指定する。
最後に、HTMLViewerでは、プログラムでインテントからgetData()を実行しているので、そこにURLをセットする。URLはUriクラスのparseメソッドを経由して指定する。
そして、startActivity()でアクティビティを起動。
これでHTMLViewerを起動できる。URLには、fileプロトコルで指定することもできる。

今後のアプリ開発に活用しよう。

1
5
1

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
5