在Android OS上通过服务插件(例如步骤1..3)设置打印机后,我的工作流程应该是:
是否有可能在我自己的应用程序中获取此可用打印机列表?怎么样?
到目前为止,我通过谷歌的文档运行的最接近的解决方案是在网络预览上打开我的pdf,从那里让Android处理所有内容。但是,如果可能的话,我不想打破我的用户体验。选择我的打印机后,理想的情况是直接打印pdf。
提前致谢
- - - - 脚步 - - - -
是否有可能在我自己的应用程序中获取此可用打印机列表?怎么样?
是的你可以使用PrintService
:https://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.")
}
}
到目前为止,没有找到适当的解决方案。可能的选择是:
我采取了第二条道路。