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

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

相关内容

热门资讯

透视能赢!wepoker辅助器... 透视能赢!wepoker辅助器最新版本更新内容,wepoker安装教程(详细辅助软件价格)1、完成w...
透视教程!aapoker怎么控... 透视教程!aapoker怎么控制牌(透视)aapoker怎么提高中牌率,扑克教程(有挂解说);1、点...
透视好友!wepoker私人局... 透视好友!wepoker私人局透视方法,wepokerplus外挂(详细购买模拟器)一、wepoke...
透视游戏!aapoker辅助器... 透视游戏!aapoker辅助器是真的吗(透视)aapoker破解侠是真的吗,可靠教程(有挂细节)在进...
透视美元局!wepoker正确... 透视美元局!wepoker正确养号方法,wepoker正确养号方法(详细可以挂底牌);1、下载好we...
透视攻略!aapoker辅助器... 透视攻略!aapoker辅助器是真的吗(透视)aapoker辅助器是真的吗,必胜教程(有挂细节)小薇...
透视私人局!wepoker怎么... 透视私人局!wepoker怎么挂飞机,wepoker怎么看牌型(详细辅助器安装包定制)1、完成wep...
透视教程!aapoker辅助工... 透视教程!aapoker辅助工具安全吗(透视)aapoker脚本怎么用,扑克教程(有挂攻略);1、玩...
透视美元局!wepoker怎么... 透视美元局!wepoker怎么拿到好牌,we poker辅助器v3.3(详细私人辅助器);1)wep...
透视脚本!aapoker透视方... 透视脚本!aapoker透视方法(透视)aapoker脚本怎么用,第三方教程(有挂黑科技);1、点击...