@SuppressLint({"NewApi", "SetJavaScriptEnabled"}) private void loadWebView() { // 实例化WebView对象 // webview = new MyWebView(WebViewActivity.this); LogUtil.i(TAG, "loadWebView===实例化WebView==="); webview = (MyWebView) findViewById(R.id.id_webview); new MobclickAgentJSInterface(this, webview); WebSettings webSettings = webview.getSettings(); // 设置WebView属性,能够执行Javascript脚本 webSettings.setJavaScriptEnabled(true); webSettings.setJavaScriptCanOpenWindowsAutomatically(true); // webSettings.setUseWideViewPort(true); // 关键点 webSettings.setLoadWithOverviewMode(true); webSettings.setAppCacheEnabled(true); webSettings.setAppCacheMaxSize(8 * 1024 * 1024); // 8MB // webSettings.setAppCachePath(Constants.WEBVIEW_CACHE_DIR ); String appCacheDir = this.getApplicationContext().getDir("cache", Context.MODE_PRIVATE).getPath(); webSettings.setAppCachePath(appCacheDir); webSettings.setAllowFileAccess(true); webSettings.setDomStorageEnabled(true); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); // js调用安卓方法 webview.addJavascriptInterface(this, "RedirectListner"); }
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); }*/ }
public static AlertDialog showAlertGetMoreApps(final Activity activity, String urlPage) { AlertDialog.Builder alert = new AlertDialog.Builder(activity); WebView wv = new WebView(activity); WebSettings settings = wv.getSettings(); settings.setCacheMode(WebSettings.LOAD_DEFAULT); settings.setJavaScriptEnabled(true); settings.setAppCacheMaxSize(50000 * 1024); progressBar1 = new ProgressBar(activity); // ,null, android.R.attr.progressBarStyleHorizontal); progressBar1.setLayoutParams( new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT)); wv.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Intent i = new Intent(Intent.ACTION_VIEW); i.setData(Uri.parse(url)); activity.startActivity(i); return true; } }); wv.setWebChromeClient( new WebChromeClient() { public void onProgressChanged(WebView view, int progress) { if (progress < 95) { progressBar1.setVisibility(View.VISIBLE); if (progressBar1.getProgress() < progress) { progressBar1.setProgress(progress); } } else { view.setVisibility(View.VISIBLE); progressBar1.setVisibility(View.GONE); } } }); wv.loadUrl(urlPage); LinearLayout linearLayout = new LinearLayout(activity); if (android.os.Build.VERSION.SDK_INT >= 17) { // call something for API Level 11+ linearLayout.setLayoutDirection(LinearLayout.LAYOUT_DIRECTION_LTR); } linearLayout.addView(progressBar1); linearLayout.addView(wv); alert.setView(linearLayout); alert.setNegativeButton( "Close", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int id) { dialog.dismiss(); } }); // alert.setIcon(android.R.drawable.box_new) return alert.show(); }
@SuppressLint("SetJavaScriptEnabled") public void configureSetting(WebViewBridge webViewBridge) { WebSettings settings = webViewBridge.getSettings(); settings.setJavaScriptEnabled(true); settings.setJavaScriptCanOpenWindowsAutomatically(true); settings.setGeolocationEnabled(true); settings.setDomStorageEnabled(true); File databasePath = new File(mContext.getCacheDir(), "webstorage"); settings.setDatabasePath(databasePath.toString()); settings.setAppCacheEnabled(true); settings.setAppCacheMaxSize(1024 * 1024 * 4); }
@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 }