Android Question Print document finish

santiago

Member
Licensed User
Longtime User
I use this code to print a webview

code:
sub CreateWebPrintJob (wv As WebView)
    Dim jwv As JavaObject = wv
    Dim jprintmanager As JavaObject = jwv.RunMethodJO("getContext", Null).RunMethod("getSystemService", Array As Object("print"))
  
    Dim jPrintAdapter As JavaObject = jwv .RunMethod("createPrintDocumentAdapter", Null)
    Dim jobName As String = "Document"
    'jPrintManager.RunMethod("print", Array As Object(jobName, jPrintAdapter, Null))
    [B]jprintmanager.RunMethod("print", Array As Object(jobName, jPrintAdapter, CreatePrinterAttributes))[/B]
    
End Sub
It worked fine. It shows the print preview

But at least I need an event for job finsh to clear the view after the printer finish and maybe a job cancel too
And I am not able to find out how to add that event .I read the Android printer docs ,fount printjob but not able to add the event
Sorry for my English and thanks for your time and help
 

santiago

Member
Licensed User
Longtime User
Erel , thanks too much.
The simplest, the best. I used activity/resume as you told and working well.
Have a good day

On the other hand, is it not possible to add a JavaObject for job print to evaluate the result for jPrintManager or jPrintAdapter ? or a code to define the printJob like the CreatePrintAtributes function you showed to set paper size ?

Thanks in advance and thanks for your time
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
you can monitor the print job status (cancelled, finished, etc) and raise an event.
check the documentation for android.print.PrintJob class

 
Upvote 0

santiago

Member
Licensed User
Longtime User
you can monitor the print job status (cancelled, finished, etc) and raise an event.
check the documentation for android.print.PrintJob class

Thanks.
The problem its I found the anndroid printjob call but I am not able to implement it or deploy to access to job status. Do you can help with that issue? I use the code I show at the begining of this threat
Thanks in advance
 
Upvote 0

drgottjr

Expert
Licensed User
Longtime User
printmanager has an addPrintJobStateChangeListener() method. it signals when print job status changes. you can raise an event in b4a when such change occurs. the system identifies the job and the change (complete, cancelled, etc). i would use inline java:

B4X:
printManager.addPrintJobStateChangeListener(new PrintManager.PrintJobStateChangeListener() {
    @Override
    public void onPrintJobStateChanged(PrintJob printJob) {
        if (printJob.isCompleted()) {
            // raise your event
        } else if (printJob.isFailed()) {
            // raise event if desired
        } else if (printJob.isCancelled()) {
            // raise event if you want
        } else if (printJob.isBlocked()) {
            // raise event if interested
        }
    }
});

erel would use javaobject the way you currently do, but the trick is the listener. i've seen him use a polling timing loop to mimic one. if you look at his barcodescanner class for mlkit, you can see how he does it. in that case, he only has to deal with 1 result; in this case you have 4 possible states. i'm not sure how to handle that, so i opted for inline java. it shouldn't interfere with what you have done so far with javaobject. once you issue the print command, the printmanager knows about the job. you can add a listener to the printmanager after calling print and handle the event you raise.
 
Upvote 0
Top