美文网首页
2018-03-19 安卓:从网页唤醒APP

2018-03-19 安卓:从网页唤醒APP

作者: 心灵屋宿客 | 来源:发表于2018-03-19 15:25 被阅读0次

从网页打开APP,如果没有安装就进行下载。

安卓AndroidMainfext.xml

在要启动的activity下面添加一个intent-filter。

        <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:host="splash" android:scheme="vchao" />
       </intent-filter>

html 网页

<!doctype html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8">

        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black"/>

        <title>唤醒APP</title>
        <meta id="viewport" name="viewport" content="width=device-width,initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0,minimal-ui">
    </head>
    <body>
        <div>
            <a id="J-call-app" href="javascript:;" class="label">立即打开&gt;&gt;</a>
            <input id="J-download-app" type="hidden" name="storeurl" value="http://m.chanyouji.cn/apk/chanyouji-2.2.0.apk">
        </div>

        <script>
            (function(){
                /*获取UA标识,并转为小写*/
                var ua = navigator.userAgent.toLowerCase();
                var t;
                var config = {
                    /*scheme:必须*/
                    scheme_IOS: 'vchao://',
                    scheme_Adr: 'vchao://splash',
                    download_url: document.getElementById('J-download-app').value,
                    timeout: 600
                };

                function openclient() {
                    var startTime = Date.now();

                    var ifr = document.createElement('iframe');
                    
                    /*通过UA标识,判断是否是苹果系统*/
                    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("J-call-app").addEventListener('click',openclient,false);

                }, false);
            })()
        </script>
    </body>
</html>

如果需要传参数。

在html网页中直接拼接参数。

vchao://splash/?arg0=marc&arg1=xie

在唤醒的activity页面做参数的接收。

    Uri uri = getIntent().getData();  
     if (uri != null) {  
         String test1 = uri.getQueryParameter("arg0");  
         String test2 = uri.getQueryParameter("arg1");  
         tv.setText(test1 + test2);  
     }  

相关文章

网友评论

      本文标题:2018-03-19 安卓:从网页唤醒APP

      本文链接:https://www.haomeiwen.com/subject/cqbuqftx.html