scheme打开手机内APP


需求:网页上一个链接,点击后会唤醒手机内响应的app,打开指定APP的功能页面。

方法:在AndroidManifest.xml里面对需要打开的页面设置action,两个category, data

1
2
3
4
5
6
7
8
<activity android:name=".MainActivity2">
<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:scheme="fshao" android:host="my.app" android:pathPrefix="/openwith"/>
</intent-filter>
</activity>

一会儿url请求格式是: scheme://host/path?传的参数

对应页面获取数据:

kotlin

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
val uri = intent.data
if(uri != null){
val url = uri.toString()
val scheme = uri.scheme
val host = uri.host
val port = uri.port
val path = uri.path
val pathSegments = uri.pathSegments
val query = uri.queryParameterNames

for (key in query){
showText = showText + key + ":" + uri.getQueryParameter(key) + "\n"
}
tv_show.setText(showText)
}

java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
Uri uri = getIntent().getData();  
if (uri != null) {
// 完整的url信息
String url = uri.toString();
Log.e(TAG, "url: " + uri);
// scheme部分
String scheme = uri.getScheme();
Log.e(TAG, "scheme: " + scheme);
// host部分
String host = uri.getHost();
Log.e(TAG, "host: " + host);
//port部分
int port = uri.getPort();
Log.e(TAG, "host: " + port);
// 访问路劲
String path = uri.getPath();
Log.e(TAG, "path: " + path);
List<String> pathSegments = uri.getPathSegments();
// Query部分
String query = uri.getQuery();
Log.e(TAG, "query: " + query);
//获取指定参数值
String goodsId = uri.getQueryParameter("goodsId");
Log.e(TAG, "goodsId: " + goodsId);
}

本文章参考的博客:https://www.cnblogs.com/chaoyuehedy/p/9004224.html

本文也是看这个博客,记录一下。