zoukankan      html  css  js  c++  java
  • 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 prefix
           com.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 allowed
            android.webkit.CookieManager.getInstance().setAcceptCookie(true);
    
            // magic starts here
            WebkitCookieManagerProxy 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();
        }
    
        @Override
        public void put(URI uri, Map<String, List<String>> responseHeaders) throws IOException
        {
            // make sure our args are valid
            if ((uri == null) || (responseHeaders == null)) return;
    
            // save our url once
            String url = uri.toString();
    
            // go over the headers
            for (String headerKey : responseHeaders.keySet())
            {
                // ignore headers which aren't cookie related
                if ((headerKey == null) || !(headerKey.equalsIgnoreCase("Set-Cookie2") || headerKey.equalsIgnoreCase("Set-Cookie"))) continue;
    
                // process each of the headers
                for (String headerValue : responseHeaders.get(headerKey))
                {
                    this.webkitCookieManager.setCookie(url, headerValue);
                }
            }
        }
    
        @Override
        public Map<String, List<String>> get(URI uri, Map<String, List<String>> requestHeaders) throws IOException
        {
            // make sure our args are valid
            if ((uri == null) || (requestHeaders == null)) throw new IllegalArgumentException("Argument is null");
    
            // save our url once
            String url = uri.toString();
    
            // prepare our response
            Map<String, List<String>> res = new java.util.HashMap<String, List<String>>();
    
            // get the cookie
            String cookie = this.webkitCookieManager.getCookie(url);
    
            // return it
            if (cookie != null) res.put("Cookie", Arrays.asList(cookie));
            return res;
        }
    
        @Override
        public CookieStore getCookieStore()
        {
            // we don't want anyone to work with this cookie store directly
            throw new UnsupportedOperationException();
        }
    }

     参考:http://stackoverflow.com/questions/18057624/two-way-sync-for-cookies-between-httpurlconnection-java-net-cookiemanager-and

  • 相关阅读:
    图论算法(三) 最短路SPFA算法
    图论算法(二)最短路算法:Floyd算法!
    图论算法(一)存图与STL第六弹——vector容器
    C++指针变量的基本写法
    杂记——深度优先搜索(dfs)与出题感想
    分治算法(二分查找)、STL函数库的应用第五弹——二分函数
    网站开发小技巧总结
    网站开发动态效果插件
    jquery获得ul下li的个数
    jquery的循环函数和点击事件绑定
  • 原文地址:https://www.cnblogs.com/fanglove/p/3502091.html
Copyright © 2011-2022 走看看