如何經過Html網頁調用本地安卓app?

如何使用html網頁和本地app進行傳遞數據呢?通過研究,發現仍是有方法的,總結了一下,大體有一下幾種方式html

        更新一下吧,這篇日誌寫於2013年11月,離如今已經好久了,依然不少朋友在查閱。目前應該有更新的技術。你們也去補充一下。另外評論裏面有朋友說只有webkit內核的瀏覽器能夠使用,這個我沒有去作過驗證,你們使用的時候,能夠參考一下。android

                ---update in 2015.11.13web

  

1、經過html頁面打開Android本地的app瀏覽器

 

一、首先在編寫一個簡單的html頁面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/">打開app</a><br/>

    </body>

</html>

二、在Android本地app的配置ide

在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>

示例截圖以下:ui

image

 

而後使用「手機瀏覽器」或者「webview」的方式打開這個本地的html網頁,點擊「打開APP」便可成功開啓本地的指定的appurl

 

2、如何經過這個方法獲取網頁帶過來的數據spa

 

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

咱們能夠使用上述的方法,把一些數據傳給本地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;
  }
});
相關文章
相關標籤/搜索