Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

createPrintDocumentAdapter replacement

I get a warning when building my android project that "createPrintDocumentAdapter" has been deprecated. It is a method on WebView.

I have been looking but have been unable to find what the intended replacement is. Can anyone shed some light on this?

Thanks, Jason

like image 346
Jason Bray Avatar asked Feb 08 '16 01:02

Jason Bray


2 Answers

Per the Javadoc for createPrintDocumentAdapter():

This method was deprecated in API level 21.

Use createPrintDocumentAdapter(String) which requires user to provide a print document name.

Therefore if you need to support < API 21, continue to use the current method, but on API 21+ devices, use createPrintDocumentAdapter(String)

like image 103
ianhanniballake Avatar answered Nov 09 '22 03:11

ianhanniballake


Based on the accepted answer and the official example:

@SuppressWarnings("deprecation")
private static void createWebPrintJob(Context context, WebView webView) {

    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) context.getSystemService(Context.PRINT_SERVICE);

    String jobName = context.getString(R.string.app_name) + " Document";

    // Get a print adapter instance
    PrintDocumentAdapter printAdapter;
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        printAdapter = webView.createPrintDocumentAdapter(jobName);
    }
    else {
        printAdapter = webView.createPrintDocumentAdapter();
    }

    // Create a print job with name and adapter instance

    PrintJob printJob = printManager.print(jobName, printAdapter,
            new PrintAttributes.Builder().build());

    // Save the job object for later status checking
    mPrintJobs.add(printJob);

}
like image 41
ban-geoengineering Avatar answered Nov 09 '22 03:11

ban-geoengineering