AndroidでWebブラウザのリンクをクリックするとDelphiアプリケーションを起動する方法を紹介します。
Delphiアプリケーションの作成
アプリケーションが起動したときに、リンクのURLを取得してブラウザコンポーネントに表示します。
procedure TForm1.FormCreate(Sender: TObject);
var
Intent: JIntent;
Uri: JString;
begin
Intent := SharedActivity.getIntent;
if TJIntent.JavaClass.ACTION_VIEW.equals(Intent.getAction) then
begin
Uri := Intent.getDataString;
FWebBrowser.URL := JStringToString(Uri);
end;
end;
AndroidManifest.template.xmlの編集
AndroidManifest.xmlにインテントフィルタを設定します。
プロジェクトファイルのあるフォルダーに「AndroidManifest.template.xml」というファイルがあります。
この「AndroidManifest.template.xml」をテキストエディタで開きます。
次のコードを追加します。
<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="http" android:host="www.gesource.jp" android:path="/" />
</intent-filter>
dataタグで、アプリケーションを起動するリンクを設定しています。
「https://www.gesource.jp/」のリンクをクリックしたとき、Delphiアプリケーションでリンクを開くことができます。
リンクは「http」に限られているわけではありません。
dataタグを次のようにすると、「test://hello」のリンクをクリックしたときに、アプリケーションを起動します。
<data android:scheme="test" android:host="hello" />