从Android浏览器(并不是Appliaction里面的webkit)打开应用程序/应用商店

转自:http://www.cnblogs.com/chorrysky/archive/2012/05/16/2503961.html 


刚才为了测试在android浏览器中一个应用程序,走了一点弯路,在这里记录下来

androidManifest.xml

  1. <activity android:name=".UploadActivity" android:screenOrientation="portrait">  
  2.     <intent-filter>  
  3.         <strong><span style="color:#ff0000;"><data android:scheme="mtcmtc"/></span></strong>  
  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>  

 

浏览器中的超链接 <a href="mtcmtc://aaaaa/aaa">点击打开程序</a>


从浏览器中直接打开Android Market Place,这个market schema 是Google已经配置好了的,可以直接使用。

<a href="market://search?q=pname:com.nytimes.android">点击打开商店</a>


点击打开程序

点击打开Market

==========================================================================================

android/iPhone:如何从browser直接打开应用程序或者打开应用商店(如果没有应用程序)
  1. </pre>最近在做一个项目,该项目的前身是为mobile browser量身打造的一个网站。现在有这样一个需求:<p></p><p>当用户在用mobile browser浏览该网站的时候会点击一个按钮/超链接,通过这个按钮的点击事情需要打开安装在本机的应用程序,或者如果本机没有安装该应用程序则打开应用商店并打开该程序在商店中的搜索结果页面。</p><p></p><p>刚开始的时候iPhone team的人给出一个solution,下面是实施跳转的HTML + javascript源代码。</p><p></p><pre name="code" class="html"><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

 

  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
  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的东西。

 

  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>  
  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应用商店并根据参数进入搜索页面并显示结果。


### 解决方案 当遇到 `application.properties` 文件在打包后的 Spring Boot JAR 中丢失的问题时,通常是因为文件未被正确包含到最终的构建产物中。以下是详细的解决方案: #### 1. 确认资源目录配置 确保项目的 `application.properties` 文件位于标准的 Maven 或 Gradle 资源路径下,即 `src/main/resources/` 目录[^1]。 如果项目结构不遵循此约定,则需要手动调整构建工具的资源配置。例如,在 Maven 的 `pom.xml` 中可以这样指定额外的资源路径: ```xml <build> <resources> <resource> <directory>custom/path/to/resources</directory> <includes> <include>**/*.properties</include> </includes> </resource> </resources> </build> ``` #### 2. 验证构建过程中的资源复制行为 运行以下命令来验证 `application.properties` 是否已成功复制到目标 JAR 文件中: ```bash jar tf target/kkfileview.jar | grep application.properties ``` 如果没有找到该文件,则可能是构建过程中遗漏了它。此时应检查构建脚本是否正确处理了资源文件。 #### 3. 使用 Spring Boot 提供的功能加载外部配置 即使 `application.properties` 缺失,也可以通过设置环境变量或命令行参数的方式提供必要的配置项。例如: ```java public static void main(String[] args) { SpringApplication app = new SpringApplication(MainApplication.class); app.setDefaultProperties(Collections.singletonMap("spring.config.location", "classpath:/config/application.properties")); app.run(args); } ``` 上述代码片段强制指定了一个备用位置用于读取配置文件[^3]。 另外还可以利用 Java 命令启动应用时附加 `-Dspring.config.location` 参数指向实际存在的 `.properties` 文件副本。 #### 4. 检查是否存在多模块冲突 对于复杂的多模块项目来说,可能存在不同子模块间覆盖或者忽略某些共享资源的情况。务必确认父 POM 和各子模块间的继承关系定义清晰无误,且所有依赖都声明得当。 --- ### 示例代码修正版 下面给出一个完整的示例程序入口类实现方式作为参考: ```java import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class KkFileViewApplication { public static void main(String[] args) { System.setProperty("spring.config.name", "application"); System.setProperty("spring.config.additional-location", "classpath:/,file:./config/"); SpringApplication.run(KkFileViewApplication.class, args); } } ``` 以上方法能够有效应对大多数因缺少默认配置而导致的服务异常场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值