1. 程式人生 > >Android 通過URL scheme 實現點選瀏覽器中的URL連結,啟動特定的App,並調轉頁面傳遞引數

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

 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