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 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); } }
@Override public View onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fun_everyday, container, false); menu = (ImageButton) view.findViewById(R.id.btn_header_left); menu.setOnClickListener(this); TextView headText = (TextView) view.findViewById(R.id.title_header_mid); headText.setText(R.string.text_menu_story); String url = "http://www.molegegu.com/im/webview.htm"; mWebView = (WebView) view.findViewById(R.id.sub_xeb_webview); mWebView.loadUrl(url); mWebView.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); mProgressBar = (ProgressBar) view.findViewById(R.id.loadProgress); mWebView = (WebView) view.findViewById(R.id.sub_xeb_webview); mWebView.loadUrl(url); mWebView.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); // set the webview WebSettings settings = mWebView.getSettings(); settings.setSupportZoom(true); settings.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); settings.setDefaultTextEncodingName("GBK"); settings.setTextSize(TextSize.NORMAL); settings.setJavaScriptEnabled(true); settings.setUseWideViewPort(true); mWebView.setInitialScale(80); mWebView.setWebChromeClient( new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress > 0 && newProgress < 100) { mProgressBar.setVisibility(View.VISIBLE); mProgressBar.setProgress(newProgress); } else if (newProgress == 100) { mProgressBar.setVisibility(View.GONE); } } }); mWebView.requestFocusFromTouch(); return view; }
@SuppressLint("SetJavaScriptEnabled") private void initWebView() { WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName("utf-8"); mWebView.setWebViewClient( new WebViewClient() { public void onReceivedError( WebView view, int errorCode, String description, String failingUrl) { AppLog.e(T.EDITOR, description); } }); mWebView.setWebChromeClient( new WebChromeClient() { public boolean onConsoleMessage(ConsoleMessage cm) { AppLog.e( T.EDITOR, cm.message() + " -- From line " + cm.lineNumber() + " of " + cm.sourceId()); return true; } @Override public boolean onJsAlert(WebView view, String url, String message, JsResult result) { AppLog.e(T.EDITOR, message); return true; } @Override public void onConsoleMessage(String message, int lineNumber, String sourceId) { AppLog.e(T.EDITOR, message + " -- from line " + lineNumber + " of " + sourceId); } }); String htmlEditor = getHtmlEditor(); mWebView.loadDataWithBaseURL("file:///android_asset/", htmlEditor, "text/html", "utf-8", ""); }
@Override public void initComponents() { wvUrl = (WebView) innerView.findViewById(R.id.wvUrl); WebSettings ws = wvUrl.getSettings(); ws.setAllowFileAccess(true); ws.setAppCacheEnabled(true); ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); ws.setBuiltInZoomControls(false); ws.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); ws.setDisplayZoomControls(false); ws.setJavaScriptEnabled(true); ws.setSupportZoom(false); ws.setDefaultTextEncodingName("GBK"); wvUrl.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(final WebView view, final String url) { view.post( new Runnable() { @Override public void run() { view.loadUrl(url); } }); return false; } }); wvUrl.setWebChromeClient(new WebChromeClient()); }
/** * @Title: initViews @Description: TODO(这里用一句话描述这个方法的作用) * * @param 设定文件 * @return void 返回类型 * @throws */ private void initViews() { mWebView = (CustomWebView) rootView.findViewById(R.id.webview); mWebView.setScrollBarStyle(0); WebSettings settings = mWebView.getSettings(); settings.setDefaultTextEncodingName("utf-8"); // 避免中文乱码 settings.setNeedInitialFocus(false); settings.setSupportZoom(true); settings.setLoadWithOverviewMode(true); // 适应屏幕 settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN); settings.setLoadsImagesAutomatically(true); // 自动加载图片 settings.setCacheMode(WebSettings.LOAD_DEFAULT | WebSettings.LOAD_CACHE_ELSE_NETWORK); // 滚动事件监听 mWebView.setOnCustomScroolChangeListener( new ScrollInterface() { @Override public void onScrollChanged(int l, int t, int oldl, int oldt) { // TODO Auto-generated method stub if (t == 0) { // 已经处于顶端 mCanScrollUp = true; } else { mCanScrollUp = false; } } }); }
@SuppressLint("SetJavaScriptEnabled") @Override protected void onCreate(final Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_reader); final int apiVersion = android.os.Build.VERSION.SDK_INT; if (apiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { getActionBar().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); getActionBar().setStackedBackgroundDrawable(new ColorDrawable(Color.WHITE)); } syndicationName = getIntent().getStringExtra("syndicationName"); if (TextUtils.isEmpty(syndicationName)) { getActionBar().setTitle(Html.fromHtml("<b><u>" + getTitle().toString() + "</u><b>")); } else { getActionBar().setTitle(Html.fromHtml("<b><u>" + syndicationName + "</u><b>")); } publicationTitle = getIntent().getStringExtra("title"); if (!TextUtils.isEmpty(publicationTitle)) { final TextView tv = (TextView) findViewById(R.id.reader_title); tv.setTypeface(TypeFaceSingleton.getInstance(this).getUserTypeFace(), Typeface.BOLD); tv.setText(publicationTitle); getWindow().setBackgroundDrawable(new ColorDrawable(Color.WHITE)); } getActionBar().setBackgroundDrawable(new ColorDrawable(0xeeeeee)); getActionBar().setStackedBackgroundDrawable(new ColorDrawable(0xeeeeee)); final String text = getIntent().getStringExtra("read"); link = getIntent().getStringExtra("link"); isFavorite = getIntent().getBooleanExtra("isFavorite", false); publicationId = getIntent().getIntExtra("publicationId", -1); syndicationId = getIntent().getIntExtra("syndicationId", -1); final WebView webView = (WebView) findViewById(R.id.reader); final WebSettings settings = webView.getSettings(); settings.setAllowFileAccess(true); settings.setDefaultTextEncodingName("utf-8"); // For enable video webView.setWebChromeClient(new WebChromeClient()); settings.setJavaScriptEnabled(true); settings.setDefaultFontSize( TypeFaceSingleton.getInstance(getApplicationContext()).getUserFontSize()); webView.loadDataWithBaseURL(null, getHtmlData(text), "text/html", "utf-8", null); publicationRepository = new PublicationRepository(this); }
// Call to show the changelog dialog public void show() { // Get resources String _PackageName = fActivity.getPackageName(); Resources _Resource; try { _Resource = fActivity.getPackageManager().getResourcesForApplication(_PackageName); } catch (NameNotFoundException e) { e.printStackTrace(); return; } // Get dialog title int _resID = _Resource.getIdentifier(TITLE_CHANGELOG, "string", _PackageName); String _Title = _Resource.getString(_resID); _Title = _Title + " v" + GetAppVersion(); // Get Changelog xml resource id _resID = _Resource.getIdentifier(CHANGELOG_XML, "xml", _PackageName); // Create html change log String _HTML = GetHTMLChangelog(_resID, _Resource); // Get button strings String _Close = _Resource.getString(R.string.changelog_close); // Check for empty changelog if (_HTML.equals("") == true) { // Could not load change log, message user and exit void Toast.makeText(fActivity, "Could not load change log", Toast.LENGTH_SHORT).show(); return; } // Create webview and load html WebView _WebView = new WebView(fActivity); WebSettings settings = _WebView.getSettings(); settings.setDefaultTextEncodingName("utf-8"); _WebView.loadData(_HTML, "text/html; charset=utf-8", null); AlertDialog.Builder builder = new AlertDialog.Builder(fActivity) .setTitle(_Title) .setView(_WebView) .setPositiveButton( _Close, new Dialog.OnClickListener() { public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); } }); builder.create().show(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webView = (WebView) findViewById(R.id.web_view); Log.e("TAG", webView.getSettings().getDefaultTextEncodingName()); WebSettings settings = webView.getSettings(); settings.setJavaScriptEnabled(true); settings.setDefaultTextEncodingName("utf-8"); settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); // webView.loadData(Url.url, "text/html;charset=UTF-8", null); String str = "<head><link rel=\"stylesheet\" href=\"wcss.css\" type=\"text/css\" /></head>"; webView.loadDataWithBaseURL("", Url.url, "text/html;charset=UTF-8", "", ""); }
public BockoView(Context context, AttributeSet attrs) { super(context, attrs); mWebView = new WebView(context); mWebView.setWillNotDraw(true); final WebSettings webSettings = mWebView.getSettings(); webSettings.setAppCacheEnabled(false); webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE); webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName("utf-8"); mWebView.loadUrl(getUrl()); mWebView.addJavascriptInterface(new JSObject(), "android"); SurfaceHolder holder = getHolder(); holder.addCallback(this); }
@SuppressLint("JavascriptInterface") public void showWebView() { // webView与js交互代码 try { mWebView = new WebView(this); setContentView(mWebView); mWebView.requestFocus(); mWebView.setWebChromeClient( new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { JSAndroidActivity.this.setTitle("Loading..."); JSAndroidActivity.this.setProgress(progress); if (progress >= 80) { JSAndroidActivity.this.setTitle("JsAndroid Test"); } } }); mWebView.setOnKeyListener( (v, keyCode, event) -> { if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) { mWebView.goBack(); return true; } return false; }); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName("utf-8"); mWebView.addJavascriptInterface(this, "android"); mWebView.loadUrl("file:///android_asset/index.html"); } catch (Exception e) { e.printStackTrace(); } }
@SuppressLint({"JavascriptInterface", "SetJavaScriptEnabled"}) private void initWebView() { mWebContent = (WebView) findViewById(R.id.news_detail_content); customViewContainer = (FrameLayout) findViewById(R.id.customViewContainer); mWebViewClient = new CustomWebViewClient(); mWebContent.setWebViewClient(mWebViewClient); mWebChromeClient = new MyWebChromeClient(); mWebContent.addJavascriptInterface(new JavascriptCallBack(), "Android"); mWebContent.setWebChromeClient(mWebChromeClient); WebSettings settings = mWebContent.getSettings(); settings.setJavaScriptEnabled(true); settings.setDomStorageEnabled(true); settings.setAppCachePath(AppUtils.getWebCachePath(this)); settings.setAllowFileAccess(true); settings.setAppCacheEnabled(true); settings.setCacheMode(WebSettings.LOAD_DEFAULT); settings.setDefaultTextEncodingName("UTF-8"); settings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS); settings.setCacheMode(WebSettings.LOAD_DEFAULT); mWebContent.loadUrl(url); }
@AfterViews void afterViews() { Log.d(TAG, "state: " + state.toString()); commonActivityTrait.initActivity(preferences); urlText.setSelectAllOnFocus(true); urlText.setOnEditorActionListener(this); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); webSettings.setDefaultTextEncodingName(ENCODING); webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); webView.addJavascriptInterface( new BrowserActivityJavaScriptInterface(this), BrowserActivityJavaScriptInterface.NAME); webView.setWebViewClient(new BrowserWebViewClient(this)); webView.setActionModeListener(this); translatePanelFragment.setListener(this); Intent intent = getIntent(); if (intent != null) { String action = intent.getAction(); if (action != null && action.equals(Intent.ACTION_VIEW)) { Uri url = intent.getData(); if (url != null) { loadUrl(url.toString()); return; } } } if (extraUrlOrKeywords.isEmpty()) { loadUrl(state.getLastUrl()); } else { loadUrl(extraUrlOrKeywords); } }
@Override protected void onCreate(Bundle savedInstanceState) { this.requestWindowFeature(Window.FEATURE_NO_TITLE); // 去掉标题栏 super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); WebView webview = (WebView) findViewById(R.id.webView_Main); webview.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); WebSettings setting = webview.getSettings(); setting.setJavaScriptEnabled(true); // 支持js setting.setDefaultTextEncodingName("GBK"); // 设置字符编码 webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY); // 滚动条风格,为0指滚动条不占用空间,直接覆盖在网页上 webview.loadUrl("http://42.200.39.108/"); }
@Override protected void onCreate(Bundle savedInstance) { super.onCreate(savedInstance); setContentView(R.layout.webview_activity); // adjust actionbar setDisplayHomeAsUpEnabled(true); mWebView = (WebView) findViewById(R.id.webViewContent); // enable javascript mWebView.getSettings().setJavaScriptEnabled(true); if (getIntent() == null) return; try { if ("android.resource".equals(getIntent().getData().getScheme())) { int rawId = Integer.parseInt( getIntent() .getData() .getPathSegments() .get(getIntent().getData().getPathSegments().size() - 1)); WebSettings settings = mWebView.getSettings(); settings.setDefaultTextEncodingName("utf-8"); mWebView.loadData( RawFileUtils.getRawAsString(getApplicationContext(), rawId), "text/html; charset=utf-8", null); } else { mWebView.loadUrl(getIntent().getData().toString()); } } catch (Exception e) { Log.e(LOGCAT, e.getMessage()); } }
@Override protected void layoutInit(LayoutInflater inflater, Bundle savedInstanceSate) { super.layoutInit(inflater, savedInstanceSate); BaseActivity activity = (BaseActivity) getActivity(); activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true); activity.getSupportActionBar().setTitle(R.string.title_login); setHasOptionsMenu(true); progressBar.setIndeterminate(true); editAccount = textinputAccount.getEditText(); editAccount.addTextChangedListener(new MTextWatcher(textinputAccount)); editPassword = textinputPassword.getEditText(); editPassword.addTextChangedListener(new MTextWatcher(textinputPassword)); if (getArguments() != null) { editAccount.setText(getArguments().getString("account")); editPassword.setText(getArguments().getString("password")); } WebSettings setting = webView.getSettings(); setting.setJavaScriptEnabled(true); setting.setDomStorageEnabled(true); setting.setAppCacheEnabled(true); setting.setDefaultTextEncodingName("utf-8"); webView.addJavascriptInterface(new LoginJavaScriptInterface(), "loginjs"); webView.setWebViewClient( new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, final String url) { if (url.startsWith(SettingUtility.getStringSetting("callback_url"))) webView.loadUrl("javascript:getAccount()"); view.loadUrl(url); Logger.d(TAG, "load url = " + url); // 是否授权成功 if (accountTask == null && url != null && url.startsWith(SettingUtility.getStringSetting("callback_url"))) { Params params = ParamsUtil.deCodeUrl(url); String code = params.getParameter("code"); Logger.d(TAG, "授权成功, code = " + code); if (getActivity() == null) return true; accountTask = new AccountTask(); accountTask.execute(code); } return true; } }); webView.setWebChromeClient( new WebChromeClient() { @Override public void onProgressChanged(WebView view, int newProgress) { if (newProgress < 100) { progressBar.setVisibility(View.VISIBLE); } else if (newProgress == 100) { progressBar.setVisibility(View.GONE); } if (newProgress < 100) { } else if (newProgress == 100) { Logger.d(TAG, String.format("progress = 100 , url = %s", view.getUrl())); if (!accountFilled && !TextUtils.isEmpty(view.getUrl()) && view.getUrl().equalsIgnoreCase("about:blank")) { webView.loadUrl("javascript:fillAccount()"); accountFilled = true; } // 是否授权成功 if (accountTask == null && webView.getUrl() != null && webView.getUrl().startsWith(SettingUtility.getStringSetting("callback_url"))) { Params params = ParamsUtil.deCodeUrl(webView.getUrl()); String code = params.getParameter("code"); Logger.d(TAG, "授权成功, code = " + code); if (getActivity() == null) return; accountTask = new AccountTask(); accountTask.execute(code); } } if (webView.getUrl() != null && webView.getUrl().startsWith(SettingUtility.getStringSetting("callback_url"))) { webView.setVisibility(View.GONE); } super.onProgressChanged(view, newProgress); } }); if (!TextUtils.isEmpty(editAccount.getText().toString()) && !TextUtils.isEmpty(editPassword.getText().toString())) { new Handler() .postDelayed( new Runnable() { @Override public void run() { doLogin(); } }, 500); } }