请选择 进入手机版 | 继续访问电脑版

莱州网

 找回密码
 立即注册
查看: 796|回复: 0

通过Html网页调用本地安卓app程序代码

[复制链接]

33

主题

34

帖子

196

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
196
发表于 2022-1-16 18:24:38 | 显示全部楼层 |阅读模式
前段时间写一些移动端的项目,正好项目中遇到与native交互的需求,特此将其整理下来:
一. 通过html页面打开Android本地的app
首先在编写一个简单的html页面
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <a href="m://my.com/">打开app</a><br/>
    </body>
</html>
1
2
3
4
5
6
7
8
9
在Android本地app的配置
在AndroidManifest的清单文件里的intent-filte中加入如下元素:
<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="my.com"
                    android:scheme="m" />
</intent-filter>
1
2
3
4
5
6
7
8
然后使用“手机浏览器”或者“webview”的方式打开这个本地的html网页,点击“打开APP”即可成功开启本地的指定的app

二、如何通过这个方法获取网页带过来的数据
只能打开就没什么意思了,最重要的是,我们要传递数据,那么怎么去传递数据呢?
我们可以使用上述的方法,把一些数据传给本地app,那么首先我们更改一下网页,代码修改后:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <a href="m://my.com/?arg0=0&arg1=1">打开app</a><br/>
    </body>
</html>
1
2
3
4
5
6
7
8
9
(1).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

    uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0");  String test2= uri.getQueryParameter("arg1");
1
(2)如果使用webview访问该网页,获取数据的操作为:

webView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Uri uri=Uri.parse(url);
          if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){
              String arg0=uri.getQueryParameter("arg0");
              String arg1=uri.getQueryParameter("arg1");

          }else{
              view.loadUrl(url);
          }
      return true;
  }
});
————————————————
版权声明:本文为CSDN博主「爽哥V武」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_38194357/article/details/69267199

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|手机版|莱州网

GMT+8, 2023-3-22 03:45 , Processed in 0.058310 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表