问题
I would like to add wireless printing to my android 2.2 application which is targeted for the Galaxy tablet. I see that the internet browser has a print option so I am assuming that an activity hook must exist, and I was hoping that someone has figured this out. I have found a possibility using the PrinterShare application from Mobile Dynamix, but my preference would be to no require a 3rd party.
Here is the code example that they provide, just for reference.
Intent i = new Intent(Intent.ACTION_VIEW);
i.setPackage("com.dynamixsoftware.printershare");
i.setDataAndType(data_uri, data_type);
startActivity(i);
Where:
data_uri - Uri of the object to print, such as "file:///sdcard/something.pdf" or "content://something"
data_type - Mime type. The following mime types are supported: "application/pdf" "text/html" "text/plain" "image/png" "image/jpeg"
回答1:
Printing is not yet supported on Android. You have to use a 3rd party solution like Mobile Dynamix, HP iPrint for Android or Send2Printer.
Intents are described here:
http://www.openintents.org/en/node/735
http://www.openintents.org/en/node/278
回答2:
Use build in PrintManager Service
private void doWebViewPrint() {
mWebView = new WebView(PrintActivity.this);
WebSettings settings = mWebView.getSettings();
settings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
@Override
public void onPageFinished(WebView view, String url) {
mProgressBar.setVisibility(View.GONE);
createWebPrintJob(view);
mWebView = null;
}
});
mWebView.loadUrl(urlToPrint);
}
protected void createWebPrintJob(WebView webView) {
PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);
@SuppressWarnings("deprecation")
PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();
mPrintJob = printManager.print(docName, printAdapter,
new PrintAttributes.Builder().build());
}
you need to set
urlToPrint = "whatUrlToPrint"
docName = "anyDocumentName";
来源:https://stackoverflow.com/questions/4580088/android-printing-api-on-galaxy-tab