Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android 4.4 Printing - "java.lang.IllegalStateException: printing is already pending"

I am working on printing a webview on android. I'm running into a problem where I get an Illegal State Exception. This only occurs when I call "print" two (or more) times very quick. The crash is not in my callstack either so using a try-catch won't work.

Note: I am making the calls via reflection (How do I print a WebView using KitKat 4.4 print API via reflection?) but I'm pretty sure using the API would do the same thing to me (But I will eventually check) (Edit: Just checked. Made a simple app with a webview and a button. When I click the button two times very quickly the whole app crashes.).

Stacktrace:

java.lang.IllegalStateException: printing is already pending
  at com.android.org.chromium.android_webview.AwPdfExporter.exportToPdf(AwPdfExporter.java:51)
  at com.android.org.chromium.android_webview.AwPrintDocumentAdapter.exportPdf(AwPrintDocumentAdapter.java:100)
  at com.android.org.chromium.android_webview.AwPrintDocumentAdapter.onWrite(AwPrintDocumentAdapter.java:81)
  at android.print.PrintManager$PrintDocumentAdapterDelegate$MyHandler.handleMessage(PrintManager.java:609)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4998)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:515)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
  at dalvik.system.NativeStart.main(Native Method)

The question is: How do I avoid this?

Edit: This is my test app. If I click the button twice before the dialog shows up then I get the exception shown above. How can I avoid this without using a something like a 'hacky' timeout.

MainActivity.java:

package com.example.test;

import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button button = (Button) findViewById(R.id.button1);
        final WebView webview = (WebView) findViewById(R.id.webView1);
        webview.setWebViewClient(new WebViewClient());
        webview.loadUrl("http://google.com");

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        createWebPrintJob(webview);
                    }
                });
            }
        });
    }

    private void createWebPrintJob(WebView webView) {
        PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
        String jobName = getString(R.string.app_name) + " Document";
        printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build());
    }
}

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="fill"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <WebView
        android:id="@+id/webView1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="Button" />

</RelativeLayout>
like image 823
Randy Avatar asked Nov 22 '13 15:11

Randy


1 Answers

This happens because, when you click the button twice two print job is created and the first print job hasn't completed yet,

Use a handler for the delay, to disable the button, this prevents a user from clicking twice accidentally.

You can't able to give print till the Print Spooler get cleared. To clear the Print Spooler settings -> Apps -> Show system apps -> select and clear catch for print spooler.

like image 104
livemaker Avatar answered Oct 19 '22 06:10

livemaker