Android業務組件化之URL Schema使用

來源:互聯網
上載者:User

標籤:

前言:

     最近公司業務發展迅速,單一的項目工程不再適合公司發展需要,所以開始推進公司APP業務組件化,很榮幸自己能夠牽頭做這件事,經過研究實現組件化的通訊方案通過URL Schema,所以想著現在還是在預研階段,很有必要先瞭解一下URL Schema,看看是如何使用的?其實在之前做Hybrid混合編程的時候就接觸過URL Schema,總來的來說還不算陌生,今天就來回顧總結一下。

什麼是 URL Schema?

    android中的scheme是一種頁面內跳轉協議,是一種非常好的實現機制,通過定義自己的scheme協議,可以非常方便跳轉app中的各個頁面;通過scheme協議,伺服器可以定製化告訴App跳轉那個頁面,可以通過通知欄訊息定製化跳轉頁面,可以通過H5頁面跳轉頁面等。

URL Schema應用情境:

    用戶端應用可以向作業系統註冊一個 URL scheme,該 scheme 用於從瀏覽器或其他應用中啟動本應用。通過指定的 URL 欄位,可以讓應用在被調起後直接開啟某些特定頁面,比如商品詳情頁、活動詳情頁等等。也可以執行某些指定動作,如完成支付等。也可以在應用內通過 html 頁來直接調用顯示 app 內的某個頁面。綜上URL Schema使用情境大致分以下幾種:

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

   先來個完整的URL Schema協議格式:

xl://goods:8888/goodsDetail?goodsId=10011002

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

  • xl代表該Schema 協議名稱
  • goods代表Schema作用於哪個地址域
  • goodsDetail代表Schema指定的頁面
  • goodsId代表傳遞的參數
  • 8888代表該路徑的連接埠號碼
URL Schema如何使用: 1.)在AndroidManifest.xml中對<activity />標籤增加<intent-filter />設定Schema
    <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.)擷取Schema跳轉的參數
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.)如何判斷一個Schema是否有效
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);}
總結:

   Schema的基本使用也就這麼多了,其他的使用在以後用到的時候再做總結。

Android業務組件化之URL Schema使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.