html通过javascript页面调起APP
前几天瞎折腾搞了一个影视APP,用网站封装打包的。
弄完之后在我网站里加一个打开APP的按钮,但是并没有实现。
后来百度了一下,答案真的是花里胡哨啊,作为一个前端开发者完全不懂他们说的什么协议什么权限问题。
后来经过我反复搜索终于摸索到一点头绪,下面就把完整的代码分享给有用的人(暂时支持安卓系统,iso还没研究,毕竟ios门槛太高)
核心思路:点击按钮挑起APP,如果2秒内没有任何响应就跳转app下载页
注意:代码里的第一个软件的命名是自己随意写的,命名只能是英文,我为了方便你们理解就写成了中文
前者是软件名,后者是app的下载地址
testApp('m://软件的命名','/app/down.html')
<script type="text/javascript">
function testApp(url1,url2) {
var timeout, t = 500, hasApp = true;
setTimeout(function () {
if (hasApp) {
} else {
if (!document.hidden) {
window.location.href=url2;
}
console.log(window.location.href);
}
}, 2000)
var t1 = Date.now();
var ifr = document.createElement("iframe");
ifr.setAttribute('src', url1);
ifr.setAttribute('style', 'display:none');
document.body.appendChild(ifr);
timeout = setTimeout(function () {
var t2 = Date.now();
if (t2 - t1 < t + 100 ) {
hasApp = false;
}
}, t);
}</script>然后在APP的AndroidManifest.xml文件启动activity下面加入一下代码
注意:软件的命名必须和你按钮上的命名一致,否则无效,命名只能是英文,我为了方便你们理解就写成了中文
<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="m" android:host="软件的命名" /> </intent-filter>
版权声明:本文由 LzxBlog 发布,如需转载请注明出处。









赞助云储存