0

我正在尝试构建一个 android 应用程序,我相处得很好,但我在通过 webview 查看的网页调用 javascript 函数时遇到问题。

我使用本教程在 webview 中获取地理位置:http: //turbomanage.wordpress.com/2012/04/23/how-to-enable-geolocation-in-a-webview-android/

现在我想在我的页面中调用这个函数:

var t=setTimeout("navigator.geolocation.getCurrentPosition(foundLocation);",15000); function foundLocation(position) { var lat = position.coords.latitude; var long = position.coords.longitude; window.location.href='http://rittenservice.nl/rittensysteem2.php?lat='+lat+'&long='+long+''; }

调用此函数时,它应该使用 url 重新加载页面,以便我可以使用 PHP 保存位置

有谁知道为什么它不起作用?

网页浏览代码:

public class GeoWebViewActivity extends Activity {
public class GeoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.startsWith("androidurl://")) {
            url = url.replaceAll("androidurl://", "http://");
        }
        // When user clicks a hyperlink, load in the existing WebView
        view.loadUrl(url);
        return true;
    }
}

/**
 * WebChromeClient subclass handles UI-related calls
 * Note: think chrome as in decoration, not the Chrome browser
 */
public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
            GeolocationPermissions.Callback callback) {
        // Always grant permission since the app itself requires location
        // permission and the user has therefore already granted it
        callback.invoke(origin, true, false);
    }
}

WebView mWebView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_geo_web_view);
    mWebView = (WebView) findViewById(R.id.webView1);
    // Brower niceties -- pinch / zoom, follow links in place
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.setWebViewClient(new GeoWebViewClient());
    // Below required for geolocation
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.setWebChromeClient(new GeoWebChromeClient());
    // Load google.com
    mWebView.loadUrl("http://www.rittenservice.nl/keypad.php");
}

@Override
public void onBackPressed() {
    // Pop the browser back stack or exit the activity
    if (mWebView.canGoBack()) {
        mWebView.goBack();
    }
    else {
        super.onBackPressed();
    }
}

}

4

1 回答 1

0
mWebView.getSettings().setJavaScriptEnabled(true);

在显示 Web 视图之前添加该代码。这允许 javascript 在 webview 中运行

于 2013-10-25T08:56:35.177 回答