Android 浏览器打开本地app

来源:互联网 发布:java实现九九乘法表 编辑:程序博客网 时间:2024/05/30 05:10
<html>    <head>        <meta charset="utf-8"/>        <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=0">        <title>页面打开App</title>    </head>    <body>        <script language="javascript">            function open_or_download_app() {                if (navigator.userAgent.match(/(iPhone|iPod|iPad);?/i)) {            // 判断useragent,当前设备为ios设备                    var loadDateTime = new Date();            // 设置时间阈值,在规定时间里面没有打开对应App的话,直接去App store进行下载。                    window.setTimeout(function() {                        var timeOutDateTime = new Date();                        if (timeOutDateTime - loadDateTime < 5000) {                            window.location = "https://itunes.apple.com/cn/app/hu-lu/id627370076?mt=8";                        } else {                            window.close();                        }                    },                    25);                    window.location = "XXXX://XXXX";  // Android端URL Schema                } else if (navigator.userAgent.match(/android/i)) {            // 判断useragent,当前设备为ios设备            window.location = "XXX://YYYY:8080/ZZZ/AAAA/BBB.html";  // Android端URL Schema           }        }     </script>    <p style="height:30px;line-height:30px;text-align:center;">WAP页面打开本地应用测试</p>    <a href="javascript:open_or_download_app();" style="margin:100px 100px 100px 100px;"> 打开本地阿里巴巴 </a>  </body></html>
复制代码

  移动网站的实现代码就是上面这段,不复杂吧?我感觉很不复杂。但是光有这段代码是不行了,有心人会发现我代码中有XXXX。。。类似的东东,这个是由App端设置的URL Schema。

  什么是URL Schema呢?我不告诉你,自己问google和度娘去。

  IOS端怎么来配置URL Schema呢?这个我也不会告诉你,因为我没有做过IOS开发,所以具体的配置方法我也不知道,如果有IOS开打的看客的话,欢迎在评论中给出IOS端URL Schema的配置方法。

  有人会说,你不也没说客户端怎么玩呢?光有你上面一段代码有屁用啊?等等,我染指过Android应用开发,所以呢,我会给出Android端URL Schema的配置方法,各位仅做参考。

复制代码
<intent-filter>    <action android:name="android.intent.action.VIEW" />    <category android:name="android.intent.category.DEFAULT" />    <category android:name="android.intent.category.BROWSABLE" />    <category android:name="android.intent.category.LAUNCHER" />    <data android:scheme="XXX" android:host="YYYY" android:port="8080" android:path="ZZZ/AAAA/BBB.html"/></intent-filter>
复制代码

  将以上intent定义部分追加到你的Manifest定义文件,但是有两点需要注意的:

1、以上intent的定义千万不要放入到主Activity中,因为主Activity是android.intent.action.MAIN,而这里是VIEW,两者是冲突的,我在这上面纠结了好久。将以上的intent定义放到主Activity以后的任意Activity。

2、scheme的配置,android不像IOS,在ios里面可以随意进行配置,只需要schema(nihao)和host(11111)就ok,这样访问的时候只要:nihao://11111。但是android端最好把URL Schema配置成如果本地没有对应App的下载URL。

3、<uses-permission android:name="android.permission.INTERNET" />

注:可以放在主Activity中,不过需要另起一个<intent-filter></intent-filter>,不能与android.intent.action.MAIN放在一个intent-filter中,否则会出现应用图标消失的情况。
代码如下:
<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="xxx" 
android:scheme="m" />
</intent-filter>

 按照上面的设置android肯定木问题的

0 0
原创粉丝点击