android中的通过网页链接打开本地app

通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。

首先在Mainifest文件里面对要启动的Activity添加一个过滤器。

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

1.首先在编写一个简单的html页面

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2.   
  3.     <head>  
  4.   
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6.       
  7.         <title>Insert title here</title>  
  8.   
  9.     </head>  
  10.   
  11.     <body>  
  12.   
  13.         <a href="m://my.com/">打开app</a><br/>  
  14.   
  15.     </body>  
  16.   
  17. </html>  

2.在android本地的清单文件中添加

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <activity  
  2.            android:name=".MainActivity"  
  3.            android:configChanges="orientation|locale"  
  4.            android:screenOrientation="portrait">  
  5.            <intent-filter>  
  6.                <action android:name="android.intent.action.MAIN" />  
  7.   
  8.                <category android:name="android.intent.category.LAUNCHER" />  
  9.   
  10.                <action android:name="android.intent.action.VIEW" />  
  11.   
  12.                <category android:name="android.intent.category.DEFAULT" />  
  13.                <category android:name="android.intent.category.BROWSABLE" />  
  14.   
  15.                <data  
  16.                    android:host="my.com"  
  17.                    android:scheme="m" />  
  18.            </intent-filter>  
  19.        </activity>  

然后使用“手机浏览器”或者“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=marc&arg1=xie">打开app</a><br/>
    </body>
</html>

(1).假如你是通过浏览器打开这个网页的,那么获取数据的方式为:

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

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Uri uri = getIntent().getData();  
  2.      if (uri != null) {  
  3.          String test1 = uri.getQueryParameter("arg0");  
  4.          String test2 = uri.getQueryParameter("arg1");  
  5.          tv.setText(test1 + test2);  
  6.      }  

(2)如果使用webview访问该网页,获取数据的操作为:

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. webView.setWebViewClient(new WebViewClient(){  
  2.   @Override  
  3.   public boolean shouldOverrideUrlLoading(WebView view, String url) {  
  4.       Uri uri=Uri.parse(url);  
  5.           if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){  
  6.               String arg0=uri.getQueryParameter("arg0");  
  7.               String arg1=uri.getQueryParameter("arg1");  
  8.                
  9.           }else{  
  10.               view.loadUrl(url);  
  11.           }  
  12.       return true;  
  13.   }  
  14. });  

上面只是实现了 传参等操作。但是如果需要判断是否安装了该应用。

[html]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. <html>  
  2.   
  3.     <head>  
  4.   
  5.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  6.       
  7.         <title>Insert title here</title>  
  8.   
  9.     </head>  
  10.   
  11.     <body>  
  12.           <script language="javascript">  
  13.                 function openApp(){  
  14.                       
  15.                               
  16.                             window.location.href = 'm://marc.com/?arg0=marc&arg1=xie';  //app内部  
  17.                             setTimeout(function(){  
  18.                                     window.location.href='http://www.wln100.com/Aat/App/index.html';//下载app的网页  
  19.                             },500);  
  20.                        
  21.                 }  
  22.          </script>  
  23.   
  24.         <a href="javascript:openApp()">打开app</a><br/>  
  25.   
  26.     </body>  
  27.   
  28. </html>  

添加js 就可以实现了。亲测有效

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值