Можно ли получить этот список доступных принтеров в моем собственном приложении?Как?
Да, вы можете использовать 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.")
}
}