Android 应用程序和网站 JavaScript

Android app and website javascript

本文关键字:JavaScript 网站 应用程序 Android      更新时间:2023-09-26

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

我使用本教程在网络视图中获取地理位置: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();
    }
}

}

mWebView.getSettings().setJavaScriptEnabled(true);

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