Android - JavaScript 界面在 android 3.0 中无法工作

发布于 2024-11-30 19:23:26 字数 2103 浏览 1 评论 0原文

我正在使用 JavaScript 接口来检查 Google 的 StreetView 是否可用。我的问题是,从 android 3.0 代码开始停止工作,我无法找到原因。问题是“JavascriptCheck”接口中的方法永远不会被调用,并且 Logcat 不会显示任何错误。

Java代码:

public void showStreetView(GeoPoint geoPoint) {
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavascriptCheck(), "Android");

    lat = geoPoint.getLatitudeE6()/1E6;
    lon = geoPoint.getLongitudeE6()/1E6;

    webView.loadDataWithBaseURL("", context.getString(R.string.html_streetview, lat, lon), "text/html", "UTF-8", "");

}

public class JavascriptCheck {

    public void hasStreetview(boolean hasStreetview) {
        if (hasStreetview) {
            openStreetView();
        } else {
            Toast.makeText(context, context.getString(R.string.loc_no_street_view), Toast.LENGTH_SHORT).show();
        }
    }

}

布局文件中的WebView:

<WebView android:id="@+id/webView" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:visibility="gone">
</WebView>

JavaScript字符串:

 <string name="html_streetview">
     &lt;html>
         &lt;head>
             &lt;script src=\"http://maps.google.com/maps?file=api&amp;v=2&amp;  sensor=false\" type=\"text/javascript\"/>
         &lt;/head>
         &lt;body>
             &lt;script type=\"text/javascript\">
                 var testPoint = new GLatLng(%1$s, %2$s);
                 var svClient = new GStreetviewClient();

                 svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
                     if ((nearest !== null) &amp;&amp; (testPoint.distanceFrom(nearest) &lt;= 100)) {
                         Android.hasStreetview(true);
                     } else {
                         Android.hasStreetview(false);
                     }
                 });
             &lt;/script>
         &lt;/body>
     &lt;/html>
</string>

I'm using JavaScript interface for checking if Google's StreetView is available. My problem is that from android 3.0 code stopped working, and I am unnable to find why. Problem is that methods from "JavascriptCheck" interface are never called and Logcat doesn't show any errors.

Java code:

public void showStreetView(GeoPoint geoPoint) {
    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webView.addJavascriptInterface(new JavascriptCheck(), "Android");

    lat = geoPoint.getLatitudeE6()/1E6;
    lon = geoPoint.getLongitudeE6()/1E6;

    webView.loadDataWithBaseURL("", context.getString(R.string.html_streetview, lat, lon), "text/html", "UTF-8", "");

}

public class JavascriptCheck {

    public void hasStreetview(boolean hasStreetview) {
        if (hasStreetview) {
            openStreetView();
        } else {
            Toast.makeText(context, context.getString(R.string.loc_no_street_view), Toast.LENGTH_SHORT).show();
        }
    }

}

WebView in layout file:

<WebView android:id="@+id/webView" 
    android:layout_height="wrap_content"
    android:layout_width="wrap_content" 
    android:visibility="gone">
</WebView>

JavaScript string:

 <string name="html_streetview">
     <html>
         <head>
             <script src=\"http://maps.google.com/maps?file=api&v=2&  sensor=false\" type=\"text/javascript\"/>
         </head>
         <body>
             <script type=\"text/javascript\">
                 var testPoint = new GLatLng(%1$s, %2$s);
                 var svClient = new GStreetviewClient();

                 svClient.getNearestPanoramaLatLng(testPoint, function (nearest) {
                     if ((nearest !== null) && (testPoint.distanceFrom(nearest) <= 100)) {
                         Android.hasStreetview(true);
                     } else {
                         Android.hasStreetview(false);
                     }
                 });
             </script>
         </body>
     </html>
</string>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

紧拥背影 2024-12-07 19:23:26

很久以前就解决了我的问题,只是想与其他人分享。 Honeycomb 及更高版本的 Android 要求您使用完整的 html

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">

  var sv = new google.maps.StreetViewService();

  function hasStreet(lat, lon) {
    var point = new google.maps.LatLng(lat, lon);
    sv.getPanoramaByLocation(point, 50, isSVAvailable);
  }

  function isSVAvailable(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
      Android.hasStreetview(true);
    } else {
      Android.hasStreetview(false);
    }
  }
</script>
</head>
<body></body>
</html>

Solved my problem long ago, just wanted to share with others. Honeycomb and later Android versions require that you use full html <script> tags. Also it is better to keep script string in assets folder. My assets/index.html looks like this now:

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<script src="http://maps.googleapis.com/maps/api/js?sensor=false" type="text/javascript"></script>
<script type="text/javascript">

  var sv = new google.maps.StreetViewService();

  function hasStreet(lat, lon) {
    var point = new google.maps.LatLng(lat, lon);
    sv.getPanoramaByLocation(point, 50, isSVAvailable);
  }

  function isSVAvailable(data, status) {
    if (status == google.maps.StreetViewStatus.OK) {
      Android.hasStreetview(true);
    } else {
      Android.hasStreetview(false);
    }
  }
</script>
</head>
<body></body>
</html>
奶气 2024-12-07 19:23:26

我也在使用这个功能,自从我尝试升级 ICS 应用程序以来,它就被破坏了。如果您有外部 src 链接,则 Javascript 似乎不会执行。如果您取出 javascript src 链接并添加一些日志记录,您将看到脚本将运行(并且显然始终返回 false)。

我知道在文档中他们建议不要使用调用本机代码的 javascript,除非您控制 javascript 中的所有元素,但也许现在他们明确停止运行引用外部资源的代码?

I too was using this function and have seen it broken since I tried upgrading my app for ICS. It seems that the Javascript won't execute if you have an external src link. If you take out the javascript src link and add some logging you'll see that the script will run (and obviously return false all the time).

I know in the docs they recommend not using javascript that calls into your native code unless you control all elements in the javascript but perhaps now they explicitly stop code from running that references an external resource?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文