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

从网页打开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);  
     }  
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 179,143评论 25 708
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,355评论 0 17
  • 1、Androiddvm的进程和Linux的进程, 应用程序的进程是否为同一个概念 DVM指dalivk的虚拟机。...
    ifeng_max阅读 1,014评论 0 10
  • 今天收到了第一件生日礼物,已经很久没有收到过礼物了,很开心。。。都快忘记自己生日是什么时候了。 心烦气燥的时候 先...
    冷冷的看你们表演阅读 200评论 0 0
  • 近日天降大雪,虽有瑞雪兆丰年之说,但城市如果不及时对道路进行除雪,有可能因为道路湿滑、白雪遮挡路标等产生严重的交通...
    龙爪槐守望者阅读 811评论 5 13

友情链接更多精彩内容