日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android打印html页面,Android打印HTML文档

發布時間:2024/9/27 Android 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android打印html页面,Android打印HTML文档 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

來源官網,總結用.

WebView類在Android?4.4(API?Level?19)中得到了更新,使得它可以打印HTML內容。該類允許我們加載一個本地HTML資源或者從網頁下載一個頁面,創建一個打印任務,并把它交給Android打印服務。

1.[代碼]如何構建一個HTML的字符串并將它加載到WebView中

private WebView mWebView;

private void doWebViewPrint() {

// Create a WebView object specifically for printing

WebView webView = new WebView(getActivity());

webView.setWebViewClient(new WebViewClient() {

public boolean shouldOverrideUrlLoading(WebView view, String url) {

return false;

}

@Override

public void onPageFinished(WebView view, String url) {

Log.i(TAG, "page finished loading " + url);

createWebPrintJob(view);

mWebView = null;

}

});

// Generate an HTML document on the fly:

String htmlDocument = "

Test Content

Testing, " +

"testing, testing...

";

webView.loadDataWithBaseURL(null, htmlDocument, "text/HTML", "UTF-8", null);

// Keep a reference to WebView object until you pass the PrintDocumentAdapter

// to the PrintManager

mWebView = webView;

}

/**

*Note: 請確保在WebViewClient)中的onPageFinished()方法內調用創建打印任務的方法。

如果沒有等到頁面加載完畢就進行打印,打印的輸出可能會不完整或空白,甚至可能會失敗。

Note:在上面的樣例代碼中,保留了一個WebView對象實例的引用,

這樣能夠確保它不會在打印任務創建之前就被垃圾回收器所回收。

在編寫代碼時請務必這樣做,否則打印的進程可能會無法繼續執行。

*/

2.[代碼]創建一個打印任務

/**

在創建了WebView并加載了我們的HTML內容之后,應用就已經幾乎完成了屬于它的任務。

接下來,我們需要訪問PrintManager,創建一個打印適配器,并在最后創建一個打印任務。*/

private void createWebPrintJob(WebView webView) {

// Get a PrintManager instance

PrintManager printManager = (PrintManager) getActivity()

.getSystemService(Context.PRINT_SERVICE);

// Get a print adapter instance

PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

// Create a print job with name and adapter instance

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

PrintJob printJob = printManager.print(jobName, printAdapter,

new PrintAttributes.Builder().build());

// Save the job object for later status checking

mPrintJobs.add(printJob);

}

總結

以上是生活随笔為你收集整理的android打印html页面,Android打印HTML文档的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。