gpt4 book ai didi

java - 如何判断打印作业是否完成?

转载 作者:行者123 更新时间:2023-12-02 12:32:31 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,我正在尝试打印 PDF 文档,一切正常,这是我当前用于打印文件的代码段

private void printFile(){
PrintManager printManager = (PrintManager) this.getSystemService(getApplicationContext().PRINT_SERVICE);
PrintDocumentAdapter pda = new PrintDocumentAdapter(){

@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback){
OutputStream output = null;

try {
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = _responseBody;
output.write(buf);
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});

} catch (FileNotFoundException ee){
//Catch exception
} catch (Exception e) {
//Catch exception
} finally {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

@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("Name of file").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}

@Override
public void onFinish(){
System.out.println("PRINT IS FINISHED");
}
};



PrintAttributes printAttrs = new PrintAttributes.Builder().
setMediaSize(PrintAttributes.MediaSize.ISO_A6).
setMinMargins(PrintAttributes.Margins.NO_MARGINS).
build();

PrintJob printJob = printManager.print(labelID, pda, printAttrs);

}

但是,当打印完成后,我想运行一个方法来返回到应用程序的先前 View ,因此我需要捕获打印作业完成时触发的事件,有人可以解释一下如何执行此操作吗?任何代码 fragment 将不胜感激。

最佳答案

我同意 CommonsWare 的评论,打印作业可能需要很长时间,并且在某些情况下可能会无限期地处于“阻止”状态。根据记录,onFinish()回调应该在正常打印成功的情况下工作。

关于java - 如何判断打印作业是否完成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47473496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com