【问题标题】:is this possible to print the web page in android without popup?这可以在没有弹出窗口的情况下在android中打印网页吗?
【发布时间】:2015-04-30 05:54:16
【问题描述】:

我正在使用默认的 android 打印机选项(android 版本 4.4)

我想绕过 printManager 适配器弹出窗口。如何隐藏弹出窗口并直接打印到android中的打印机

【问题讨论】:

    标签: printing android-print-framework


    【解决方案1】:

    您不能扩展 PrintManager 类。这是最后一堂课。请查看以下链接

    http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4.4_r1/android/print/PrintManager.java

    这些人设计了自己的打印框架。他们在哪里设计自己的对话框。看看

    http://apf.isb-vietnam.com/index.php/programming-with-apf.html

    【讨论】:

    【解决方案2】:

    在我看来答案是是的

    这是来自 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);
    

    你可以试试这个,虽然我不确定它是否有效,因为我还没有测试过。请回复结果,如果不起作用,我会尽力帮助您。

    【讨论】:

    • 上述方法中的mService是什么??
    • @John 看看 Android 源代码中的PrintManager 类:android.googlesource.com/platform/frameworks/base/+/afd1967/… 这是一个IPrintManager,由系统在创建过程中提供。我认为您可以复制整个 PrintManager 类,但更改 print() 方法。
    • 我尝试复制 PrintManager 类但没有找到使用 IPrintManager 类,并且缺少一些导入类,例如:import com.android.internal.os.SomeArgs;导入 libcore.io.IoUtils;导入android.printservice.PrintServiceInfo;如果我尝试从 GrepCode 复制任何文件。它不断地询问这么多的类文件。那么有没有其他方法可以解决这个问题??
    • @John 遗憾的是我不知道任何其他方式。我什至不确定这会奏效。对不起。
    • @John 没问题! ;)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-15
    • 1970-01-01
    • 2013-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多