Html网页调用本地安卓APP

前言

android中的scheme是一种页面内跳转协议。

通过定义自己的scheme协议,可以非常方便跳转app中的各个页面;

HTML

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<html>
<meta charset="UTF-8">
<body>
<h1>webToApp</h1>
<!--手动点击跳转-->
<a id="linkToCart" href="#">点我打开</a>
</body>
<script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//点击链接的时候调用
$("#linkToCart").click(function(){
//设置linkToCart的href的值
$("#linkToCart").attr("href","qgyxnews://psvmc.cn:8080/test?nid="+123);
});
});
</script>
</html>

XML

1
2
3
4
5
6
7
8
9
10
11
12
<activity
android:name=".ui.activity.MainActivity"
android:label="@string/title_activity_main"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<!--host可以不写-->
<data android:scheme="qgyxnews" />
</intent-filter>
</activity>

onCreate中调用如下方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
fun loadnewsinfo() {
val intent = intent
val uri = intent.data
if (uri != null) {
val scheme = uri.getScheme();
val host = uri.getHost();
val port = "" + uri.getPort();
val path = uri.getPath();
val query = uri.getQuery();
val nid = uri.getQueryParameter("nid");
L.e("获得的数据\n" + "scheme:" + scheme + "\n"
+ "host:" + "host:" + host + "\n"
+ "port:" + port + "\n"
+ "path:" + path + "\n"
+ "query:" + query + "\n"
+ "nid:" + nid);
}
}