打开网站检测APP并提示打开

打开网站判断是否安装APP,如果已经安装则提示打开,适用于Android、iPhone等设备,亲测可用!

Scheme定义Activity

1)在androidmanifest.xml中定义scheme

<!-- 启动页 --><activity    android:name="com.qiyuan.congmingtou.activity.SplashActivity"    android:label="@string/app_name">    <intent-filter>        <action android:name="android.intent.action.MAIN" />        <category android:name="android.intent.category.LAUNCHER" />    </intent-filter>    <!-- 要想在别的App上能成功调起App,必须添加intent过滤器 -->    <intent-filter>        <!-- 协议部分,随便设置 -->        <data android:scheme="cmt" android:host="splash"/>        <!-- 下面这几行也必须得设置 -->        <category android:name="android.intent.category.DEFAULT" />        <category android:name="android.intent.category.BROWSABLE" />        <action android:name="android.intent.action.VIEW" />    </intent-filter></activity>这样我们便定义了能够接受scheme请求的activity实例,当网页或者是android代码发送这种规则scheme的请求的时候就能够吊起SplashActivity了。

2).网页中Html代码和js代码

<!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>this's a demo</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(){
                var ua = navigator.userAgent.toLowerCase();
                var t;
                var config = {
                    /*scheme:必须*/
                    scheme_IOS: 'IfInstalledCongMingTou://congmingtou',
                    scheme_Adr: 'cmt://splash',
                    download_url: document.getElementById('J-download-app').value,
                    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("J-call-app").addEventListener('click',openclient,false);

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


注:本文非原创,转自:CSDN,不知道站长让不让贴原创链接,所以就不发原创链接了,免得删帖了,看论坛有过相关内容出售过,下载了测试下,不知道是不是我兼容问题,我这没效果,特意在网上找了找文章,分享给大家,有能力的搞个类库给大家也是稳稳的!

相关推荐

网友评论(0)