zoukankan      html  css  js  c++  java
  • Android 通过URL scheme 实现点击浏览器中的URL链接,启动特定的App,并调转页面传递参数

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

    1. 首先做成HTML的页面,页面内容格式如下:
    <a href="[scheme]://[host]/[path]?[query]">启动应用程序</a>
    这一句就可以了。
    各个项目含义如下所示:
    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. 接下来是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>

    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. 接下来在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");
       }
    }

    这样就能获取到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);
                }
            }
    
        }
    
    }

    服务器的Json数据

    {'singer':{'daytime':12,'order':'45','title':'订单'}}

    日志输出结果:

    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

    demo链接点击去下载
    http://download.csdn.net/detail/jackron2014/9483691

  • 相关阅读:
    第二阶段冲刺(三)
    第二阶段冲刺(二)
    第二阶段冲刺(一)
    阿里云体验:安装jdk
    知识储备
    wcf服务编程(二)
    wcf服务编程(一)
    操作xml练习
    操作文件简单的方法
    【mongoDB】学习笔记_02
  • 原文地址:https://www.cnblogs.com/ldq2016/p/8618109.html
Copyright © 2011-2022 走看看