Android WebView打印支持:doWebViewPrint()方法实现位置及教程咨询
Hey there! Let's break down your questions about implementing WebView printing in your Android app, and share some solid resources to help you out.
Where to Implement doWebViewPrint()
Your current placement of doWebViewPrint() inside MainActivity is totally reasonable—since it’s tied directly to your WebView’s behavior and the Activity’s lifecycle, keeping it here makes logical sense. But let’s refine how and when you use it:
1. Call Timing Options
- Automatic trigger after page load: If you want to print the page as soon as it finishes loading, uncomment the
doWebViewPrint();line in youronPageFinishedmethod. Just note this will trigger print every time the page loads, which might not be what your users want. - User-initiated trigger (better for most cases): Since your web page has print options, you’ll want to let users trigger print via the web UI. To do this, set up a JavaScript interface between your WebView and Android code:
Then in your web page’s print button, add this JS code:// Add this in your onCreate method after enabling JavaScript webView.addJavascriptInterface(new PrintBridge(), "AndroidPrint"); // Create an inner class to handle JS calls class PrintBridge { @JavascriptInterface public void startPrint() { // Run on UI thread since WebView operations need to be on main thread runOnUiThread(() -> doWebViewPrint()); } }document.getElementById("your-print-button-id").addEventListener("click", function() { AndroidPrint.startPrint(); });
2. Optimize the doWebViewPrint() Method
Your current code creates a new WebView just for printing, which works for test content, but if you want to print the actual content loaded in your existing WebView, you can simplify it to avoid redundant loading:
private void doWebViewPrint() { // Get PrintManager system service PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE); String jobName = getString(R.string.app_name) + " Web Page"; // Use your existing WebView to create the print adapter PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter(jobName); // Create and track the print job PrintJob printJob = printManager.print(jobName, printAdapter, new PrintAttributes.Builder().build()); printJobs.add(printJob); }
This is more efficient and ensures you’re printing the exact content the user sees.
Recommended Tutorial Resources
- Android Official Documentation: The Android Developers guide on Printing HTML Content is the most reliable source. It covers everything from basic WebView printing to customizing print settings, handling print job statuses, and troubleshooting common issues.
- Android Code Labs: Look for hands-on code labs focused on printing in Android—these walk you through real-world scenarios like printing dynamic content and integrating print with WebView JS interfaces.
- Community Tutorials: Search for Android WebView print tutorials that focus on JS-Android interaction, since that’s key to connecting your web page’s print button to your app’s native print logic. These often include step-by-step examples tailored to real app use cases.
内容的提问来源于stack exchange,提问作者Stephen Balaji




