天天看點

Android 自定義URL Scheme

在從URI看Mobile和Web資料一文中,我們講到了Android的設計者們在Activity 中引入了URI,即通過自定義URL Scheme來定位一個頁面,外部和内部應用可以通過一個uri非常友善的定位到Android的各個頁面,文章轉載自Android業務元件化之URL Scheme使用,感謝總李寫代碼

1、什麼是URL Scheme?

android中的scheme是一種頁面内跳轉協定,是一種非常好的實作機制,通過定義自己的scheme協定,可以非常友善跳轉app中的各個頁面;通過scheme協定,伺服器可以定制化告訴App跳轉那個頁面,可以通過通知欄消息定制化跳轉頁面,可以通過H5頁面跳轉頁面等。

2、URL Scheme應用場景

用戶端應用可以向作業系統注冊一個 URL scheme,該 scheme 用于從浏覽器或其他應用中啟動本應用。通過指定的 URL 字段,可以讓應用在被調起後直接打開某些特定頁面,比如商品詳情頁、活動詳情頁等等。也可以執行某些指定動作,如完成支付等。也可以在應用内通過 html 頁來直接調用顯示 app 内的某個頁面。綜上URL Scheme使用場景大緻分以下幾種:

  • 伺服器下發跳轉路徑,用戶端根據伺服器下發跳轉路徑跳轉相應的頁面
  • H5頁面點選錨點,根據錨點具體跳轉路徑APP端跳轉具體的頁面
  • APP端收到伺服器端下發的PUSH通知欄消息,根據消息的點選跳轉路徑跳轉相關頁面
  • APP根據URL跳轉到另外一個APP指定頁面

3、URL Scheme協定格式

先來個完整的URL Scheme協定 xl://goods:8888/goodsDetail?goodsId=10011002

通過上面的路徑 Scheme、Host、port、path、query全部包含,基本上平時使用路徑就是這樣子的。

  • xl代表該Scheme 協定名稱
  • goods代表Scheme作用于哪個位址域
  • goodsDetail代表Scheme指定的頁面
  • goodsId代表傳遞的參數
  • 8888代表該路徑的端口号

4、URL Scheme如何使用?

1.)在AndroidManifest.xml中對<activity />标簽增加<intent-filter />設定Scheme

<activity
            android:name=".GoodsDetailActivity"
            android:theme="@style/AppTheme">
            <!--要想在别的App上能成功調起App,必須添加intent過濾器-->
            <intent-filter>
                <!--協定部分,随便設定-->
                <data android:scheme="xl" android:host="goods" android:path="/goodsDetail" android:port="8888"/>
                <!--下面這幾行也必須得設定-->
                <category android:name="android.intent.category.DEFAULT"/>
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.BROWSABLE"/>
            </intent-filter>
        </activity>
           

2.)擷取Scheme跳轉的參數

Uri uri = getIntent().getData();
if (uri != null) {
    // 完整的url資訊
    String url = uri.toString();
    Log.e(TAG, "url: " + uri);
    // scheme部分
    String scheme = uri.getScheme();
    Log.e(TAG, "scheme: " + scheme);
    // host部分
    String host = uri.getHost();
    Log.e(TAG, "host: " + host);
    //port部分
    int port = uri.getPort();
    Log.e(TAG, "host: " + port);
    // 通路路勁
    String path = uri.getPath();
    Log.e(TAG, "path: " + path);
    List<String> pathSegments = uri.getPathSegments();
    // Query部分
    String query = uri.getQuery();
    Log.e(TAG, "query: " + query);
    //擷取指定參數值
    String goodsId = uri.getQueryParameter("goodsId");
    Log.e(TAG, "goodsId: " + goodsId);
}
           

3.)調用方式

網頁上

<a href="xl://goods:8888/goodsDetail?goodsId=10011002">打開商品詳情</a>      

原生調用

Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
  startActivity(intent);      

4.)如何判斷一個Scheme是否有效

PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("xl://goods:8888/goodsDetail?goodsId=10011002"));
List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
    startActivity(intent);
}