Android应用和网站javascript

时间:2013-10-25 08:43:24

标签: javascript android webview geolocation

我正在尝试构建一个Android应用程序并且我相处得很好,但是我在通过网页浏览的网页上调用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保存位置

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

webview代码:

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();
    }
}

}

1 个答案:

答案 0 :(得分:0)

mWebView.getSettings().setJavaScriptEnabled(true);

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