After setting up a printer through services plugin (eg Steps 1..3) on Android OS, my working flow should:
Is it possible to acquire this list of available printers inside my own application? How?
So far, closest solution I got running through google's documentation was to open my pdf on a web preview and from there let Android handle everything. However, if possible, I wouldn't like to break my UX. After selecting my printer, ideal scenario would be to print pdfs directly.
Thanks in advance
------- STEPS -------
Is it possible to acquire this list of available printers inside my own application? How?
Yes you can use PrintService
: https://developer.android.com/reference/android/printservice/PrintService
A print service is responsible for discovering printers, adding discovered printers, removing added printers, and updating added printers.
The Android docs also have (somewhat outdated, but still useful) lessons on using the printing-related APIs: https://developer.android.com/training/printing
This sample includes code related to printing a PDF: https://developer.android.com/training/printing/custom-docs
Sample code from the docs:
// Connect to the print manager
private fun doPrint() {
activity?.also { context ->
// Get a PrintManager instance
val printManager = context.getSystemService(Context.PRINT_SERVICE) as PrintManager
// Set job name, which will be displayed in the print queue
val jobName = "${context.getString(R.string.app_name)} Document"
// Start a print job, passing in a PrintDocumentAdapter implementation
// to handle the generation of a print document
printManager.print(jobName, MyPrintDocumentAdapter(context), null)
}
}
// Compute print document info
override fun onLayout(
oldAttributes: PrintAttributes?,
newAttributes: PrintAttributes,
cancellationSignal: CancellationSignal?,
callback: LayoutResultCallback,
extras: Bundle?
) {
// Create a new PdfDocument with the requested page attributes
pdfDocument = PrintedPdfDocument(activity, newAttributes)
// Respond to cancellation request
if (cancellationSignal?.isCanceled == true) {
callback.onLayoutCancelled()
return
}
// Compute the expected number of printed pages
val pages = computePageCount(newAttributes)
if (pages > 0) {
// Return print information to print framework
PrintDocumentInfo.Builder("print_output.pdf")
.setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
.setPageCount(pages)
.build()
.also { info ->
// Content layout reflow is complete
callback.onLayoutFinished(info, true)
}
} else {
// Otherwise report an error to the print framework
callback.onLayoutFailed("Page count calculation failed.")
}
}
So far, no proper solution was found. Possible alternatives are:
Try to access printers through shared preferences stored by services plugins (eg PrintHand).
If you just to want to ease acquisition of printer's IP address & case you're using devices with bar code scanner, you could place a label on the printer with its IP Address and make a scan to store the address on a session/singleton field.
I took the second path.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.