Androidのディープリンクテクノロジー

序文

日常生活では、ブラウザのWebページまたは他のAPPから直接APPを開く必要があり、ディープリンクテクノロジーを使用する必要があります。実装方法は、それぞれディープリンクとAPPリンクです。

ディープリンク

ディープリンクは、アプリの指定されたページを開くためにGoogleがサポートする方法であり、H5ページからアプリのターゲットページにジャンプするためによく使用されます。指定されたページに対応する一致ルールは、URIに従って一致します。一般的なURI形式は次のとおりです。

image.png

  1. H5テストページ
<html>
<a href="http://demo.deaven.com:2003/test/data?params1=value1&params2=value2">点击唤起app</a>
<a href="https://demo.deaven.com:2003/test/data?params1=value1&params2=value2">点击唤起app</a>
<a href="abc://demo.deaven.com:2003/test/data?params1=value1&params2=value2">点击唤起app</a>
</html>
复制代码

上記のように

  • スキーム=http、https、abc。DeepLinkのスキームはカスタマイズできます
  • ホスト=demo.deaven.com
  • ポート=2003
  • パス=テスト/データ
  • パスパラメータ(キー値):params1:value1 params2:value2
  1. Androidの構成
 <activity android:name=".MainActivity"
           android:exported="true"
           android:launchMode="singleTask">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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" /> 
                <data android:scheme="https" />
                <data android:scheme="abc" />
                <data android:host="demo.deaven.com"/>
                <data android:port="2003"/>
                <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
                <!-- 注意 "/" 在pathPrefix中是必须的-->
                <data android:pathPrefix="/test"/>

            </intent-filter>
        </activity>
复制代码

3.アクティビティのインテントを解析します

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Uri uri = getIntent().getData();
    String scheme = uri.getScheme(); // http、https、abc
    String host = uri.getHost(); // demo.deaven.com
    String path = uri.getPath(); // test/data
    String query = uri.getQuery(); // params1=value1&params2=value2
    String value1 = uri.getQueryParameter("params1"); 
    String value2 = uri.getQueryParameter("params2");
}
复制代码

管理とユーザーエクスペリエンスを向上させるために、アプリで中間ページを宣言して、パラメーターに従ってジャンプリクエストを均一に分散させることができます。

予防

  1. スキームはhttp/httpsで始まるURIです。一部のブラウザとモバイルROMはAPPにリンクできませんが、ブラウザで対応するリンクを開きます。したがって、ディープリンクを実行するときはカスタムスキームの形式を使用することをお勧めします。

  2. APPを使用して対応するリンクを開くかどうかを尋ねるときに、[キャンセル]が選択され、[選択を記憶する]がオンになっている場合、次にAPPに再度リンクするときは、何も起こりません!!!

  3. 同じインテントフィルターに異なるホストを記述しないでください。一致するルールごとに新しいインテントフィルターを作成することをお勧めします。

アプリのリンク

AndroidはAndroid6.0(APIレベル23)以降にアプリリンクを追加しました。ユーザーが対応するURIをクリックすると、対応するアプリが直接起動され、ディープリンクでアプリを開くかどうかのようなダイアログボックスが表示されなくなります。 。

インテントフィルター

 <activity android:name=".MainActivity"
           android:exported="true"
           android:launchMode="singleTask"
           android:autoVerify="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <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" /> 
                <data android:scheme="https" />
                <data android:host="demo.deaven.com"/>
                <data android:port="2003"/>
                <!--表示匹配 Path 以/test 开头的uri,此项可以不写-->
                <!-- 注意 "/" 在pathPrefix中是必须的-->
                <data android:pathPrefix="/test"/>

            </intent-filter>
        </activity>
复制代码
  • インテントフィルターはディープリンクに似ていますが、スキームはhttpまたはhttpsのみを使用でき、カスタムスキームをサポートしていません

  • android:autoVerify = "true"は、リストされたホストでAPPを自動的に検証します。検証が成功すると、APPは一致するURIのデフォルトのオープンメソッドになります。

Assetlinks.jsonを構成します

  1. 以下に示すように、 developers.google.com / digital-ass ...にアクセスして、assetlinks.jsonを生成できます。

image.png

壁を越えられない場合は、以下のコードをコピーして独自のパラメーターに変更し、assetlinks.jsonファイルを生成できます。jsonファイル名はassetlinksのみであり、カスタマイズすることはできません。

[{
  "relation": ["delegate_permission/common.handle_all_urls"],
  "target" : { "namespace": "android_app", "package_name": "com.deaven.link",
               "sha256_cert_fingerprints": [""14:6D:E9:83:C5:73:06:50:D8:EE:B9:95:2F:34:FC:64:16:A0:83:42:E6:1D:BE:A8:8A:04:96:B2:3F:CF:44:E5""] }
}]
复制代码

2.assetlinks.jsonをデプロイします

ホストはdemo.deaven.comであるため、https://にassetlinks.jsonを配置する必要があります。demo.deaven.com/.well-known/assetlinks.jsonであり、通常どおりアクセスできます。以下に示すように、 developers.google.com /digital-ass…のサーバーでassetlinks.jsonにアクセスできるかどうかを確認することもできます。

image.png

3.ディープリンクと同様のアクティビティでインテントを解析します

リファレンスドキュメント

www.jianshu.com/p/1632be1c2…developer.android.com/training/ap…developer.android.com/training/ap… _ _

おすすめ

転載: juejin.im/post/7077831040705757214
おすすめ