如何通过反射使用奇巧4.4打印API我打印的WebView? [英] How do I print a WebView using KitKat 4.4 print API via reflection?

查看:192
本文介绍了如何通过反射使用奇巧4.4打印API我打印的WebView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要支持打印的奇巧设备,但我的目标SDK是13(改变不是一个选项)。

I need to support printing on KitKat devices but my target SDK is 13 (changing is not an option).

我特别需要打印的WebView。

Specifically I need to print a webview.

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

This is the API for printing a webview: http://developer.android.com/training/printing/html-docs.html

推荐答案

这是一个旧的,但印刷是一种有用的所以这可能是不错的正常工作。 (无反射;))

It's an old one but printing is kind of usefull so this could be good to work correctly. (Without reflection ;))

一个更好的方式与设备的版本工作。不需要的try-catch,只需要在返回之前添加一些信息,或者你只是隐藏按钮/菜单/ ...根据相同的条件。

A better way to work with devices version. No try-catch needed, just need to add some messages before the return or you just hide the button/menu/... depending on the same condition.

@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());

    }

将打印作业将只用SDK为19及以上

The printJob will be execute only with SDK 19 and above

这篇关于如何通过反射使用奇巧4.4打印API我打印的WebView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆