【发布时间】:2015-04-30 05:54:16
【问题描述】:
我正在使用默认的 android 打印机选项(android 版本 4.4)
我想绕过 printManager 适配器弹出窗口。如何隐藏弹出窗口并直接打印到android中的打印机
【问题讨论】:
标签: printing android-print-framework
我正在使用默认的 android 打印机选项(android 版本 4.4)
我想绕过 printManager 适配器弹出窗口。如何隐藏弹出窗口并直接打印到android中的打印机
【问题讨论】:
标签: printing android-print-framework
您不能扩展 PrintManager 类。这是最后一堂课。请查看以下链接
这些人设计了自己的打印框架。他们在哪里设计自己的对话框。看看
http://apf.isb-vietnam.com/index.php/programming-with-apf.html
【讨论】:
在我看来答案是是的。
这是来自 Android 的 PrintManager 类的 print() 方法:
public PrintJob print(String printJobName, PrintDocumentAdapter documentAdapter, PrintAttributes attributes)
{
if (mService == null)
{
Log.w(LOG_TAG, "Feature android.software.print not available");
return null;
}
if (!(mContext instanceof Activity))
{
throw new IllegalStateException("Can print only from an activity");
}
if (TextUtils.isEmpty(printJobName))
{
throw new IllegalArgumentException("printJobName cannot be empty");
}
if (documentAdapter == null)
{
throw new IllegalArgumentException("documentAdapter cannot be null");
}
PrintDocumentAdapterDelegate delegate = new PrintDocumentAdapterDelegate((Activity) mContext, documentAdapter);
try
{
Bundle result = mService.print(printJobName, delegate, attributes, mContext.getPackageName(), mAppId, mUserId);
if (result != null)
{
PrintJobInfo printJob = result.getParcelable(EXTRA_PRINT_JOB);
IntentSender intent = result.getParcelable(EXTRA_PRINT_DIALOG_INTENT);
if (printJob == null || intent == null)
{
return null;
}
try
{
mContext.startIntentSender(intent, null, 0, 0, 0);
return new PrintJob(printJob, this);
}
catch (SendIntentException sie)
{
Log.e(LOG_TAG, "Couldn't start print job config activity.", sie);
}
}
}
catch (RemoteException re)
{
Log.e(LOG_TAG, "Error creating a print job", re);
}
return null;
}
只需创建你自己的类(我会假装它被命名为MyPrintManager),其中extends PrintManager 和@Override 中的print() 方法。然后,使用上面的代码,但删除这一行:
mContext.startIntentSender(intent, null, 0, 0, 0);
然后,使用以下代码获取MyPrintManager 类的实例:
MyPrintManager printManager = (MyPrintManager) appContext.getSystemService(Context.PRINT_SERVICE);
你可以试试这个,虽然我不确定它是否有效,因为我还没有测试过。请回复结果,如果不起作用,我会尽力帮助您。
【讨论】:
PrintManager 类:android.googlesource.com/platform/frameworks/base/+/afd1967/… 这是一个IPrintManager,由系统在创建过程中提供。我认为您可以复制整个 PrintManager 类,但更改 print() 方法。