PrintServices are plug-in components that know how to talk to printers via some standard protocols. These services serve as a bridge between the system and the printers. Hence, the printer and print protocol specific implementation is factored out of the system and can be independently developed and updated.
In other word, PrintService is a service that performs the detection of the printer, the processing of the print job. Addition to the role, such as a driver for the network printer, it can also be implemented as a service to be output to any location as a PDF file.
Add and configure PrintService
When you install an application that contains the implementation of PrintService, service will be added to the Printing screen of Settings. The user can launch the settings screen you can switch the On / Off of PrintService each, each PrintService provides.
Execution flow of PrintService
PrintService works with PrintManager managed by the system.
- PrintManager to display the Print dialog
- (Call of onCreatePrinterDiscoverySession method) that is required to start the session detection of printer from the system
- Return an instance of the class PrinterDiscoverySession
- Add a printer, delete, or update in PrinterDiscoverySession class
- User selects a printer and settings, then printing starts
- PrintJob is passed to the method onPrintJobQueued
- Printing process based on the information of PrintJob
PrintService is an abstract class that inherits from the Service. There are three abstract methods.
When the system starts the detection of printer, onCreatePrinterDiscoverySession method of PrintService is called. There is a need to return an instance of PrinterDiscoverySession is PrintService side at this time. During the detection of the printer, PrinterDiscoverySession is a class that encapsulates the interaction between the system PrintService. Each callback method of PrinterDiscoverySession class will be called, and printer can be added, updated, and deleted.
|void onStartPrinterDiscovery (List ||It starts the detection of the printer. Printer information is already discovered. It adds and updates printers in addPrinters method. It deletes printers in removePrinters method.|
|void onStopPrinterDiscovery ()||Callback notifying you that you should stop printer discovery.|
|void onStartPrinterStateTracking (PrinterId printerId)||A callback that asks the user to start tracking of the status of the printer. It is called when the printer is selected in the Print dialog. You must notify the system as soon as possible when the state of the printer has changed|
|void onStopPrinterStateTracking (PrinterId printerId)||A callback to notify that the tracking of the status of the printer is finished.|
|void onValidatePrinters (List ||A callback to seek validation list of PrinterId passed as if it were a valid. There is a need to update the printer information in addPrinters method if it is valid|
|void onDestroy ()||It will be called when this session is determined to be unnecessary from the system|
In the following section, an implementation example of PrinterDiscoverySession is showed with DropboxPrintService. It does not do anything except that you are adding a printer in onStartPrinterDiscovery method. PrinterInfo.Builder is created for printer information. You can specify printable paper size, color settings, such as resolution. if you pass a list of PrinterInfo in addPrinters method, printer information is displayed in the print dialog box. Create a PrinterInfo in onStartPrinterDiscovery within the method, it calls addPrinters method, but the detection is performed printer to start the thread normally, it will be able to add the printer information using the Handler, etc. .
onPrintJobQueued (PrintJob paramPrintJob)
When a printer is detected, the printing starts, PrintJob is passed to onPrintJobQueued method. Other printer ID, and FileDescriptor color information and print size, to a PDF file is included in the PrintJob. This is done using the FileDescriptor is taken out of print data. PrintJob will have a status start, finished, failed, and canceled. Should be set in accordance with the state of printing. The following is an example to save and retrieve the PDF from PrintJob. You have done all the work on onPrintJobQueued method, but will be using the AsyncTask, etc. usually. However, operation of PrintJob must be done on the UI thread. FileDescriptor of PDF data is similar. If you want to send to the network on the PDF data, you will need to use the AsyncTask on which the file was created, etc. time.
PrintJob so are queued automatically PrintService, you do not need to be the handling of the queue on your own again.PrintJob active can be retrieved in getActivePrintJobs method.
onRequestCancelPrintJob (PrintJob paramPrintJob)
onRequestCancelPrintJob method is called when the user cancels the print job. PrintJob are passed, you need to stop the process corresponding. You can also check in isCancelled method of PrintJob class
In many cases, you will need to PrintService settings to connect to the printer over a network. Alternatively, you can configure the metadata when you define a PrintService in AndroidManifest.xml.
You can specify the metadata of AndroidManifest.xml the XML file that you create and define Activity of the setting screen.
- Source is here → DropboxPrintService
- It does not include an API key towards the source, be sure to get the API key in Dropbox If you want to use it to build from source.
- apk here → DropboxPrintService.Apk
Log in to the Dropbox
When you have made all of your installed the DropboxPrintService, first log in to the Dropbox. Select the DropboxPrintService in Printing screen of Settings, turn ON the DropboxPrintService open the detail screen. Please log in to Dropbox so open the setting screen from the bottom menu.
PDF is uploaded to Dropbox