Update:
更新了实现方式:http://my.oschina.net/liucundong/blog/354029
2014-12-9
---------我是分割线------------------------------------------------------------------------------
原文:
最近遇到这么一个需求:
当用户在手机浏览器中点击一个按钮时,如果手机上已经该应用程序,则直接打开,如果没有安装,则转向应用下载页面。 再详细一点就是:通过点击网页中一个按钮,打开本地某个Activity(如果有的话)或用浏览器打开某个url。
查了一下文档,Android是支持这个的:
http://developer.android.com/guide/topics/manifest/data-element.html
解释一下文档中的描述:scheme://host:port/path or pathPrefix or pathPattern
这里面定义的schema+host+port+(path or pathPrefix or pathPattern)能拼凑出一个http链接,包含这个filter的Activity,能处理这个http链接。
实现:
网页代码
点击这个按钮:
<a id="applink1" href="http://test.xx.com/demo/test.php">打开</a>
在 http://test.xx.com/demo/test.php中,直接开始下载xxxx.apk。
Activity代码
给目标Activity增加以下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="test.xx.com "
android:path="/demo/test.php"
android:scheme="http" />
</intent-filter>
增加该filter后,该Activity就能处理 http://test.xx.com/demo/test.php。在浏览器中点击“开始”,发起对该URL的请求时,如果本机安装了这个应用,系统就会弹出一个选择,询问你想使用浏览器打开,还是使用该应用打开,如下图:
如果本机没有安装这个应用,则直接会使用浏览器(多个浏览器的话,还需要选择一下)打开 http://test.xx.com/demo/test.php,浏览器会提示你下载应用,如下图:
by @Cundong
2014年12月9日: