您的位置:首页 > 移动开发

Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数

2016-12-06 02:36 916 查看
http://blog.csdn.net/jackron2014/article/details/51085446
http://blog.csdn.net/jackron2014/article/details/51085446 http://blog.csdn.net/jackron2014/article/details/51085446 http://blog.csdn.net/jackron2014/article/details/51085446
 


Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数

标签: URLscheme
2016-04-07 14:08 1824人阅读 评论(0) 收藏 举报


 分类:

Android(20) 


版权声明:本文为博主原创文章,未经博主允许不得转载。

点击浏览器中的URL链接,启动特定的App。
首先做成HTML的页面,页面内容格式如下:
<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>
1
1
这一句就可以了。
各个项目含义如下所示:
scheme:判别启动的App。 ※详细后述
host:适当记述
path:传值时必须的key     ※没有也可以
query:获取值的Key和Value  ※没有也可以

作为测试好好写了一下,如下:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<br/>
<a href="fyfeng://Panda/test1?name=http://124.200.36.22:8000/test/namelist.json&age=26">打开app</a><br/>
<br/>
<a href="fyfeng://Panda/test2?name=zhangsan&age=27">打开app1</a><br/>
<br/>
<a href="fyfeng://Panda/test3?name=zhangsan&age=28">打开app2</a><br/>
<br/>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
接下来是Android端。 

首先在AndroidManifest.xml的MAIN Activity下追加以下内容。(启动Activity时给予) 

※必须添加项
<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/>
</intent-filter>
1
2
3
4
5
6
1
2
3
4
5
6

HTML记述的内容加入
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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="myapp" android:host="jp.app" android:pathPrefix="/openwith"/> </intent-filter>
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10

可以复制代码,这样的话,没有问题。
接下来在Activity中需要取值的地方添加以下代码,我是直接写在OnCreate函数里的:
Intent i_getvalue = getIntent();
String action = i_getvalue.getAction();

if(Intent.ACTION_VIEW.equals(action)){

Uri uri = i_getvalue.getData();
if(uri != null){
String name = uri.getQueryParameter("name");
String age= uri.getQueryParameter("age");
}
}
1
2
3
4
5
6
7
8
9
10
11
1
2
3
4
5
6
7
8
9
10
11

这样就能获取到URL传递过来的值了。 

我在测试的超链接上添加name的属性是一个需要请求服务器的URL,这样的话,我们可以通过根据这个URL请求服务器,获取到一些我们所需要的数据,这个数据的是以Json的形式存在,通过volley请求下来数据,并使用Gson解析后得到数据。
public static void getUrlValue(Intent i_getvalue, final String tag, Context context) {
String action = i_getvalue.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Uri uri = i_getvalue.getData();
if (uri != null) {
mRequestQueue = Volley.newRequestQueue(context);
final String name = uri.getQueryParameter("name");
// System.out.print(name);
// final String name = "http://124.200.36.22:8000/test/namelist.json";
String age = uri.getQueryParameter("age");
String host = uri.getHost();
String dataString = i_getvalue.getDataString();
String id = uri.getQueryParameter("id");
String path = uri.getPath();
String path1 = uri.getEncodedPath();
String queryString = uri.getQuery();
new Thread(new Runnable() {
@Override
public void run() {
if (name != null) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(name.toString().trim(), null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Gson gson = new Gson();
Singersin=gson.fromJson(response.toString(),Singer.class);
daytime= sin.getSinger().getDaytime();                               order=sin.getSinger().getOrder();                                           title=sin.getSinger().getTitle();
Log.d("TAG", response.toString());
Log.i(tag, "name:" + name);
Log.i(tag, "time:" + daytime);
Log.i(tag, "order:" + order);
Log.i(tag, "title:" + title);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("TAG", error.getMessage(), error);
}
});                            mRequestQueue.add(jsonObjectRequest);
}
}
}).start();

Log.i(tag, "dataString:" + dataString);
Log.i(tag, "id:" + id);
Log.i(tag, "path:" + path);
Log.i(tag, "path1:" + path1);
Log.i(tag, "queryString:" + queryString);
Log.i(tag, "name" + name + " age" + age);
}
}

}

}
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56

服务器的Json数据
{'singer':{'daytime':12,'order':'45','title':'订单'}}
1
1

日志输出结果:
D/TAG: {"singer":{"daytime":12,"order":"45","title":"订单"}}
name:http://124.200.36.22:8000/test/namelist.json
time:12
order:45
title:订单
path:/test1
path:/test2
1
2
3
4
5
6
7
1
2
3
4
5
6
7

demo链接点击去下载 
http://download.csdn.net/detail/jackron2014/9483691
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐