您的位置:首页 > 移动开发

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

2015-12-09 13:08 483 查看
点击浏览器中的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页面里面能够自己主动重定向到下载链接。这个就能自己主动下载。

                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: