简体   繁体   中英

How to get, inside my app, a list of printers previously settle to android printing services?

After setting up a printer through services plugin (eg Steps 1..3) on Android OS, my working flow should:

  • Press "print" button
  • Show dialog with available printers (previously defined on Android settings)
  • Execute pdf printing

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 -------

  1. Android Printing Settings

Android打印设置]

  1. Installed Services

已安装的服务

  1. Printers List

打印机列表

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:

  1. Try to access printers through shared preferences stored by services plugins (eg PrintHand).

  2. 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM