简体   繁体   English

Android WebView地理定位无法正常工作

[英]Android WebView geolocation not working

My application is displaying a WebView which is requesting for user's geolocation. 我的应用程序正在显示请求用户地理位置的WebView The onGeolocationPermissionsShowPrompt callback in my WebChromeClient is called, but the JS callback is never called in response. 调用我的WebChromeClientonGeolocationPermissionsShowPrompt回调,但从不调用JS回调。 Here's my Java code: 这是我的Java代码:

protected void onCreate(Bundle savedInstanceState) {
    // Binding views…

    webview.getSettings().setJavaScriptEnabled(true);
    webview.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webview.getSettings().setGeolocationEnabled(true);
    webview.setWebChromeClient(new GeoWebChromeClient());

    webview.loadUrl(BASE_URL);
}

private class GeoWebChromeClient extends WebChromeClient {
    @Override
    @DebugLog
    public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
        callback.invoke(origin, true, true);
    }
}

As expected, onGeolocationPermissionsShowPrompt is called once, the first time the web page requests for user's position, then isn't called anymore. 正如预期的那样, onGeolocationPermissionsShowPrompt被调用一次,第一次网页请求用户的位置,然后不再被调用。 But either on the first or the following position requests, the JS callback is never called. 但无论是在第一个还是以下的位置请求中,都不会调用JS回调。

Edit: I just tested with an other application requesting the position in background. 编辑:我刚刚测试了其他应用程序请求后台位置。 It worked. 有效。 My application hass the following permissions (I'm pretty sure most of them are useless for this, but…): 我的应用程序具有以下权限(我很确定它们中的大多数都没用,但是......):

<uses-permission android:name="android.permission.ACCESS_GPS" />
<uses-permission android:name="android.permission.ACCESS_ASSISTED_GPS" />
<uses-permission android:name="android.permission.ACCESS_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Yes it's true : onGeolocationPermissionsShowPrompt is called once, the first time the web page requests for user's location, then it's not called anymore. 是的,这是真的:onGeolocationPermissionsShowPrompt被调用一次,第一次网页请求用户的位置,然后它不再被调用。 I am also facing the same issue. 我也面临同样的问题。 One workaround could be reload the page after location permission is granted. 一个解决方法是在授予位置权限后重新加载页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM