Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android PrintManager get callback

I am using the PrintManager to print a webpage, how can my activity get a callback once printing was successfull? I am using this code:

    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

    // Create a print job with name and adapter instance
    String jobName = getString(R.string.app_name) + " Document";
    PrintJob printJob = printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());
    // Save the job object for later status checking
    //mPrintJobs.add(printJob);
like image 769
Alexander Fuchs Avatar asked Jun 09 '15 20:06

Alexander Fuchs


1 Answers

Create a wrapper around the PrintDocumentAdapter received from the webView. Then you can put a hook in the onFinish().

public class PrintDocumentAdapterWrapper extends PrintDocumentAdapter{

     private final PrintDocumentAdapter delegate;
     public PrintDocumentAdapterWrapper(PrintDocumentAdapter adapter){
         super();
         this.delegate = adapter;
     }

     public void onFinish(){
          delegate.onFinish();
          //insert hook here
     }

     //override all other methods with a trivial implementation calling to the delegate
}

And here is how to use it in your code :

// Get a PrintManager instance
PrintManager printManager = (PrintManager)    this.getSystemService(Context.PRINT_SERVICE);

// Get a print adapter instance and wrap it in my own adapter
PrintDocumentAdapter printAdapter = new PrintDocumentAdapterWrapper(webView.createPrintDocumentAdapter());

// Create a print job with name and adapter instance
String jobName = getString(R.string.app_name) + " Document";
PrintJob printJob = printManager.print(jobName, printAdapter,
        new PrintAttributes.Builder().build());
// Save the job object for later status checking
//mPrintJobs.add(printJob);
like image 142
ben75 Avatar answered Oct 07 '22 01:10

ben75