关于Android 中webView 中定位失败的原因

xiaoxiao2021-02-28  46

最近做项目调用H5页面,而在页面中有打卡上班的功能,所以要定位..然后当我一切准备就绪当我测试的时候问题出现了,没错,定位失败,,而Ios正常,无意间测试发现后台给我的链接 我用微信打开是正常的,用qq打开是失败的.后来经过一系列的百度和问大佬,终于解决了..下面进入正题吧!

-------------------------------------------------------------------------我割---------------------------------------------------------------------------

第一种在清单文件中配置

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

第二种是在WebChromeClient中实现的方法

//配置权限 private class MyWbChromeClient extends WebChromeClient { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); super.onGeolocationPermissionsShowPrompt(origin, callback); } }

然后是主要代码

wv_content.getSettings().setJavaScriptEnabled(true); wv_content.getSettings().setUseWideViewPort(true); wv_content.getSettings().setLoadWithOverviewMode(true); wv_content.getSettings().setDatabaseEnabled(true); String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); wv_content.getSettings().setGeolocationDatabasePath(dir); wv_content.getSettings().setDomStorageEnabled(true); // 开启 Application Caches 功能 wv_content.getSettings().setAppCacheEnabled(true); wv_content.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { // TODO Auto-generated method stub //返回值是true的时候控制去WebView打开,为false调用系统浏览器或第三方浏览器 view.loadUrl(url1); return true; } }); wv_content.setWebChromeClient(new MyWbChromeClient()); } private class MyWbChromeClient extends WebChromeClient { @Override public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) { callback.invoke(origin, true, false); super.onGeolocationPermissionsShowPrompt(origin, callback); } } @Override protected void onDestroy() { super.onDestroy(); wv_content.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); } 好了,,问题解决了..很显然我这次写博客距离上次已经半年了....

转载请注明原文地址: https://www.6miu.com/read-82123.html

最新回复(0)