通過html調起app,並傳遞數據

一、通過html頁面打開Android本地的app

 

1、首先在編寫一個簡單的html頁面

複製代碼
<html>

    <head>

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
        <title>Insert title here</title>

    </head>

    <body>

        <a href="m://my.com/">打開app</a><br/>

    </body>

</html>
複製代碼

2、在Android本地app的配置

複製代碼
在AndroidManifest的清單文件裏的intent-filte中加入如下元素:
 <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:host="my.com" 
                    android:scheme="m" />
</intent-filter>
複製代碼

示例截圖如下:

image

 

然後使用“手機瀏覽器”或者“webview”的方式打開這個本地的html網頁,點擊“打開APP”即可成功開啓本地的指定的app

 

二、如何通過這個方法獲取網頁帶過來的數據

 

只能打開就沒什麼意思了,最重要的是,我們要傳遞數據,那麼怎麼去傳遞數據呢?

我們可以使用上述的方法,把一些數據傳給本地app,那麼首先我們更改一下網頁,代碼修改後:

複製代碼
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert title here</title>
    </head>
    <body>
        <a href="m://my.com/?arg0=0&arg1=1">打開app</a><br/>
    </body>
</html>
複製代碼

(1).假如你是通過瀏覽器打開這個網頁的,那麼獲取數據的方式爲:

Uri uri = getIntent().getData();  String test1= uri.getQueryParameter("arg0");  String test2= uri.getQueryParameter("arg1");

(2)如果使用webview訪問該網頁,獲取數據的操作爲:

複製代碼
webView.setWebViewClient(new WebViewClient(){
  @Override
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
      Uri uri=Uri.parse(url);
          if(uri.getScheme().equals("m")&&uri.getHost().equals("my.com")){
              String arg0=uri.getQueryParameter("arg0");
              String arg1=uri.getQueryParameter("arg1");
             
          }else{
              view.loadUrl(url);
          }
      return true;
  }

});

此文爲轉載, 原文地址:http://www.cnblogs.com/yejiurui/p/3413796.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章