is this possible to print the web page in android without popup?

若如初见. 提交于 2019-12-10 10:08:00

问题


I am using default android printer option (android version 4.4)

I want to byPass printManager adapter popup. how to hide the popup and give a direct print to the printer in android


回答1:


you cannot extend PrintManager class. It is a final class .Please check below link

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

These guys have designed their own printing framework.Where they are designing their own dialog.Have a look

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




回答2:


It seems to me like the answer is yes.

Here is the print() method from Android's PrintManager class:

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;
}

Simply create your own class (I will pretend it is named MyPrintManager) which extends PrintManager and @Override the print() method in it. Then, use the code above, but remove this line:

mContext.startIntentSender(intent, null, 0, 0, 0);

Then, get an instance of the MyPrintManager class using the code below:

MyPrintManager printManager = (MyPrintManager) appContext.getSystemService(Context.PRINT_SERVICE);

You could try this, though I am not sure if it works, because I haven't tested it. Please reply with the result, and I will try to help you further if it didn't work.



来源:https://stackoverflow.com/questions/28738148/is-this-possible-to-print-the-web-page-in-android-without-popup

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!