Directory
* Package Android.printservice
* Public abstract class Printservice
* Public abstract class Printerdiscoverysession
* Public final class Printerinfo
* Public final class Printerid
* Public final class PrintJob
* Public final class Printjobinfo
* Public final class Printercapabilitiesinfo
* Public final class PrintDocument
* Public final class Printdocumentinfo
Original link: https://developer.android.com/reference/android/printservice/package-summary.html
Package
Android.printservice
The class that provides the print service implementation. The print service is a plug-in (plug-in components) that can communicate with some standard protocols and printers. These services are like a bridge between the system and the printer. As a result, the specific implementation of the printer and printing protocol is decoupled from the system and can be independently developed and updated.
A print service implementation should be based on the PrintService
class and implement its abstract method (abstract methods). In addition, the print service must manage PrintJob
(print task) classes by convention.
The system is responsible for starting and stopping a print service, depending on whether the printer managed by the Print Service has an active print job (active print jobs). The Print service also performs printer discovery operations in a timely manner to ensure a good user experience. During the printer discovery process, the interaction between the system and the Print service is encapsulated in an instance of the PrinterDiscoverySession
class, which is created by the Print service when required by the system.
Class (Classes) PrintDocument
From the point of view of a print service, this class represents a file to be printed (document).
Printerdiscoverysession
This class encapsulates the interaction of a print service and system during the printer discovery process.
PrintJob
For a print service, this class represents a print job.
Printservice
This basic class is used to implement printing services.
Original link: https://developer.android.com/reference/android/printservice/PrintService.html
Public abstract class
Printservice
Extends Service
java.lang.Object ? android.content.Context ? android.content.ContextWrapper ? android.app.Service ? android.printservice.PrintService
Category Overview Class overview
This is the underlying class for the print services implementation. A print service knows how to discover a printer and communicates with one or more printers through one or more protocols.
Printer Discovery Printer Discovery
A print service is responsible for discovering the printer, adding the discovered printer, removing the added printer, and updating the added printer. When the system needs the printer that your service manages, the system calls onCreatePrinterDiscoverySession()
, and in this function you must return an PrinterDiscoverySession
object instance (instance). The returned session (session) encapsulates the content of the system and your service interaction, including operations in the printer discovery phase. For more descriptions of this interaction, refer to the PrinterDiscoverySession
documentation.
For each print discovery session, all printers must be added because the system will not persist after the session. Therefore, each printer that the Print service detects should be added and added only once in a discovery session. Only printers that have been added can be removed (removed) or updated (updated). The removed printer can also be added again.
Printing work print jobs
When a new print job is assigned to the printer managed by the Print service, the print job is put into the queue (queued). That is, ready to be processed by the Print service. You will receive a onPrintJobQueued(PrintJob)
call. The print service may handle this print job immediately or put it in a schedule. All activities of the Print Service are printed in a list that can be obtained by invoking the list getActivePrintJobs()
. Active print jobs (active print jobs) are those that are in the queue or have started.
When a print service is processing a print job, the print service is responsible for setting the print job to the appropriate state. First, a print job is in the queue, that is, PrintJob.isQueued()
returning true, which means that the document to be printed is already scheduled (spooled) and the print service can handle it at any time. You can get the PrintJob.getDocument()
document to be printed by calling it, and the data can be PrintDocument.getData()
read. When the print service starts printing data, the print job status should be set to started (started) by calling start()
function settings. After setting, PrintJob.isStarted()
it should return true. After the work is completed successfully, the print job should be marked as completed (completed) by invoking the PrintJob.complete()
settings. After setting, PrintJob.isCompleted()
it should return true. If it fails, the print job should be marked as failed (failed) by invoking the PrintJob.fail( String)
settings. After setting, it PrintJob.isFailed()
should return true.
If a print job is queued (queued) or started (started), the print service receives a call when the user requests that it be canceled onRequestCancelPrintJob(PrintJob)
. This request in the service wants to do its best to cancel the job. If the job is successfully canceled, its status needs to PrintJob.cancel()
be flagged for cancellation. tag, you PrintJob.isCacnelled()
should return true.
Life cycle Lifecycle
The life cycle of a print service is managed only by the system and is based on the specified life cycle activities. In addition, starting or stopping a print service is triggered only by a specific user behavior, which is to enable (enabling) or disable (disabling) services in the device settings. After the system binds (binds) a print service, the system is called onConnected()
. This method can be overridden by the client (clients) to perform binding-related operations. And after the system is unbound (unbinds) a print service, the system is called onDisconnected()
. This method can be overridden by the client (clients) to perform cleanup operations related to unbinding (unbinding). None of your work should be done after the system is disconnected from your print service because the service will be killed at any time when it reclaims memory. When the print service manages a printer that has active print work, the system is not disconnected from it.
Statement Declaration
A print service, like any other service, needs to be declared in Androidmanifest.xml. But it must also deal with the action- android.printservice.PrintService
intent. Failure of this intent declaration causes the system to ignore the print service. In addition, a print service must request android.permission.BIND_PRINT_SERVICE
permissions to ensure that only the system can bind (BIND) it. Declaring this failure will cause the system to ignore the print service. Here is an example of a declaration:
<service android:name=".MyPrintService" android:permission="android.permission.BIND_PRINT_SERVICE"> <intent-filter> <action android:name="android.printservice.PrintService" /> </intent-filter> </service>
Configuring the Configuration
A print service can be configured via the Custom settings page (setting activity), which provides custom settings functionality. An activity that adds a printer can manually add printers, vendor names, and so on. The system is responsible for starting the setup and adding the printer's activities at the appropriate time.
A print service in the Declaration, to provide a mainfest meta-data
, this is the way to specify the above activities. A service's meta-data
label (tag) declaration is as follows:
<service android:name=". Myprintservice "android:permission=" Android.permission.BIND_PRINT_SERVICE "> <intent-filter> <action android:name="Android.printservice.PrintService" /> </intent-filter> <meta-data android:name= "android.printservice" android:resource=" @xml/printservice " /> </Service>
For more details on configuring your print service with Meta-data, you can refer to SERVICE_META_DATA
and <print-service>
.
tip: All callback Functions (callbacks) in this class are executed in the main thread of the program. You should also call (Invoke) the method in this class in the main thread of the program.
Original link: https://developer.android.com/reference/android/printservice/PrinterDiscoverySession.html
Public abstract class
Printerdiscoverysession
Extends Object
java.lang.Object ? android.printservice.PrinterDiscoverySession
Category Overview Class overview
This class encapsulates the interactive content of a print service and system in the printer look-up (printer discovery) process. In the printer search process, you (referring to this class) are responsible for adding the discovered printers, removing the previously added but invalid printers, and updating the printers that have been added.
During the lifetime of this session, you may be asked to start and stop looking for the printer multiple times. You will be called onStartPrinterDiscovery(List)
to start looking for the printer and then be called onStopPrinterDiscovery()
to stop looking for the printer. When the system no longer needs this session to look for the printer, you will receive the onDestroy()
call. At this point, the system will no longer call this session session
, the entire session of the method is no longer useful.
The discovered printer is added by calling the addPrinters(List)
method. The added printer is removed by calling removePrinters(List)
. The attributes and features of the added printer (properties or capabilities) are addPrinters(List)
updated by calling. These added printers can be getPrinters()
obtained, and the printer returned in this method is a recent snapshot of the printer you escalated (snapshot). These printers are not retained after the session.
If you (guess the user, referring to this class before) need to update some printers, the system will call onValidatePrinters(List)
. You may add only one printer without configuring its feature (capabilities). This mechanism allows you to avoid querying all printers in order to configure the functionality of the printer, but only to query the functionality of a printer when necessary. For example, if a printer is selected for use by the user, you will be asked to update the printer. You do not need to provide the features of the printer when verifying the printer, but you can do so.
If the system continues to monitor the latest state of the printer, you will receive a onStartPrinterStateTracking(PrinterId)
call. Once received, you must keep the system up to date with the latest state and functionality of the printer. When you add a printer before you do not provide its features, you must update the content at this time. Otherwise, the printer will be ignored. You receive a call when the system no longer needs to get the latest state of the printer onStopPrinterStateTracking(PrinterId)
.
tip: All callback functions in this class are executed in the main thread of the program. You also call these functions on the main thread when you use them yourself.
Public final class
Printerinfo
Extends Object
Implements Parcelable
java.lang.Object ? android.print.PrinterInfo
Category Overview Class overview
This class is a description of a printer. Instances of this class are created by the Print Service and report the printers they manage to the system. There are two parts of the information in this class. The first part, printer properties, columns such as name, ID (number), status (state), description (description). The second part, printer capabilities (printer capability). Printer capabilities describes the various print modes supported by the printer, such as media sizes (probably paper size), margins (left white size), and so on.
Public final class
Printerid
Extends Object
Implements Parcelable
java.lang.Object ? android.print.PrinterId
Category Overview Class overview
This class represents a unique number for a printer
Public final class
PrintJob
Extends Object
java.lang.Object ? android.printservice.PrintJob
Category Overview Class overview
From the point of view of a print service, this class represents a print job. It provides APIs to observe the status of the print task and perform actions on the print task.
Tip: All methods inside the class must be executed in the main thread of the application.
Public final class
Printjobinfo
Extends Object
Implements Parcelable
java.lang.Object ? android.print.PrintJobInfo
Category Overview Class overview
This class represents a detailed description of a print task. The status of a printer task includes some configuration, such as its ID (number), print properties, which are used to generate some content, and so on. Note that the print task status may change over time. This class represents only one moment of the state (snapshot).
Public final class
Printercapabilitiesinfo
Extends Object
Implements Parcelable
java.lang.Object ? android.print.PrinterCapabilitiesInfo
Category Overview Class overview
This class represents the ability of a printer (capabilities). An instance of this class is created by a print service to report on the capabilities of the printer it manages. The ability of a printer indicates how it prints the content. For example: What paper size the printer supports (media sizes), the minimum white space for the printer design (the minimal margins), and so on.
Public final class
PrintDocument
Extends Object
java.lang.Object ? android.printservice.PrintDocument
Category Overview Class overview
For a print service, this class represents a file to be printed. It provides APIs to query the file and the data it contains.
Tip: All methods must be executed in the main thread of the program.
Public final class
Printdocumentinfo
Extends Object
Implements Parcelable
java.lang.Object ? android.print.PrintDocumentInfo
Category Overview Class overview
This class encapsulates information about a document that is related to printing. This meta-data
(meta data) is used by the platform (platform) and the Print service to compose and interact with the printer. For example, this class contains the number of pages in a document, and the number of document pages is presented to the user so that they can select a range to print. A print service may optimize printing for content types, such as documents or photos.
An instance of this class is created by the application that needs to be printed, and the callback function is called after the content is successfully typeset (laying out the contents) PrintDocumentAdapter.LayoutResultCallback.onLayoutFinished( PrintDocumentInfo, boolean)
. This process is carried out in PrintDocumentAdapter.onLayout(PrintAttributes, PrintAttributes, android.os.CancellationSignal, PrintDocumentAdapter.LayoutResultCallback, android.os.Bundle)
.
An example of use is as follows:
... public void onlayout (Printattributes oldattributes, Printattributes newattributes, cancellationsignal cancellationsignal, LayoutResult Callback Callback, Bundle metadata) {//assume the app defined a Layoutresult C Lass which contains //the layout result data and the content is a document. Layoutresult result = Dosomelayoutwork (); Printdocumentinfo info = new printdocumentinfo. Builder ( "printed_file.pdf" ). setContentType (Printdocumentinfo.content_type _document). Setpagecount (Result.getpagecount ()). build (); Callback.onlayoutfinished (Info, result.getcontentchanged ()); } . . .
Android_printservice_api_ Partial translation