/** * 初始化内容控件 */ private void initWeb() { // TODO Auto-generated method stub String release = android.os.Build.VERSION.RELEASE; release = release.substring(0, 3); if ("4.4".equals(release)) { web_content.setWebViewClient(new MyWebViewClient()); } else { web_content.setVisibility(View.VISIBLE); WebSettings ws = web_content.getSettings(); ws.setJavaScriptEnabled(true); ws.setAllowFileAccess(true); ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); ws.setDefaultTextEncodingName("utf-8"); ws.setTextSize(TextSize.NORMAL); ws.setAppCacheEnabled(false); ws.setDomStorageEnabled(true); if (android.os.Build.VERSION.SDK_INT >= 8) { ws.setPluginState(PluginState.ON); } ws.setRenderPriority(RenderPriority.HIGH); web_content.setWebViewClient(new WebViewClientDemo()); web_content.setWebChromeClient(new WebViewChromeClientDemo()); web_content.loadDataWithBaseURL("", unit.getIntroduce(), "text/html", "utf-8", null); } }
/** * 初始化 * * @param web_view * @param activity * @return */ public static WebView WebSettingInit(WebView web_view, final Activity activity) { WebSettings ws = web_view.getSettings(); ws.setJavaScriptEnabled(true); // 支持js ws.setBuiltInZoomControls(false); // 支持缩放按钮 ws.setUseWideViewPort(true); // 设置此属性,可任意比例缩放 将图片调整到适合webview的大小 ws.setLoadWithOverviewMode(true); // 缩放至屏幕的大小 ws.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); ws.setSupportZoom(false); // 支持缩放 // 设置 缓存模式 ws.setCacheMode(WebSettings.LOAD_NO_CACHE); // 关闭webview中缓存 web_view.clearCache(true); web_view.setTag(true); // 开启 DOM storage API 功能 ws.setDomStorageEnabled(true); ws.setRenderPriority(RenderPriority.HIGH); // 开启 database storage API 功能 ws.setDatabaseEnabled(false); web_view.setWebChromeClient( new WebChromeClient() { @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { return super.onJsAlert(view, url, message, result); } }); return web_view; }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.oauthactivity_layout); ActionBar actionBar = getActionBar(); actionBar.setDisplayHomeAsUpEnabled(false); actionBar.setTitle(getString(R.string.login)); webView = (WebView) findViewById(R.id.webView); webView.setWebViewClient(new WeiboWebViewClient()); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setSaveFormData(false); settings.setSavePassword(false); settings.setCacheMode(WebSettings.LOAD_NO_CACHE); settings.setRenderPriority(WebSettings.RenderPriority.HIGH); CookieSyncManager.createInstance(this); CookieManager cookieManager = CookieManager.getInstance(); cookieManager.removeAllCookie(); }
/** * Have to be called from WebView.WebViewClient.onPageFinished ex : mWebView.setWebViewClient(new * WebViewClient() { onPageFinished(WebView view, String url) { [HERE] }}); Inject a header to a * webview : add a margin-top="**dpx" Had to have a transparent background with a placeholder on * top So inject js for placeholder and setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); for * transparency TODO : inject JavaScript for Pre-Lolipop with loadUrl("js:...") * * @param webView * @param withAnimation if true, disapear with a fadein */ public static void injectHeader(final WebView webView, boolean withAnimation) { if (webView != null) { MaterialViewPagerAnimator animator = MaterialViewPagerHelper.getAnimator(webView.getContext()); if (animator != null) { WebSettings webSettings = webView.getSettings(); webSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); // transparent background webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null); { // inject margin top final int marginTop = animator.getHeaderHeight() + 10; final String js = String.format("document.body.style.marginTop= \"%dpx\"", marginTop); webView.evaluateJavascript(js, null); } { final String js = "document.body.style.backround-color= white"; webView.evaluateJavascript(js, null); } if (withAnimation) webView.postDelayed( new Runnable() { @Override public void run() { webView.setVisibility(View.VISIBLE); ObjectAnimator.ofFloat(webView, "alpha", 0, 1).start(); } }, 400); } } }
@TargetApi(18) protected void init() { try { this.webView = new MyWebView(this.mContext); WebSettings localWebSettings = this.webView.getSettings(); if (localWebSettings != null) { localWebSettings.setJavaScriptEnabled(true); localWebSettings.setUserAgentString("Android-QingtingFM Mozilla/5.0 (Linux; U; Android 4.4.0; zh-cn; MB200 Build/GRJ22;) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1"); localWebSettings.setSupportZoom(true); localWebSettings.setRenderPriority(WebSettings.RenderPriority.HIGH); localWebSettings.setCacheMode(2); localWebSettings.setJavaScriptCanOpenWindowsAutomatically(true); } WebViewPlayer.getInstance().setbackPolicy(null); this.webView.addJavascriptInterface(WebViewPlayer.getInstance(), "QTJsPlayer"); this.webView.addJavascriptInterface(WebViewFunc.getInstance(), "QTJsReserve"); this.webView.setWebChromeClient(new WebChromeClient() { public boolean onJsAlert(WebView paramAnonymousWebView, String paramAnonymousString1, String paramAnonymousString2, JsResult paramAnonymousJsResult) { AlertDialog.Builder localBuilder = new AlertDialog.Builder(paramAnonymousWebView.getContext()); localBuilder.setTitle("蜻蜓提示").setMessage(paramAnonymousString2).setPositiveButton("确定", null); localBuilder.setCancelable(false); localBuilder.create().show(); paramAnonymousJsResult.confirm(); return true; } public void onProgressChanged(WebView paramAnonymousWebView, int paramAnonymousInt) { if (paramAnonymousInt > 70) GroupWebView.this.removeLoading(); } public void onReceivedTitle(WebView paramAnonymousWebView, String paramAnonymousString) { } }); this.webView.setHorizontalScrollBarEnabled(false); this.webView.setVerticalScrollBarEnabled(false); this.webView.setWebViewClient(this.webViewClient); this.webView.loadUrl(getUrl()); this.webView.setDownloadListener(new DownloadListener() { public void onDownloadStart(String paramAnonymousString1, String paramAnonymousString2, String paramAnonymousString3, String paramAnonymousString4, long paramAnonymousLong) { Intent localIntent = new Intent("android.intent.action.VIEW", Uri.parse(paramAnonymousString1)); GroupWebView.this.getContext().startActivity(localIntent); } }); WebViewPlayer.getInstance().setWebview(this.webView); WebViewFunc.getInstance().setWebview(this.webView); LinearLayout.LayoutParams localLayoutParams = new LinearLayout.LayoutParams(-1, -1); addView(this.webView, localLayoutParams); return; } catch (Exception localException) { } }
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); linearLayout = (LinearLayout) findViewById(R.id.linearLayout); linearLayout.setBackgroundColor(Color.BLACK); // Navbar setup navbar = (LinearLayout) findViewById(R.id.navbar); goButton = (Button) findViewById(R.id.go_button); urlField = (EditText) findViewById(R.id.url); goButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { doNav(); } }); urlField.setOnEditorActionListener( new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { boolean handled = false; if (actionId == EditorInfo.IME_ACTION_GO) { doNav(); handled = true; } return handled; } }); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); String lastUrl = settings.getString("lastUrl", ""); urlField.setText(lastUrl); // Navbar animation settings AnimationListener slideListener = new AnimationListener() { @Override public void onAnimationEnd(Animation animation) { if (animation.equals(slideUp)) { navbar.setVisibility(View.GONE); } } @Override public void onAnimationRepeat(Animation animation) {} @Override public void onAnimationStart(Animation animation) { if (animation.equals(slideDown)) { navbar.setVisibility(View.VISIBLE); } } }; slideUp = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f); slideUp.setDuration(500); slideUp.setAnimationListener(slideListener); slideDown = new TranslateAnimation( Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f); slideDown.setDuration(500); slideDown.setAnimationListener(slideListener); // WebView setup webView = (WebView) findViewById(R.id.webView); WebSettings webSettings = webView.getSettings(); webView.setWebChromeClient(new WebChromeClient()); webView.setWebViewClient( new WebViewClient() { @Override public void onPageFinished(WebView view, String url) { Log.d("scale", view.getScale() + ""); Log.d("pageFinished", url); view.setInitialScale(70); if (url.equalsIgnoreCase(HOME_PAGE)) { // navbar.setVisibility(View.VISIBLE); navbar.startAnimation(slideDown); } } @Override public void onScaleChanged(WebView view, float oldScale, float newScale) { Log.d("scale changed", oldScale + " - " + newScale); if (newScale > 0.7) { Log.d("scale", "reset"); // view.setInitialScale(70); } } }); webView.setInitialScale(70); webView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE); webView.setVerticalScrollBarEnabled(false); webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); webView.setOverScrollMode(WebView.OVER_SCROLL_NEVER); webSettings.setBuiltInZoomControls(true); webSettings.setDisplayZoomControls(false); webSettings.setSupportZoom(true); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setPluginState(WebSettings.PluginState.ON); webSettings.setJavaScriptEnabled(true); webSettings.setDomStorageEnabled(true); webSettings.setDatabaseEnabled(true); webSettings.setDatabasePath( "/data/data/" + webView.getContext().getPackageName() + "/databases/"); webSettings.setSaveFormData(false); webSettings.setLightTouchEnabled(false); webSettings.setLayoutAlgorithm(LayoutAlgorithm.NORMAL); webSettings.setRenderPriority(RenderPriority.HIGH); webSettings.setUserAgentString( "Mozilla/5.0 (X11; Linux i686) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.77 Large Screen Safari/534.24 GoogleTV"); final Intent intent = getIntent(); if ((intent.getAction() == Intent.ACTION_VIEW) && (intent.getData() != null)) { final String url = intent.getDataString(); urlField.setText(url); webView.loadUrl(url); navbar.setVisibility(View.GONE); } else { webView.loadUrl(HOME_PAGE); } webView.requestFocus(); }