GVKun编程网logo

android – 如何通过反射使用KitKat 4.4 print API打印WebView?

2

本篇文章给大家谈谈android–如何通过反射使用KitKat4.4printAPI打印WebView?,同时本文还将给你拓展Android4.4KitKat里应用可调用的WebView已经是基于Ch

本篇文章给大家谈谈android – 如何通过反射使用KitKat 4.4 print API打印WebView?,同时本文还将给你拓展Android 4.4 KitKat 里应用可调用的 WebView 已经是基于 Chromium 的了且也会自动升级、Android KitKat中的新WebView问题、android listview的item中有webview,但是点击webview会没有反应?、Android webview 使用Cookie 免登录等相关知识,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

android – 如何通过反射使用KitKat 4.4 print API打印WebView?

android – 如何通过反射使用KitKat 4.4 print API打印WebView?

我需要支持在KitKat设备上打印,但我的目标SDK是13(更改不是一个选项).

具体来说,我需要打印一个webview.

这是用于打印webview的API:
http://developer.android.com/training/printing/html-docs.html

解决方法:

这是一个旧的,但打印是有用的,所以这可能是正常工作的好. (没有反思;))

使用设备版本的更好方法.不需要try-catch,只需要在返回之前添加一些消息,或者只是隐藏按钮/菜单/ …取决于相同的条件.

@H_301_17@@TargetApi(Build.VERSION_CODES.KITKAT)
    private void createWebPrintJob(WebView webView) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) 
            return;

        // Get a PrintManager instance
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

        // Get a print adapter instance
        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        // Create a print job with name and adapter instance
        String jobName = getString(R.string.app_name) + " Document";
        printManager.print(jobName, printAdapter,
                new PrintAttributes.Builder().build());

    }

printJob将仅使用SDK 19及更高版本执行

Android 4.4 KitKat 里应用可调用的 WebView 已经是基于 Chromium 的了且也会自动升级

Android 4.4 KitKat 里应用可调用的 WebView 已经是基于 Chromium 的了且也会自动升级

Android 4.4 KitKat还有一个对应用开发者来说很欣慰的变化,就是应用里嵌入WebView浏览器的核心已经默认就是基于Chromium的了,而且Google 今天还确认未来会跟Chrome一样,对WebView里的这个浏览器也进行自动升级更新。Google承认这么做带来了极大的工程和逻辑上的挑战,目前 还有一些问题没有解决,但正在着手解决自动升级的问题。

这样开发者的嵌入网页的应用就可以随时获得最新的Web API和HTML 5功能,且效率也会越来越高。

Via Chrome Story/谷奥

Android KitKat中的新WebView问题

Android KitKat中的新WebView问题

我的应用程序使用WebView显示长文本

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    WebView mWebView = new WebView(getApplicationContext());

    setContentView(mWebView);

    String s = getHtml(); //get a long html from a file

    webView.loadDataWithBaseURL("file:///android_asset/", s, "text/html", "UTF-8", null);
}

该应用程序适用于旧版Android,
但是当在KitKat上运行时,在创建或销毁WebView时,此错误会出现在logcat中:

libGLESv2(21582): hwui Protection: wrong calling from app context F:ES3-glDeleteShader

我读过“在Android 4.4中迁移到WebView”,但我无法解决问题.

我该如何解决?

解决方法:

更改

WebView mWebView = new WebView(getApplicationContext());

WebView mWebView = new WebView(this);

android listview的item中有webview,但是点击webview会没有反应?

android listview的item中有webview,但是点击webview会没有反应?

listview的item中嵌套有webview,给listview设置了onclickItem的点击事件,但是当点到webview的位置时没有响应,其他位置却可以,应该怎么设置才能在点到webview的位置时也有响应?

Android webview 使用Cookie 免登录

Android webview 使用Cookie 免登录

这两天有个项目需求要用cookie 缓存 免登录,
在网上找了好多相关教程,
没有一个完整可以测试的。

经历了重重磨难终于... 
突破了... 突破了... 突破了... 


下面为实例代码
package com.example.lin.webbrower;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.http.SslError;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.webkit.CookieManager;
import android.webkit.SslErrorHandler;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static android.webkit.CookieSyncManager.createInstance;
import static android.webkit.CookieSyncManager.getInstance;

/**
 * Created by LIN on 2018/8/1.
 * web浏览器
 */

public class WebBrowerActivity extends AppCompatActivity {

    WebView webView;
    private String url;
    private CookieManager mCookieManager;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webbrower);
        setTitle("加载中...");

        webView = (WebView) findViewById(R.id.web_view);
        //获取sharedPreferences对象
        sharedPreferences = getSharedPreferences("myshare", Context.MODE_PRIVATE);
        //获取editor对象
        //获取编辑器
        editor = sharedPreferences.edit();

        WebSettings settings = webView.getSettings();
        settings.setJavaScriptEnabled(true);//支持js
        //settings.setPluginState(true);//支持插件
        settings.setUseWideViewPort(false);//将图片调整到适合webview的大小
        settings.setSupportZoom(true);//支持缩放
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN); //支持内容重新布局
        settings.supportMultipleWindows();//多窗口
        settings.setAllowFileAccess(true);//设置可以访问文件
        settings.setNeedInitialFocus(true); //当webview调用requestFocus时为webview设置节点
        settings.setBuiltInZoomControls(true); //设置支持缩放
        settings.setJavaScriptCanOpenWindowsAutomatically(true); //支持通过JS打开新窗口
        settings.setLoadWithOverviewMode(true); // 缩放至屏幕的大小
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
        settings.setAppCacheEnabled(true);
        settings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
        settings.setLoadsImagesAutomatically(true);//支持自动加载图片

        String cookiea = sharedPreferences.getString(url, "");
        //测试可用

        String cookie = "这里为关键cookie串 用户名 权限 等信息; domain=通过抓包工具得到; expires=Sat, 03-Aug-2019 14:01:48 GMT; path=/";
        //同步cookie 到 url 中
        if (cookiea != null) {
            syncCookie(url, cookiea);
        }
        webView.loadUrl(url);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                //这里一定要是url不能是网页
                view.loadUrl(url);
                return true;
            }//证书的设置支持所有的证书

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
            }


            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                CookieManager cookieManager = CookieManager.getInstance();
                String cookie = cookieManager.getCookie(url);
                String lastUser = cookie.substring(cookie.indexOf("UsUser"), cookie.length());
                editor.putString(url, lastUser);
                editor.commit();
                String title = view.getTitle();
                if (!TextUtils.isEmpty(title)) {
                    setTitle(title);
                }
            }
        });
    }


    private void syncCookie(String url, String cookie) {
        try {
            createInstance(this);//创建一个cookie管理器
            CookieManager cookieManager = CookieManager.getInstance();
            cookieManager.setAcceptCookie(true);
            cookieManager.removeSessionCookie();// 移除以前的cookie
            cookieManager.removeAllCookie();
            StringBuilder sbCookie = new StringBuilder();//创建一个拼接cookie的容器,为什么这么拼接,大家查阅一下http头Cookie的结构
            sbCookie.append(cookie);
            //这里的两个key很重要,如果不知道是什么可以抓包得到
            sbCookie.append(String.format(";domain=%s", "."));
            sbCookie.append(String.format(";path=%s", "/"));
            String cookieValue = sbCookie.toString();
            cookieManager.setCookie(url, cookieValue);//为url设置cookie
            getInstance().sync();//同步cookie
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    //防止退出的时候抛Zoom异常
    @Override
    public void finish() {
        ViewGroup view = (ViewGroup) getWindow().getDecorView();
        view.removeAllViews();
        super.finish();
    }
}

 

 

 

今天的关于android – 如何通过反射使用KitKat 4.4 print API打印WebView?的分享已经结束,谢谢您的关注,如果想了解更多关于Android 4.4 KitKat 里应用可调用的 WebView 已经是基于 Chromium 的了且也会自动升级、Android KitKat中的新WebView问题、android listview的item中有webview,但是点击webview会没有反应?、Android webview 使用Cookie 免登录的相关知识,请在本站进行查询。

本文标签: