1. 程式人生 > >關於Android 中webView 中定位失敗的原因

關於Android 中webView 中定位失敗的原因

最近做專案呼叫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);
    }

好了,,問題解決了..很顯然我這次寫部落格距離上次已經半年了....