隐藏适用于HP打印机的Android打印对话框 [英] Hide Android print dialog for Hp printers
问题描述
我已经开发了一个Android应用程序,可以使用PrintHelper或Hp移动打印SDK打印照片.但是,在打印过程之前,屏幕上会出现"Android打印对话框".
I have developed an Android application which prints a photo using either PrintHelper or Hp mobile print SDK. However, when before the printing process, Android print Dialog comes up on the screen.
当应用程序打印照片时,如何跳过android打印对话框?
我已经遇到以下问题.简而言之,答案是没有办法."
I have already come across below questions. Briefly, the answers are "there is no way to do this."
- 在没有打印对话框的情况下进行打印Android
- 从没有系统对话框的android打印
- 是是否可以直接在没有android编程的用户交互的情况下将内容打印到带有wifi的打印机上?
- https://code.google.com/p/android/issues/detail?id = 160908
- Print without print dialog Android
- Print from android without system dialog
- Is it possible to directly print content to a wifi-connnected printer without user interaction in android programming?
- https://code.google.com/p/android/issues/detail?id=160908
但是,我尝试使用 HP ePrint android应用程序打印任何照片.在此应用程序中,没有任何Android打印对话框,它可以直接打印任何文档,而无需在屏幕上显示Android打印对话框.
However, I have tried to use HP ePrint android application to print any photo. In this application, there is no any Android Print Dialog, It can print any document directly, without displaying Android Print Dialog on the screen.
这意味着,有一种方法可以直接打印文档.
So that means, there is a way to print a doc directly.
推荐答案
通过调用打印适配器生命周期方法,可以将printint转换为PDF.但是,由于回调是非公共抽象类,并且如果提供null,则系统将引发segfault,因此您需要使用 DexMaker 来实施它们.我已经为这样的webView适配器实现了:
It is possible for printint to PDF by calling print adapter lifecycle methods. However as callbacks are non public abstract classes and system throws segfault if null is supplied, you need to use DexMaker to implement them. I have implemented for webView adapter like this:
@Override
protected void onPreExecute() {
super.onPreExecute();
printAdapter = webView.createPrintDocumentAdapter();
}
@Override
protected Void doInBackground(Void... voids) {
File file = new File(pdfPath);
if (file.exists()) {
file.delete();
}
try {
file.createNewFile();
// get file descriptor
descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
// create print attributes
PrintAttributes attributes = new PrintAttributes.Builder()
.setMediaSize(PrintAttributes.MediaSize.ISO_A4)
.setResolution(new PrintAttributes.Resolution("id", PRINT_SERVICE, 300, 300))
.setColorMode(PrintAttributes.COLOR_MODE_COLOR)
.setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0))
.build();
ranges = new PageRange[]{new PageRange(1, numberPages)};
// dexmaker cache folder
cacheFolder = new File(context.getFilesDir() +"/etemp/");
printAdapter.onStart();
printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (method.getName().equals("onLayoutFinished")) {
onLayoutSuccess();
} else {
Log.e(TAG, "Layout failed");
pdfCallback.onPdfFailed();
}
return null;
}
}, cacheFolder), new Bundle());
} catch (IOException e) {
e.printStackTrace();
Log.e(TAG, e != null ? e.getMessage() : "PrintPdfTask unknown error");
}
return null;
}
private void onLayoutSuccess() throws IOException {
PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (method.getName().equals("onWriteFinished")) {
pdfCallback.onPdfCreated();
} else {
Log.e(TAG, "Layout failed");
pdfCallback.onPdfFailed();
}
return null;
}
}, cacheFolder);
printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback);
}
/**
* Implementation of non public abstract class LayoutResultCallback obtained via DexMaker
* @param invocationHandler
* @param dexCacheDir
* @return LayoutResultCallback
* @throws IOException
*/
public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler,
File dexCacheDir) throws IOException {
return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class)
.dexCache(dexCacheDir)
.handler(invocationHandler)
.build();
}
/**
* Implementation of non public abstract class WriteResultCallback obtained via DexMaker
* @param invocationHandler
* @param dexCacheDir
* @return LayoutResultCallback
* @throws IOException
*/
public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler,
File dexCacheDir) throws IOException {
return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class)
.dexCache(dexCacheDir)
.handler(invocationHandler)
.build();
}
这篇关于隐藏适用于HP打印机的Android打印对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!