通过浏览器直接打开Android App 应用程序

点击浏览器中的URL链接,启动特定的App。

首先做成HTML的页面,页面内容格式如下:

<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a> 

这一句就可以了。


当然上面的 在标准形式,对于正常情况而言是OK的,但是每个浏览器有自己的特定义设置。


各个项目含义如下所示:

scheme:判别启动的App。 ※详细后述

host:适当记述

path:传值时必须的key     ※没有也可以

query:获取值的Key和Value  ※没有也可以

 

作为测试如下:

<a href="myapp://jp.app/openwith?name=zhangsan&age=26">启动应用程序</a>

首先在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 …/>。
其中必须的内容仅scheme,没有其他内容app也能启动。
※注意事项: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>


接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:
Intent i_getvalue = getIntent();  
String action = i_getvalue.getAction();  
  
if(Intent.ACTION_VIEW.equals(action)){  
    Uri uri = i_getvalue.getData();  
    if(uri != null){  
        String name = uri.getQueryParameter("name");  
        String age= uri.getQueryParameter("age");  
    }  
}

首先,是UC浏览器,如果你使用了自己的scheme,而不是http的话,uc会默认在你的scheme前面添加http://。这太坑爹了。其他浏览器没看是不是同样的情况。发现这个问题后我就试着把自己的scheme换成http。然后满怀期待的又跑了一遍,结果还是坑爹了。所以我想会不会是第三方浏览器对url做了处理。到这里,我也无可奈何了。我测试了UC,猎豹,欧朋,这3个都不支持。系统自带的和谷歌浏览器是支持的,但是最新版的谷歌浏览器好像也不支持了,firefox目前还支持。
官方的文档有解释: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链接。

运行结果应该是,有安装该应用的话,会打开该应用,如果没有会跳转到指向的页面。在HTML页面里面可以自动重定向到下载链接,这个就能自动下载。








郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。