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,并在页面加载完成后创建打印任务。

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

相关内容

热门资讯

我来分享!htx矩阵wepok... 1、我来分享!htx矩阵wepoker辅助,epoker透视底牌,攻略教程(有挂透视);详细教程。2...
分辨真假!德普软件,wepok... 自定义wepoker私人局透视教程系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用...
必备教程!wpk俱乐部辅助器,... 必备教程!wpk俱乐部辅助器,wepoker透视脚本下载,透明教程(有挂方法);玩家必备必赢加哟《1...
程序员教你!wepoker插件... 程序员教你!wepoker插件功能辅助器,pokerrrr2辅助,存在挂教程(有挂辅助);小薇(透视...
推荐十款!hhpoker有没有... 推荐十款!hhpoker有没有外挂,佛手在线大菠萝技巧,新版2025教程(有挂教程),支持语音通讯、...
盘点几款!epoker免费透视... 1、盘点几款!epoker免费透视脚本,德普之星辅助器,透视教程(有挂神器)2、进入游戏-大厅左侧-...
一分钟教会你!hhpoker有... 1、一分钟教会你!hhpoker有辅助吗,wepoker脚本下载,曝光教程(有挂透明);详细教程。2...
推荐攻略!wejoker辅助软... 1、推荐攻略!wejoker辅助软件价格,wepoker底牌透视脚本,大神讲解(有挂方法)(UU p...
重磅来袭!德州真人透视脚本,p... 1、重磅来袭!德州真人透视脚本,pokerworld修改器,技巧教程(有挂透视);详细教程。2、德州...
科技分享!HH平台挂,aapo... 科技分享!HH平台挂,aapoker安装包怎么使用,黑科技教程(有挂辅助);原来确实真的有挂(需添加...