从Android WebView调用java方法

Calling java method from Android WebView

本文关键字:java 方法 调用 WebView Android      更新时间:2023-10-24

我想在我的android应用程序中显示一个html图像地图。为此,我使用WebView,并且我希望当单击<area>时,单击的区域可以做一些事情。例如,若我使用踢打或触摸的"北卡罗来纳州",我会在祝酒词中显示它的名字。

我的代码是:

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
      <title>map_usa_856</title>
  </head>
  <body>
    <img src="usamap.png" width="320" height="266" style="margin:0 auto; display:block;" usemap="#Map" border="0" />
    <map name="Map" id="Map">
      <area shape="poly" href="North Carolina" alt="North Carolina" title="North Carolina" coords="266,109,267,107,270,107,270,105,273,102,278,100,277,98,275,98,276,96,278,96,280,93,278,91,277,91,278,90,277,88,245,93,244,97,242,97,239,100,236,102,233,102,232,104,231,104,231,105,237,104,240,102,250,102,250,102,252,104,258,102">
      <area shape="poly" href="North Dakota" alt="North Dakota" title="North Dakota" coords="119,17,152,17,155,38,118,36">
      <area shape="poly" href="Rhode Island" alt="Rhode Island" title="Rhode Island" coords="289,48,291,53,292,52,302,54,302,57,311,57,311,51,302,51,302,53,292,51,291,48">
      <area shape="poly" href="Illinois" alt="Illinois" title="Illinois" coords="208,79,207,80,208,82,207,85,205,87,205,91,203,93,203,93,202,93,200,93,200,95,198,93,199,90,192,87,194,82,191,82,190,82,190,80,185,77,185,75,187,69,187,67,190,67,192,62,189,60,204,59,204,60,206,62">
      <area shape="poly" href="New Mexico" alt="New Mexico" title="New Mexico" coords="117,96,83,93,78,130,83,131,84,127,92,129,92,127,115,129">     <area shape="poly"  alt="Georgia" title="Georgia" coords="255,123,252,123,252,120,250,119,249,117,247,117,246,114,242,114,241,111,240,109,237,107,236,107,236,105,222,107,227,121,231,125,229,127,229,130,229,132,231,134,247,133,247,134,249,131,252,131,252,128,252,125,254,124">
    </map>
  </body>
</html>

我发现了这个:

webview.setWebViewClient(new WebViewClient(){
    public void onPageFinished(WebView view, String url){
        Log.e("String HrefValue :-",url);
    }
});

但它并没有给我想要的答案。你能帮忙吗?

根据w3c规范,<area>的href属性必须是有效的URL。显然,"北卡罗来纳州"不是一个有效的URL。这是你的首要问题。

现在,当用户单击<area>时,您需要显示Toast消息。显示Toast消息必须用java代码完成,Android为您提供了一种从javascript调用某些java方法的非常简单的方法。

在活动代码中:编写一个显示Toast的方法,并用@JavascriptInterface:对其进行注释

/** Show a toast from the web page. (this method will be called by javascript) */
@JavascriptInterface
public void showToast(String areaName) {
    Toast.makeText(mContext, "You clicked on "+areaName, Toast.LENGTH_SHORT).show();
}

您还必须:

  • 在web视图中启用javascript
  • 在webview中注入您的javascript接口
WebView webView = (WebView) findViewById(R.id.webview);
//enable javascript
webView.getSettings().setJavaScriptEnabled(true);
//all methods annotated with @JavascriptInterface are callable 
//from javascript by using the "Android" object.
webView.addJavascriptInterface(this, "Android"); 

最后,在您的html代码中:使用正确的url为<area href=...>:调用javascript Android.showToast(...)

  <area shape="poly" href="javascript:Android.showToast('North Carolina');return false;" alt="North Carolina" title="North Carolina" coords="266,109,267,107,270,107,270,105,273,102,278,100,277,98,275,98,276,96,278,96,280,93,278,91,277,91,278,90,277,88,245,93,244,97,242,97,239,100,236,102,233,102,232,104,231,104,231,105,237,104,240,102,250,102,250,102,252,104,258,102">