標籤:geolocation ongeolocationpermiss webchromeclient
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
private WebViewwebView =null;
private WebSettingswebSettings =null;
private void initWeb() {
webView = (WebView)this.findViewById(R.id.webView_load);
webView.getSettings().setJavaScriptEnabled(true);
loadURL(urlStr);
setClient();
webSettings =webView.getSettings();// 網頁設定預設屬性
webView.setInitialScale(39);// 適應豎屏
webView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);// 去掉底部和右邊的捲軸
// webSettings.setRenderPriority(RenderPriority.HIGH);// 提高渲染優先順序
webSettings.setJavaScriptEnabled(true);// 設定是否可以互動Javascript
webSettings.setAllowFileAccess(true);// 啟用或禁止WebView訪問檔案資料
webSettings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);// 設定顯示模式
webSettings.setDefaultZoom(ZoomDensity.MEDIUM);// 適應螢幕
// webView.getSettings().setUseWideViewPort(true);// 是否任意比例縮放
webView.getSettings().setLoadWithOverviewMode(true);// 縮放至螢幕的大小
// webView.getSettings().setUseWideViewPort(true);// 是否任意比例縮放
webSettings.setBuiltInZoomControls(false);// 設定是否支援縮放
// webSettings.setSupportZoom(false);// 設定是否支援變焦,僅支援雙擊縮放
webSettings.setNeedInitialFocus(false);// 設定是否可以訪問檔案
// webSettings.setBlockNetworkImage(true);// 載入圖片放在最後載入渲染
// webSettings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);//
// 設定緩衝模式
webSettings.setCacheMode(webSettings.LOAD_NO_CACHE);// 不使用緩衝
webView.addJavascriptInterface(new Scan2(),"mMap");// 這裡的名稱就是在頁面window.fxl.testFunction()所取的別名
webView.addJavascriptInterface(new Scan2(),"phone");// 這裡的名稱就是在頁面window.fxl.testFunction()所取的別名
//關鍵setGeolocationEnabled
webView.getSettings().setGeolocationEnabled(true);
}
private void setClient() {
webView.setWebChromeClient(new WebChromeClient() {
@Override
publicvoid onGeolocationPermissionsShowPrompt(String origin,
android.webkit.GeolocationPermissions.Callback callback) {
callback.invoke(origin,true, false);
super.onGeolocationPermissionsShowPrompt(origin, callback);
}
}
android webview 擷取Geolocation