本文共 1655 字,大约阅读时间需要 5 分钟。
公司项目混合应用,h5界面用了百度定位,在手机系统浏览器上会弹出定位权限请求框,但在app的webview上就没响应,查看资料,发现还需要实现WebChromeClient类的onGeolocationPermissionsShowPrompt方法,步骤如下:
1、定位权限申明
2、WebView相关设置
mWebView.getSettings().setGeolocationEnabled(true); private WebChromeClient mWebChromeClient = new WebChromeClient() { @Override public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) { callback.invoke(origin, true, true); } }; mWebView.setWebChromeClient(mWebChromeClient);
这样完了之后有可能onGeolocationPermissionsShowPrompt不会被调用到,看这段源码说明:
/** * Notify the host application that web content from the specified origin * is attempting to use the Geolocation API, but no permission state is * currently set for that origin. The host application should invoke the * specified callback with the desired permission state. See * {@link GeolocationPermissions} for details. * *Note that for applications targeting Android N and later SDKs * (API level > {@link android.os.Build.VERSION_CODES#M}) * this method is only called for requests originating from secure * origins such as https. On non-secure origins geolocation requests * are automatically denied.
* * @param origin The origin of the web content attempting to use the * Geolocation API. * @param callback The callback to use to set the permission state for the * origin. */public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {}
意思是,如果targetSdkVersion设置为21以上,同时如果h5不是https开头,该方法会自动拒绝执行。解决办法是把targetSdkVersion设置为21就可以了。
转载地址:http://plrki.baihongyu.com/