如何获得,在我的应用程序中,打印机列表以前解决Android安装打印服务?

问题描述 投票:0回答:2

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

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

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

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

提前致谢

- - - - 脚步 - - - -

  1. Android打印设置

  1. 已安装的服务

  1. 打印机列表

android plugins service printing printers
2个回答
0
投票

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

是的你可以使用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.")
    }
}

0
投票

到目前为止,没有找到适当的解决方案。可能的选择是:

  1. 尝试通过服务插件(例如PrintHand)存储的共享首选项访问打印机。
  2. 如果您只想轻松获取打印机的IP地址并使用带有条形码扫描器的设备,您可以在打印机上放置一个带有IP地址的标签并进行扫描以将地址存储在会话/单一字段中。

我采取了第二条道路。

© www.soinside.com 2019 - 2024. All rights reserved.