How to determine if the print job completed?

Viewed 1650

In my Android app, I'm trying to print a PDF document, and everything is working fine, here is my current snippet for printing the file

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);

    }

But when the print is completed I want to run a method for going back to a previous view of the app, so I need to catch the event it fires when the print job is completed, can somebody explain how to do it or any code snippet will be greatly appreciated.

1 Answers
Related