android 外部内容分享到app内,外部跳转APP
需求
廣告推廣、華為微服務;通過外部網頁或者卡片跳轉到我們的app指定界面。如果app已經存在打開app,app不存在跳轉下載界面。
APP配置
android:name=".LauncherActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
android:host="host"
android:scheme="scheme" />
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 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就可以打開
打開app
2、這個可以配置下載鏈接,如果APP未安裝會跳轉下載鏈接地址
(根據需要自己修改,網上找的例子)
Start or Download
(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);
})()
通過另外一個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里面寫大寫!!!
總結
以上是生活随笔為你收集整理的android 外部内容分享到app内,外部跳转APP的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab中右三角形方向,《有限元基础
- 下一篇: 镜像浏览器_害怕win10镜像有第三方软