码迷,mamicode.com
首页 > 移动开发 > 详细

怎样通过Html网页调用本地安卓app

时间:2017-04-28 20:21:04      阅读:302      评论:0      收藏:0      [点我收藏+]

标签:lin   public   name   页面   ant   手机浏览   flow   targe   gif   

怎样使用html网页和本地app进行传递数据呢?经过研究。发现还是有方法的,总结了一下,大致有一下几种方式

 

一、通过html页面打开Android本地的app

1、首先在编写一个简单的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>
技术分享

2、在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>
技术分享

演示样例截图例如以下:

技术分享

 

然后使用“手机浏览器”或者“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).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

Uri uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0");  String test2= uri.getQueryParameter("arg1");

(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;
  }
});
技术分享

怎样通过Html网页调用本地安卓app

标签:lin   public   name   页面   ant   手机浏览   flow   targe   gif   

原文地址:http://www.cnblogs.com/brucemengbm/p/6782955.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!