外部跳轉APP
需求
廣告推廣、華為微服務;通過外部網頁或者卡片跳轉到我們的app指定介面。如果app已經存在開啟app,app不存在跳轉下載介面。
APP配置
<activity android:name=".LauncherActivity" android:launchMode="singleTask" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <!--start--> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.BROWSABLE" /> <!--scheme host 必填--> <!--path port 根據需求新增--> <data android:host="host" android:scheme="scheme" /> </intent-filter> <!--end--> </activity>
scheme 、host 是必填項
android:path="/path"
android:port="8080"
根據需求進行新增
獲取URL scheme中的值
Uri uri = getIntent().getData(); if (uri != null) { // 完整的url資訊 String url = uri.toString(); Log.i(TAG, "url:" + uri); // scheme部分 String scheme = uri.getScheme(); Log.i(TAG, "scheme:" + scheme); // host部分 String host = uri.getHost(); Log.i(TAG, "host:" + host); // port部分 int port = uri.getPort(); Log.i(TAG, "port:" + port); // 訪問路勁 String path = uri.getPath(); Log.i(TAG, "path:" + path); List<String> pathSegments = uri.getPathSegments(); // Query部分 String query = uri.getQuery(); Log.i(TAG, "query:" + query); //獲取指定引數值 String success = uri.getQueryParameter("success"); Log.i(TAG, "success:" + success); } }
通過web開啟,核心引數scheme 和 host
1、建立一個html,把這個貼上扔進去。如果安裝了app就可以開啟
<a href="scheme://host">開啟app</a>
2、這個可以配置下載連結,如果APP未安裝會跳轉下載連結地址
(根據需要自己修改,網上找的例子)
<a id="call-app" href="javascript:;" > Start or Download </a><br/><br/> <script type="text/javascript"> (function(){ var ua = navigator.userAgent.toLowerCase(); var t; var config = { /*scheme:必須*/ scheme_IOS: 'scheme://host', scheme_Adr: 'scheme://host', download_url: 'http://a.app.qq.com/o/simple.jsp?pkgname=com.test', timeout: 600 }; function openclient() { var startTime = Date.now(); var ifr = document.createElement('iframe'); ifr.src = ua.indexOf('os') > 0 ? config.scheme_IOS : config.scheme_Adr; ifr.style.display = 'none'; document.body.appendChild(ifr); var t = setTimeout(function() { var endTime = Date.now(); if (!startTime || endTime - startTime < config.timeout + 200) { window.location = config.download_url; } else { } }, config.timeout); window.onblur = function() { clearTimeout(t); } } window.addEventListener("DOMContentLoaded", function(){ document.getElementById("call-app").addEventListener('click', openclient, false); }, false); })() </script>
通過另外一個app開啟
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse("scheme://host")); startActivity(intent);
可以try catch一下,出現Exception說明手機沒有安裝想開啟的APP,進行其他處理或者提示。
華為微服務
用到的是deeplink連結,使用原理也是scheme
遇坑
scheme | host 大小寫問題(親測實坑)
scheme host 在瀏覽器裡是不分大小寫的,會統一轉為小寫。 所以不要再scheme、host裡面寫大寫!!! 所以不要再scheme、host裡面寫大寫!!! 所以不要再scheme、host裡面寫大寫!!!