android 4.4 print,Android 4.4 Printing - "java.lang.IllegalStateException: printing is already pendi...

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:

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" >

android:id="@+id/webView1"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:layout_centerHorizontal="true"

android:layout_centerVertical="true" />

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" />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值