通過瀏覽器連結開啟本地應用(APP)
阿新 • • 發佈:2019-02-08
前言:業務場景,一個分享出去的h5介面通過頁面內某個事件的觸發,啟動目標app並執行相關邏輯處理或做其他頁面跳轉(如:跳應用市場下載應用等)。下面是我在企業開發過程中,實操的記錄,對於有這塊需求的朋友,可以來參考下。
Android實現通過瀏覽器點選連結開啟本地應用(APP)並拿到瀏覽器傳遞的資料
前端頁面:(介面中觸發事件的入口)
<a href="myapp://jp.app/openwith?name=zhangsan&age=26">啟動應用程式</a>
Android端:
1.在AndroidManifest.xml的MAIN Activity下追加以下內容(按照下面的格式來追加)
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<!-- 在MAIN的同級處加入過濾器,不然會導致應用圖示在桌面消失等問題 -->
<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>
</intent-filter>
2.如果需要取值,在程式碼中進行如下操作:(測試可行)
Intent i_getvalue = getIntent();
String action = i_getvalue.getAction();
if(Intent.ACTION_VIEW.equals(action)){
Uri uri = i_getvalue.getData();
if(uri != null){
String name = uri.getQueryParameter("name");
String age= uri.getQueryParameter("age");
}
}
補充:
各個專案含義如下所示:
scheme:判別啟動的App。 ※詳細後述
host:適當記述
path:傳值時必須的key ※沒有也可以
query:獲取值的Key和Value ※沒有也可以
以上就是全部內容,大家可以自個去試試,有什麼問題可以留言。或者在微信公眾號“技術幫團隊”上找我們。