最近来了个需求,需要在浏览器中点击一个链接.如果用户没有安装APP,跳转到下载页面.如果用户已经安装APP则直接启动APP. 这个需求的难点在于如何在浏览器中启动APP.
网上有资料来讨论这个问题, 相信大家会很容易找到. 基本思路是这样的:
点击浏览器中的URL链接,启动特定的App。
首先做成HTML的页面,页面内容格式如下:
<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端。
首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动Activity时给予)
※必须添加项
<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>
HTML记述的内容加入<data …/>。
本来是很好的方法,结果使用后发现启动不了应用.
从我个人的使用经验来说,主要注意以下两个方面:
1.intent-filter的内容【android.intent.action.MAIN】和 【android.intent.category.LAUNCHER】这2个,不能与这次追加的内容混合。
所以,如果加入了同一个Activity,请按以下这样做,否则会导致应用图标在桌面消失等问题。
<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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/> </intent-filter>
这样的话,没有问题。
2. 需要在Manifest文件里边再加入一行代码
<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:scheme="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>
<data android:scheme="http" android:host="jp.app" android:pathPrefix="/openwith"/>
</intent-filter>
因为浏览器对于所有的跳转链接会加上http.
注意以上两个方面就可以启动app了. 传递参数也非常简单就不再在此啰嗦了.