0
私の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);
}
ファイルを印刷するための私の現在のスニペットがある。しかし、印刷が完了したときに私が実行したいです方法は、印刷ジョブが完了したときに発生するイベントをキャッチする必要がありますので、誰かがそれを行う方法を説明することができますまたは任意のコードスニペットが大変に感謝されます。
印刷ジョブが完了するのを待つのではなく、 'print()'呼び出しが戻ったときにユーザーがナビゲートできるようにすることをお勧めします。プリンタポリシー(デバイス上およびプリンタ上)およびステータス(たとえば、紙づまり)に基づいて、印刷ジョブはかなりの間完了しないことがあります。 – CommonsWare