iT邦幫忙

2025 iThome 鐵人賽

DAY 18
0

Deep Link 是 IPC 的機制 允許使用者點URL的方式 系統會打開應用程式 並導向特定APP的內容(Activity) 如果使用者未安裝該APP 開發者會透過程式方式 會導向到APP Store 頁面
Android 支援兩種型態的Deep Link 分別為 Standard Deep Link 另一種是 Android APP link

標準 Deep Link

在網頁中 嵌入Deep Link 當使用者點擊 Android 會自動找到該APP 與 Activity

<div>
	<p>Buy our latest PC parts.</p>
	<a href="app://myapp/products/cpu"> </a>
</div>

APP 需要先設定好接收方式 也就是intent filter 來接收Action

<activity android:name=".ProductsActivity">
    <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="app"
              android:host="myapp"
              android:pathPrefix="/products/" />
    </intent-filter>
</activity>

一但點擊符合interFilter檢查 ProductsActivity 這個Activity 就會被啟動
android:scheme -> app:// (這裡可以自訂義)
Android:host -> myapp (這裡可以自訂義)
Andorid:pathPrefix -> /products (這裡就只匹配包含這個部分url)

設定好intent後 我們就要處理 Activity 要怎麼去處理這個傳進來的intent

public class ProductActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_planet);

        Intent intent = getIntent();
        String action = intent.getAction();
        Uri data = intent.getData();

        if (Intent.ACTION_VIEW.equals(action) && data != null) {
            String ProductName = data.getLastPathSegment();
          
          	if (ProductName.equals("cpu")) {
            	// Do something. For example, query the database for information on this product.
            }
        }
    }
}

Stand Deep Link 有個問題是 URI 的schemes 與 path 都是自訂的 任何應用程式都可以透過 聲明處理相同的 scheme 和 host,導致惡意應用程式可能攔截 Deep Link。 建議使用 Android App Link

Android App Link

Android App Link 使用 HTTPS 協議

<div>
	<p>Buy our latest PC parts.</p>
	<a href="https://www.myapp.com/products/cpu"> </a>
</div>

Androidmanifest.xml

<activity android:name=".ProductsActivity">
    <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="https"
              android:host="www.myapp.com"
              android:pathPrefix="/products/" />
    </intent-filter>
</activity>

如果未安裝Deep link的應用程序,則該鏈接將在Web瀏覽器中打開 並且應用程式與網站透過 assetlinks.json 建立關聯,確保只有指定的應用程式(透過包名和簽名指紋)處理 https://www.myapp.com 的連結

[{
    "relation": ["delegate_permission/common.handle_all_urls"],
    "target": {
        "namespace": "android_app",
        "package_name": "com.example.myapp",
        "sha256_cert_fingerprints": ["SHA256_FINGERPRINT"]
    }
}]

此外Android APP link 也要驗證參數的輸入並避免傳遞敏感的參數


上一篇
DAY 17 Binders
下一篇
DAY 19 Disassemble APK - Manifest
系列文
我是不可能一直學Android 絕對不會20
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言