android/iPhone:如何从browser直接打开应用程序或者打开应用商店(如果没有应用程序)

 

android/iPhone:如何从browser直接打开应用程序或者打开应用商店(如果没有应用程序)

原文地址:http://blog.csdn.net/hundsong/article/details/6623500

[html]  view plain copy
  1.   
最近在做一个项目,该项目的前身是为mobile browser量身打造的一个网站。现在有这样一个需求:

当用户在用mobile browser浏览该网站的时候会点击一个按钮/超链接,通过这个按钮的点击事情需要打开安装在本机的应用程序,或者如果本机没有安装该应用程序则打开应用商店并打开该程序在商店中的搜索结果页面。

刚开始的时候iPhone team的人给出一个solution,下面是实施跳转的HTML + javascript源代码。

[html]  view plain copy
  1. <html>   
  2.     <head>   
  3.         <meta name="viewport" content="width=device-width" />   
  4.     </head>   
  5.     <body>   
  6.         <h2><a id="applink1" href="mtcmtc://profile/116201417">Open scheme(mtcmtc) defined in iPhone with parameters </a></h2>   
  7.         <h2><a id="applink2" href="unknown://nowhere">open unknown with fallback to appstore</a></h2>   
  8.         <p><i>Only works on iPhone!</i></p>      
  9.           
  10.         <script type="text/javascript">   
  11.             // To avoid the "protocol not supported" alert, fail must open another app.  
  12.             var appstore = "itms://itunes.apple.com/us/app/facebook/id284882215?mt=8&uo=6";  
  13.             function applink(fail){  
  14.                 return function(){  
  15.                     var clickedAt = +new Date;  
  16.                     // During tests on 3g/3gs this timeout fires immediately if less than 500ms.  
  17.                     setTimeout(function(){  
  18.                               // To avoid failing on return to MobileSafari, ensure freshness!  
  19.                               if (+new Date - clickedAt < 2000){  
  20.                               window.location = fail;  
  21.                               }  
  22.                               }, 500);      
  23.                 };  
  24.             }  
  25.             document.getElementById("applink1").onclick = applink(appstore);  
  26.             document.getElementById("applink2").onclick = applink(appstore);  
  27.             </script>   
  28.     </body>   
  29. </html>  

其原理就是为HTML页面中的超链接点击事件增加一个setTimeout方法.

如果在iPhone上面500ms内,本机有应用程序能解析这个协议并打开程序,则这个回调方法失效;如果本机没有应用程序能解析该协议或者500ms内没有打开个程序,则执行setTimeout里面的function,就是跳转到apple的itunes。


我用同样的原理来处理android的javascript跳转,发现如果本机没有程序注册intent-filter for 这个协议,那么android内置的browser就会处理这个协议并且立即给出反应(404,你懂的),不会像iPhone一样去执行setTimeout里面的function,即便你把500ms改成0ms也不管用。

我就开始了我的Google search之旅,最终在stackoverflow一个不起眼的地方找到solution。

不解释,先给出源代码

android里面androidManifest.xml文件对activity的配置,如何配置就不表述了,表达能力有限,请参考developer.android.com

[html]  view plain copy
  1. <activity android:name=".ui.UploadActivity" android:screenOrientation="portrait">  
  2.             <intent-filter>  
  3.                 <data android:scheme="http" android:host="192.168.167.33" android:port="8088" android:path="/mi-tracker-web/download.html"/>  
  4.                 <action android:name="android.intent.action.VIEW" />  
  5.                 <category android:name="android.intent.category.DEFAULT" />  
  6.                 <category android:name="android.intent.category.BROWSABLE" />  
  7.             </intent-filter>  
  8.         </activity>  


HTML页面中指向该应用程序的hyperlink
[html]  view plain copy
  1. <a id="applink1" href="http://192.168.167.33:8088/mi-tracker-web/download.html">  
  2.             Open Application</a>  

不难发现,在androidManifest.xml中配置的filter中data的属性表述,在下面的HTML.href中全部看到了。请注意,这两个路径要全部一致,不能有差别,否则android系统就不会拦截这个hyperlink。

好了,为什么我说这个solution能解决我们当初提出来的需求呢,答案在这里:

如果说本机安装了这个应用程序

在android browser中点击HTML中的applink1,browser会重定向到指定的链接,但是由于我们的应用程序在android OS中配置了一个intent-filter,也是针对这个制定的链接。就是说现在android系统有两个程序能处理这个链接:一个是系统的browser,一个是配置了intent-filter的activity。现在点击这个链接,系统就会弹出一个选择:是用browser还是你指定的activity打开。如果你选择你的activity,系统就会打开你的应用程序,如果你继续选择用browser,就没有然后了。


如果说本机木有安装这个应用程序

那么这个HTML里面的这个超链接就起很重要的左右了,这个download.html里面可以forward到android的应用商店

download.jsp源代码如下。具体为什么请求的是download.html这个地址却访问到了download.jsp,就不解释了,struts2的东西。

[html]  view plain copy
  1. <%@ page language="java" contentType="text/html; charset=ISO-8859-1"  
  2.     pageEncoding="ISO-8859-1"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <script type="text/javascript">  
  11. <span style="white-space:pre">  </span>window.location="market://search?q=com.singtel.travelbuddy.android";</script>  
[html]  view plain copy
  1. </body>  
  2. </html>  

唉,文笔不行,估计我写出来自己都不怎么看得懂。就再把跳转的关键点说一下:

在androidManifest.xml中定义intent-filter的时候定义的scheme,host,port,path拼凑起来是一个有用的HTTP路径,这样就算本机没有activity定义了intent-filter来捕获这个链接,那这个链接也会重定向到打开android market place的页面,继而打开应用商店。因为每个android手机都会捕获到market这个协议(如果android手机里面没有market商店,不怪我哈),系统就会自动打开market place应用商店并根据参数进入搜索页面并显示结果。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值