private synchronized void initWebSettings() { if (android.os.Build.VERSION.SDK_INT >= 21) WebView.enableSlowWholeDocumentDraw(); WebSettings webSettings = getSettings(); userAgentOriginal = webSettings.getUserAgentString(); webSettings.setAllowContentAccess(true); webSettings.setAllowFileAccess(true); webSettings.setAllowFileAccessFromFileURLs(true); webSettings.setAllowUniversalAccessFromFileURLs(true); webSettings.setAppCacheEnabled(true); webSettings.setAppCachePath(context.getCacheDir().toString()); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); webSettings.setDatabaseEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setGeolocationDatabasePath(context.getFilesDir().toString()); webSettings.setSupportZoom(true); webSettings.setBuiltInZoomControls(true); webSettings.setDisplayZoomControls(false); webSettings.setDefaultTextEncodingName(BrowserUnit.URL_ENCODING); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { webSettings.setLoadsImagesAutomatically(true); } else { webSettings.setLoadsImagesAutomatically(false); } }
public static void initializeSettings(WebSettings settings, Context context) { settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setUserAgentString(settings.getUserAgentString() + DB.USER_AGENT); settings.setSupportMultipleWindows(true); settings.setSaveFormData(false); settings.setSavePassword(false); // 设置为true,系统会弹出AlertDialog确认框 if (API < 18) { settings.setAppCacheMaxSize(Long.MAX_VALUE); } if (API < 17) { settings.setEnableSmoothTransition(true); } if (API < 19) { settings.setDatabasePath(context.getFilesDir().getAbsolutePath() + "/databases"); } settings.setDomStorageEnabled(true); settings.setAppCachePath(context.getCacheDir().toString()); settings.setAppCacheEnabled(true); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setGeolocationDatabasePath(context.getCacheDir().getAbsolutePath()); settings.setAllowFileAccess(true); settings.setDatabaseEnabled(true); settings.setSupportZoom(true); settings.setBuiltInZoomControls(true); settings.setDisplayZoomControls(false); settings.setAllowContentAccess(true); settings.setDefaultTextEncodingName("utf-8"); /*if (API > 16) { settings.setAllowFileAccessFromFileURLs(false); settings.setAllowUniversalAccessFromFileURLs(false); }*/ }
private void initializeSettings(WebSettings settings) { settings.setJavaScriptEnabled(true); // configure local storage apis and their database paths. settings.setAppCachePath(getDir("appcache", 0).getPath()); settings.setGeolocationDatabasePath(getDir("geolocation", 0).getPath()); settings.setDatabasePath(getDir("databases", 0).getPath()); settings.setAppCacheEnabled(true); settings.setGeolocationEnabled(true); settings.setDatabaseEnabled(true); settings.setDomStorageEnabled(true); }
@SuppressLint("SetJavaScriptEnabled") @SuppressWarnings("deprecation") private void initWebViewSettings() { this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); // TODO: The Activity is the one that should call requestFocus(). if (shouldRequestFocusOnInit()) { this.requestFocusFromTouch(); } this.setInitialScale(0); this.setVerticalScrollBarEnabled(false); if (shouldRequestFocusOnInit()) { this.requestFocusFromTouch(); } // Enable JavaScript final WebSettings settings = this.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL); // Set the nav dump for HTC 2.x devices (disabling for ICS, deprecated entirely for Jellybean // 4.2) try { Method gingerbread_getMethod = WebSettings.class.getMethod("setNavDump", new Class[] {boolean.class}); String manufacturer = android.os.Build.MANUFACTURER; Log.d(TAG, "CordovaWebView is running on device made by: " + manufacturer); if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB && android.os.Build.MANUFACTURER.contains("HTC")) { gingerbread_getMethod.invoke(settings, true); } } catch (NoSuchMethodException e) { Log.d(TAG, "We are on a modern version of Android, we will deprecate HTC 2.3 devices in 2.8"); } catch (IllegalArgumentException e) { Log.d(TAG, "Doing the NavDump failed with bad arguments"); } catch (IllegalAccessException e) { Log.d( TAG, "This should never happen: IllegalAccessException means this isn't Android anymore"); } catch (InvocationTargetException e) { Log.d( TAG, "This should never happen: InvocationTargetException means this isn't Android anymore."); } // We don't save any form data in the application settings.setSaveFormData(false); settings.setSavePassword(false); // Jellybean rightfully tried to lock this down. Too bad they didn't give us a whitelist // while we do this if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN) { Level16Apis.enableUniversalAccess(settings); } if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR1) { Level17Apis.setMediaPlaybackRequiresUserGesture(settings, false); } // Enable database // We keep this disabled because we use or shim to get around DOM_EXCEPTION_ERROR_16 String databasePath = getContext().getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); settings.setDatabaseEnabled(true); settings.setDatabasePath(databasePath); // Determine whether we're in debug or release mode, and turn on Debugging! ApplicationInfo appInfo = getContext().getApplicationContext().getApplicationInfo(); if ((appInfo.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) { enableRemoteDebugging(); } settings.setGeolocationDatabasePath(databasePath); // Enable DOM storage settings.setDomStorageEnabled(true); // Enable built-in geolocation settings.setGeolocationEnabled(true); // Enable AppCache // Fix for CB-2282 settings.setAppCacheMaxSize(5 * 1048576); settings.setAppCachePath(databasePath); settings.setAppCacheEnabled(true); // Fix for CB-1405 // Google issue 4641 settings.getUserAgentString(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(Intent.ACTION_CONFIGURATION_CHANGED); if (this.receiver == null) { this.receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { settings.getUserAgentString(); } }; getContext().registerReceiver(this.receiver, intentFilter); } // end CB-1405 }
// 封装WebView public static void wrapWebView(Context context, WebView webView, WebViewClient webclient) { WebSettings webSettings = webView.getSettings(); webSettings.setSavePassword(false); webSettings.setSaveFormData(false); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setSupportZoom(false); // 设置定位可用 if (context != null) { webSettings.setDatabaseEnabled(true); String dir = context.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath(); // 启用地理定位 webSettings.setGeolocationEnabled(true); // 设置定位的数据库路径 webSettings.setGeolocationDatabasePath(dir); // 最重要的方法,一定要设置,这就是出不来的主要原因 webSettings.setDomStorageEnabled(true); } // webView.setWebChromeClient( new WebChromeClient() { @Override public void onGeolocationPermissionsShowPrompt(String origin, Callback callback) { callback.invoke(origin, true, false); super.onGeolocationPermissionsShowPrompt(origin, callback); } }); webView.setWebViewClient(webclient); // webView.setWebViewClient(new WebViewClient() { // // // 加载链接 // @Override // public boolean shouldOverrideUrlLoading(WebView view, String url) { // view.loadUrl(url); // return true; // } // // // 链接开始加载时 // @Override // public void onPageStarted(WebView view, String url, Bitmap favicon) { // super.onPageStarted(view, url, favicon); // if (progress != null) { // progress.setVisibility(View.VISIBLE); // } // } // // // 链接加载完成 // @Override // public void onPageFinished(WebView view, String url) { // super.onPageFinished(view, url); // if (progress != null) { // progress.setVisibility(View.GONE); // } // } // // @Override // public void onReceivedSslError(WebView view, SslErrorHandler handler, // SslError error) { // super.onReceivedSslError(view, handler, error); // } // // // webview无网络情况下的人性化处理 // @Override // public void onReceivedError(WebView view, int errorCode, String // description, String failingUrl) { // super.onReceivedError(view, errorCode, description, failingUrl); // view.loadData("", "text/html", "utf-8"); // Toast.makeText(view.getContext(), "网络不给力", // android.widget.Toast.LENGTH_LONG).show(); // } // }); }
@SuppressLint("SetJavaScriptEnabled") @TargetApi(Build.VERSION_CODES.JELLY_BEAN) @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { // get url and navigatio preference (default true) final String url = getIntent().getStringExtra(getPackageName() + ".URL"); final boolean navigation = getIntent().getBooleanExtra(getPackageName() + ".NAVIGATION", true); setContentView(R.layout.webviewnav); // if we want navigation bar and not hide it, make it visible. Make it invisible if not. if (navigation && !hideNavigationBar) { findViewById(R.id.webBottomBar).setVisibility(View.VISIBLE); } else { findViewById(R.id.webBottomBar).setVisibility(View.GONE); } mButtonBack = (ImageButton) findViewById(R.id.buttonWebBack); mButtonForward = (ImageButton) findViewById(R.id.buttonWebForward); mButtonStop = (ImageButton) findViewById(R.id.buttonWebStop); mProgressView = (ProgressBar) findViewById(R.id.progressBar); mProgressView.setIndeterminate(true); // init webview mWebView = (WebView) findViewById(R.id.webView); // disable hw accel as it creates flickering pages, html5 video won't work with this if (Build.VERSION.SDK_INT >= 11 && Build.VERSION.SDK_INT < 16) mWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); // This hides white bar on the right mWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); WebSettings settings = mWebView.getSettings(); // enable plugins before java script settings.setPluginState(PluginState.ON); // TODO test these two settings with a big webpage (cafe lotty?) settings.setLoadWithOverviewMode(true); settings.setUseWideViewPort(true); // enable javascript and zoom controls settings.setJavaScriptEnabled(true); settings.setBuiltInZoomControls(true); settings.setGeolocationEnabled(true); settings.setDatabaseEnabled(true); String databasePath = getDir("database_ext", Context.MODE_PRIVATE).getPath(); settings.setDatabasePath(databasePath); settings.setGeolocationDatabasePath(databasePath); settings.setDomStorageEnabled(true); settings.setAppCacheEnabled(true); // allow XMLHttpRequests if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) settings.setAllowUniversalAccessFromFileURLs(true); MetaioWebViewClient client = new MetaioWebViewClient(); mWebView.setWebViewClient(client); mWebView.setWebChromeClient(new MetaioWebChromeClient()); registerForContextMenu(mWebView); if (savedInstanceState != null) { mWebView.restoreState(savedInstanceState); } else { // if we don't have to override the url, load it in the webview if (!client.shouldOverrideUrlLoading(mWebView, url)) mWebView.loadUrl(url); } } catch (Exception e) { MetaioCloudPlugin.log(Log.ERROR, "WebViewActivity.onCreate", e); } }