通过用手机的浏览器(内置,第三方都可)访问一个网页,实现点击一个链接启动自己的应用,并传递数据。
首先在Mainifest文件里面对要启动的Activity添加一个过滤器。
通过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本地的清单文件中添加
- <activity
- android:name=".MainActivity"
- android:configChanges="orientation|locale"
- android:screenOrientation="portrait">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
-
- <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>
- </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");
- Uri uri = getIntent().getData();
- if (uri != null) {
- String test1 = uri.getQueryParameter("arg0");
- String test2 = uri.getQueryParameter("arg1");
- tv.setText(test1 + test2);
- }
(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>
-
- <head>
-
- <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
-
- <title>Insert title here</title>
-
- </head>
-
- <body>
- <script language="javascript">
- function openApp(){
-
-
- window.location.href = 'm://marc.com/?arg0=marc&arg1=xie'; //app内部
- setTimeout(function(){
- window.location.href='http://www.wln100.com/Aat/App/index.html';//下载app的网页
- },500);
-
- }
- </script>
-
- <a href="javascript:openApp()">打开app</a><br/>
-
- </body>
-
- </html>
添加js 就可以实现了。亲测有效