andriod之应用内置浏览器 webview
生活随笔
收集整理的這篇文章主要介紹了
andriod之应用内置浏览器 webview
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
參考:http://my.eoe.cn/694183/archive/10476.html
http://blog.csdn.net/it_ladeng/article/details/8136534
?
一.webView獲取html頁面中標簽的方法:
1.在WebViewClient里的onPageFinished回調方法中執行js代碼:
view.loadUrl("javascript:console.log('MAGIC'+document.getElementById('wxImg')['src']);");2.在WebChromeClient里的onConsoleMessage回調方法中截取LOG信息:
@Override public boolean onConsoleMessage(ConsoleMessage consoleMessage) {if (consoleMessage.message().startsWith("MAGIC")) {String msg = consoleMessage.message().substring(5); // strip off prefixcom.youku.util.Logger.d("dazhu_IntegratedWebView", "msg : " + msg);if (!TextUtils.isEmpty(msg)) {if (oritationChangeActivity != null)oritationChangeActivity.setReceiveImage(msg);}return true;}return super.onConsoleMessage(consoleMessage); }二.支持縮放,電腦網頁適配手機屏幕
// 支持縮放 wb.getSettings().setBuiltInZoomControls(true); wb.getSettings().setSupportZoom(true); // 電腦網頁最小化適配手機屏幕 wb.getSettings().setUseWideViewPort(true); wb.getSettings().setLoadWithOverviewMode(true);三.app與webview共用cookie.前提訪問網絡用的是HttpURLConnection。
private void initCookie() {android.webkit.CookieSyncManager.createInstance(this);// unrelated, just make sure cookies are generally allowedandroid.webkit.CookieManager.getInstance().setAcceptCookie(true);// magic starts hereWebkitCookieManagerProxy coreCookieManager = new WebkitCookieManagerProxy(null, java.net.CookiePolicy.ACCEPT_ALL);java.net.CookieHandler.setDefault(coreCookieManager);}?
public class WebkitCookieManagerProxy extends CookieManager {private android.webkit.CookieManager webkitCookieManager;public WebkitCookieManagerProxy(){this(null, null);}public WebkitCookieManagerProxy(CookieStore store, CookiePolicy cookiePolicy){super(null, cookiePolicy);this.webkitCookieManager = android.webkit.CookieManager.getInstance();}@Overridepublic void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException{// make sure our args are validif ((uri == null) || (responseHeaders == null)) return;// save our url onceString url = uri.toString();// go over the headersfor (String headerKey : responseHeaders.keySet()){// ignore headers which aren't cookie relatedif ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;// process each of the headersfor (String headerValue : responseHeaders.get(headerKey)){this.webkitCookieManager.setCookie(url, headerValue);}}}@Overridepublic Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException{// make sure our args are validif ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");// save our url onceString url = uri.toString();// prepare our responseMap<String, List<String>> res = new java.util.HashMap<String, List<String>>();// get the cookieString cookie = this.webkitCookieManager.getCookie(url);// return itif (cookie != null) res.put("Cookie", Arrays.asList(cookie));return res;}@Overridepublic CookieStore getCookieStore(){// we don't want anyone to work with this cookie store directlythrow new UnsupportedOperationException();} }?參考:http://stackoverflow.com/questions/18057624/two-way-sync-for-cookies-between-httpurlconnection-java-net-cookiemanager-and
轉載于:https://www.cnblogs.com/fanglove/p/3502091.html
總結
以上是生活随笔為你收集整理的andriod之应用内置浏览器 webview的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: window.onscroll
- 下一篇: 1 加到 100 的 时间复杂度 C#.