【问题标题】:How to get, inside my app, a list of printers previously settle to android printing services?如何在我的应用程序中获取以前使用 android 打印服务的打印机列表?
【发布时间】:2019-04-03 17:22:30
【问题描述】:

在 Android 操作系统上通过服务插件(例如步骤 1..3)设置打印机后,我的工作流程应该:

  • 按“打印”按钮
  • 显示可用打印机对话框(之前在 Android 设置中定义)
  • 执行 pdf 打印

是否可以在我自己的应用程序中获取此可用打印机列表?怎么样?

到目前为止,我通过谷歌文档运行的最接近的解决方案是在网络预览中打开我的 pdf,然后让 Android 处理所有事情。但是,如果可能的话,我不想破坏我的用户体验。选择我的打印机后,理想的情况是直接打印 pdf。

提前致谢

------- 步骤 -------

  1. Android 打印设置

  1. 已安装的服务

  1. 打印机列表

【问题讨论】:

    标签: android plugins service printing printers


    【解决方案1】:

    是否可以在我自己的应用程序中获取此可用打印机列表?怎么样?

    是的,您可以使用PrintServicehttps://developer.android.com/reference/android/printservice/PrintService

    打印服务负责发现打印机、添加发现的打印机、删除添加的打印机以及更新添加的打印机。

    Android 文档也有(有些过时,但仍然有用)关于使用打印相关 API 的课程:https://developer.android.com/training/printing

    此示例包含与打印 PDF 相关的代码:https://developer.android.com/training/printing/custom-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.")
        }
    }
    

    【讨论】:

    • PrintService 不提供对以前通过“设置”解决的打印机的访问权限,因此这不是该问题可接受的解决方案。
    【解决方案2】:

    到目前为止,还没有找到合适的解决方案。可能的替代方案是:

    1. 尝试通过服务插件(例如 PrintHand)存储的共享首选项访问打印机。

    2. 如果您只是想轻松获取打印机的 IP 地址以及使用带有条形码扫描仪的设备的情况,您可以在打印机上放置一个带有 IP 地址的标签并进行扫描以存储地址在会话/单例字段上。

    我选择了第二条路。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多