什么是 URL Scheme?
android中的scheme是一種頁面內跳轉協(xié)議,是一種非常好的實現機制,通過定義自己的scheme協(xié)議,可以非常方便跳轉app中的各個頁面;通過scheme協(xié)議,服務器可以定制化告訴App跳轉那個頁面,可以通過通知欄消息定制化跳轉頁面,可以通過H5頁面跳轉頁面等。
URL Scheme應用場景:
客戶端應用可以向操作系統(tǒng)注冊一個 URL scheme,該 scheme 用于從瀏覽器或其他應用中啟動本應用。通過指定的 URL 字段,可以讓應用在被調起后直接打開某些特定頁面,比如商品詳情頁、活動詳情頁等等。也可以執(zhí)行某些指定動作,如完成支付等。也可以在應用內通過 html 頁來直接調用顯示 app 內的某個頁面。綜上URL Scheme使用場景大致分以下幾種:
服務器下發(fā)跳轉路徑,客戶端根據服務器下發(fā)跳轉路徑跳轉相應的頁面
H5頁面點擊錨點,根據錨點具體跳轉路徑APP端跳轉具體的頁面
APP端收到服務器端下發(fā)的PUSH通知欄消息,根據消息的點擊跳轉路徑跳轉相關頁面
APP根據URL跳轉到另外一個APP指定頁面
URL Scheme協(xié)議格式:
先來個完整的URL Scheme協(xié)議格式:
openapp://thisapp:8888/content?Id=10011002
通過上面的路徑 Scheme、Host、port、path、query全部包含,基本上平時使用路徑就是這樣子的。(Scheme和Host是必要的)
openapp代表該Scheme 協(xié)議名稱(相當于http這樣的協(xié)議頭)
thisapp代表Scheme作用于哪個地址域(相當于baidu.com這樣的域名格式,當然,可以不需要.com這樣的后綴)
content代表Scheme指定的頁面(相當于 baidu.com/css 這樣的路徑,然后在app內打開相關的頁面)
Id代表傳遞的參數(相當于 https://www.baidu.com/s?wd=12312 這樣的GET參數)
8888代表port該路徑的端口號
URL Scheme如何使用:
1.在AndroidManifest.xml中對 < activity / > 標簽 增加 < intent-filter /> 設置Scheme
<activity
android:name=".GoodsDetailActivity"
<!--Activity的名稱-->
android:theme="@style/AppTheme">
<!--Activity的主題-->
<!--要想在別的App上能成功調起App,必須添加intent過濾器-->
<intent-filter>
<data android:scheme="openapp" android:host="thisapp" android:path="/content" 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.JAVA獲取Scheme跳轉的參數
Uri uri = getIntent().getData();
if (uri != null) {
String url = uri.toString();
Log.e(TAG, "url: " + uri);
String scheme = uri.getScheme();
Log.e(TAG, "scheme: " + scheme);
String host = uri.getHost();
Log.e(TAG, "host: " + host);
int port = uri.getPort();
Log.e(TAG, "host: " + port);
String path = uri.getPath();
Log.e(TAG, "path: " + path);
List<String> pathSegments = uri.getPathSegments();
String query = uri.getQuery();
Log.e(TAG, "query: " + query);
String goodsId = uri.getQueryParameter("Id");
Log.e(TAG, "Id: " + Id);
}
3.調用方式
HTML網頁
<a href="openapp://thisapp:8888/content?Id=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);
}
如果手機內沒有安裝該APP則JS跳轉至下載頁面
<!DOCTYPE html>
<html lang="en">
<head>
<title>h5跳原生</title>
</head>
<body></body>
<script>
(function(){
var ua = navigator.userAgent.toLowerCase();
var t;
var config = {
scheme_IOS: 'openapp://',
scheme_Adr: 'openapp://thisapp:8888/content?Id=10011002',
download_url: 'http://www.baidu.com',
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);
}
}
openclient();
})()
</script>
</html>
第二種
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="utf-8" />
<title>文檔標題</title>
</head>
<body>
<div style="font-size: 68px;">
<a href="javascript:open_or_download_app();">打開APP</a>
<span id="device"></span>
</div>
<script type="text/javascript">
function open_or_download_app() {
var device = document.getElementById("device");
if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {
device.innerHTML = "ios設備";
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime <2200) {
window.location = "xxxxxxxx";
} else {
window.close();
}
},2000);
window.location = "openapp://thisapp:8888/content?Id=10011002";
} else if (navigator.userAgent.match(/android/i)) {
device.innerHTML = "Android設備";
var loadDateTime = new Date();
window.setTimeout(function() {
var timeOutDateTime = new Date();
if (timeOutDateTime - loadDateTime < 2200) {
window.location = "xxxxxxxx";
} else {
window.close();
}
},2000);
window.location = "openapp://thisapp:8888/content?Id=10011002";
}
}
</script>
</body>
</html>
總結:
Scheme的基本使用也就這么多了,其他的使用在以后用到的時候再做總結。
該文章在 2025/8/5 18:39:24 編輯過