网页打开Android APP

发布时间:2019-08-06 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了网页打开Android APP脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

原理解析

在AndROId平台而言,URI主要分三个部分:
scheme,authorITy,path
其中authority又分为host和port。格式如下:

<scheme>://<host>:<port>[<path>|<pathPRefix>|<pathPattern>]

对应的manifest中的<data>配置如下:

<data android:host=""
      android:mimeType=""
      android:path=""
      android:pathPattern=""
      android:pathPrefix=""
      android:port=""
      android:scheme=""
      android:ssp=""
      android:sspPattern=""
      android:sspPrefix=""/>

其中scheme为必须参数,若没有指定,那其它的属性均无效!
如果host没有指定,那么port,path,pathPrefix,pathPattern均无效!

我们最常用的是scheme,host,port,path这四个配置。

实现方法

首先在AndroidManifest中的MainActivity中添加一个<intent-filter>:

<intent-filter>  
      <action android:name="android.intent.action.VIEW" />  
      <category android:name="android.intent.category.BROWSABLE" />  
      <category android:name="android.intent.category.DEFAULT"/>  
      <data android:scheme="protocol" android:host="domain" android:pathPrefix="/link" />  
  </intent-filter>  

然后在你的网页中添加一个链接:

<a href="protocol://domain/link>打开app</a>

最后,点击这个a链接,如果app成功弹出,那么恭喜你,你成功了。

拓展

光打开app可能还不够,有时我们要传递数据,那么怎么去传递数据呢?

我们可以使用上面的方法,把一些数据传给app,那么先修改一下链接:

<a href="protocol://domain/link?id=123>打开app并传递id</a>

然后在app上的MainActivity中的onCreate方法中添加代码:

Uri uri = getIntent().getData();  
String id= uri.getQueryParameter("id");  

这样就可以传递数据啦!

如果用的是应用内的webview,获取数据的操作为:

webView.setWebViewClient(new WebViewClient(){
  @override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Uri uri=Uri.parse(url);
      if(uri.getScheme().equals("protocol")&&uri.getHost().equals("domain")){
        String id = uri.getQueryParameter("id");
          }else{
              view.loadUrl(url);
          }
        return true;
  }
});

API

getScheme(); //获得Scheme名称 

getDataString(); //获得Uri全部路径 

getHost(); //获得host

附上uri的官方api链接
https://developer.android.com...

欢迎评论

脚本宝典总结

以上是脚本宝典为你收集整理的网页打开Android APP全部内容,希望文章能够帮你解决网页打开Android APP所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。