Android TipsAndroid开发技术分享凌宇的Android踩坑路

#Android 通过浏览器打开手机app

2017-04-19  本文已影响348人  间歇性丶神经病患者

CTO吩咐下来的任务...弄下能不能在浏览器中启动我们的App。

踩坑

原来的做法是:


<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

各个值的定义为:

scheme:判别启动的App。 ※详细后述
host:适当记述
path:传值时必须的key ※没有也可以
query:获取值的Key和Value ※没有也可以


例如:


<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>  

而在android的清单文件的主页面配置如下:


<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>  
</intent-filter>

然而...并不可以,我用的魅族的手机,用的魅族自带浏览器。GG了
谷歌浏览器,或者是android原生浏览器可能可以,或者是我们自带的WebView。

有看到帖子说是因为data-sentintent的原因,具体原因我也没有去查看。
有兴趣的童鞋可以坐飞机直达:--------> 飞吧

解决

此路不通我们走另外一条路。

肯定是不能使用简单的<intent-filter>来进行查找,我们使用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>打开我的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">立即打开>></a>
            <input id="J-download-app" type="hidden" name="storeurl" value="http://当找不到该app的时候的下载地址,如应用宝之类的...不要谷歌,翻不了墙的哥们.com">
        </div>

        <script>
            (function(){
                var ua = navigator.userAgent.toLowerCase();
                var t;
                var config = {
                    /*scheme:必须*/
                    scheme_IOS: 'cundong://',
                    scheme_Adr: 'cundong://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>

我们需要注意这个地方:


  <input id="J-download-app" type="hidden" name="storeurl" value="http://当找不到该app的时候的下载地址,如应用宝之类的...不要谷歌,翻不了墙的哥们.com">

记得替换成你们的下载地址。

而我们大android的清单文件需要配置:


 <activity android:name=".ui.activity.SplashActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </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="cundong" />
            </intent-filter>
        </activity>

OK,使用魅族的浏览器亲测成功。其他牌子的浏览器稍候测试,毕竟我那么反感那些花里胡哨的浏览器,要下载测试我是不情愿。。。

优化

好了,好像单纯地打开app...没什么卵用啊,能不能传值呢?答案是可以的~我们看js:


         var config = {
                    /*scheme:必须*/
                    scheme_IOS: 'cundong://',
                    scheme_Adr: 'cundong://splash',
                    download_url: document.getElementById('J-download-app').value,
                    timeout: 600
                };

其中的scheme_Adr就是android的匹配Scheme,如果需要传值的话,就是在后面通过get的方式进行传值。

   var config = {
                    /*scheme:必须*/
                    scheme_IOS: 'cundong://',
                    scheme_Adr: 'cundong://splash?nama=Ly&age=18',
                    download_url: document.getElementById('J-download-app').value,
                    timeout: 600
                };

然后在跳转的页面中进行获取:


  Intent i_getvalue = getIntent();
        String action = i_getvalue.getAction();

        if (Intent.ACTION_VIEW.equals(action)) {
            Uri uri = i_getvalue.getData();
            if (uri != null) {
                String name = uri.getQueryParameter("name");
                String age = uri.getQueryParameter("age");
                showTs(name);
                showTs(age);
                showLog("name----------------"+name);
                showLog("age----------------"+age);
            }
        }

就可以进行Hhtml调起APP,并进行页面传值的操作了... 其实难度主要是在js方面。

上一篇下一篇

猜你喜欢

热点阅读