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

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
  1824人阅读  评论(0)  收藏  举报
  分类:

点击浏览器中的URL链接,启动特定的App。

  1. 首先做成HTML的页面,页面内容格式如下:
<a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>
   
   
  • 1
  • 1
这一句就可以了。
各个项目含义如下所示:
scheme:判别启动的App。 ※详细后述
host:适当记述
path:传值时必须的key     ※没有也可以
query:获取值的Key和Value  ※没有也可以
  1. 作为测试好好写了一下,如下:
<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
  1. 接下来是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

可以复制代码,这样的话,没有问题。

  1. 接下来在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



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值