Android默认打印弹窗无法检测到通过我的应用程序连接配对的打印机,但其他应用程序却可以。
创始人
2024-10-09 08:33:07
0

要解决Android默认打印弹窗无法检测到通过您的应用程序连接配对的打印机,但其他应用程序可以的问题,您可以使用PrintManager类来实现自定义打印功能。以下是一个示例代码,可以帮助您解决这个问题:

import android.content.Context;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintJob;
import android.print.PrintManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class Printer {

    public static void print(Context context, String url) {
        WebView webView = new WebView(context);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                // 页面加载完成后开始打印
                createWebPrintJob(context, view);
            }
        });
        webView.loadUrl(url);
    }

    private static void createWebPrintJob(Context context, WebView webView) {
        // 获取PrintManager实例
        PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);
        // 设置打印任务名称
        String jobName = context.getString(R.string.app_name) + " Print Job";
        // 创建打印任务
        PrintJob printJob = printManager.print(jobName, new PrintDocumentAdapter() {
            @Override
            public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
                // 完成布局
                if (cancellationSignal.isCanceled()) {
                    callback.onLayoutCancelled();
                    return;
                }
                PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("print_output.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
                callback.onLayoutFinished(pdi, true);
            }

            @Override
            public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback) {
                // 写入打印数据
                if (cancellationSignal.isCanceled()) {
                    callback.onWriteCancelled();
                    return;
                }
                callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
            }
        }, null);
    }
}

要使用此示例代码,您需要在AndroidManifest.xml文件中添加以下权限:













使用Printer.print(context,url)方法来触发打印操作。该方法将加载指定URL的WebView,并在页面加载完成后创建打印任务。

请确保您已经正确配对了打印机,并且打印机与您的设备处于连接状态。

相关内容

热门资讯

透视曝光!hhpoker透视方... 透视曝光!hhpoker透视方法,hhpoker有没有作弊辅助-本来真的有辅助神器(哔哩哔哩)1、金...
透视详情!wpk是真的还是假的... 透视详情!wpk是真的还是假的,wpk真的有透视嘛-本来存在有辅助工具(哔哩哔哩)1、实时透视辅助更...
透视揭幕!购买的wpk辅助在哪... 透视揭幕!购买的wpk辅助在哪里下载,wpk软件是真的吗-真是一直总是有辅助插件(哔哩哔哩)是不是有...
透视分享!wepoker有没有... 透视分享!wepoker有没有挂,wepoker数据分析-一贯是真的有辅助脚本(哔哩哔哩)1、免费脚...
透视了解!wepoker私人局... 透视了解!wepoker私人局有透视吗,WePoKer辅助器-确实一直都是有辅助教程(哔哩哔哩)1、...
透视辅助!wpk真的有透视嘛,... 透视辅助!wpk真的有透视嘛,wpk真吗-本来是真的有辅助app(哔哩哔哩)1、透视辅助!wpk真的...
透视详情!wepoker私人局... 透视详情!wepoker私人局辅助器怎么用,wepokerh5破解-确实一直都是有辅助工具(哔哩哔哩...
透视详情!智星德州插件2024... 透视详情!智星德州插件2024最新版,pokemmo脚本-一直真的有辅助脚本(哔哩哔哩)1、上手简单...
透视分享!hhpkoer辅助挂... 透视分享!hhpkoer辅助挂是真的吗,hhpoker德州牛仔视频-果然有辅助神器(哔哩哔哩)1、完...
透视教你!德州局怎么透视,模拟... 透视教你!德州局怎么透视,模拟器打开hhpoker-切实一直总是有辅助技巧(哔哩哔哩)1、点击下载安...