はじめに
前回はターミナルからadbコマンドでアプリを開いてみた。前回記事
今回はリンクをタップするとアプリが開く実装をする。
インテントリンクについて
Androidではインテントリンクを使うとWebページから直接アプリを起動できる。
また、タップした時点でアプリがインストールされてなければPlayStoreに遷移することも可能。
構文
インテントリンクを作成するには以下の構文に沿って作成する必要がある。
※ 全て設定する必要はない。
intent:
HOST/URI-path
#Intent;
package=\[string\];
action=\[string\];
category=\[string\];
component=\[string\];
scheme=\[string\];
end;
作成手順
①Manifestファイルの設定
遷移させたいアクティビティにaction,category,scheme,hostの設定を記述する。
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bookmanager_android">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
<!-- application,その他activityの記述は省略 -->
<activity
android:name=".AdditionalBookActivity"
android:label="@string/page_title_additional"
android:exported="true">
<!-- 遷移させたいactivityに下記を記述する -->
<intent-filter>
<action android:name="android.intent.action.VIEW"/>
<category android:name="android.intent.category.DEFAULT"/>
<category android:name="android.intent.category.BROWSABLE"/>
<data android:scheme="bookapp" android:host="book"/>
</intent-filter>
</activity>
</application>
</manifest>
②Manifestファイルをもとにintent構文を作成(②は飛ばしてもよい)
intent:
book
#Intent;
package=com.example.bookmanager_android;
scheme=bookapp;
end;
③intent構文をもとに作成したaタグを埋め込む
<a href="intent://book#Intent;package=com.example.bookmanager_android;action=view;scheme=bookapp;end;">book</a>
intent構文をつなげたものをaタグにし、リンク化してくれるアプリ等で試す。
今回はplaycodeを利用した。 https://playcode.io/
参考サイト
https://developer.chrome.com/docs/multidevice/android/intents/
https://developer.android.com/guide/components/intents-filters.html