Google云打印源代码
2016-01-07 14:52
141 查看
所需权限
代码段1:按键代码(点击所需打印文档之后触发)
代码段2:发送打印作业界面
print_dialog.xml布局界面
<uses-permission android:name="android.permission.INTERNET" />
代码段1:按键代码(点击所需打印文档之后触发)
Intent printIntent = new Intent(this, PrintDialogActivity.class); printIntent.setDataAndType(docUri, docMimeType); //docUri: 文档的Uri路径,docMimeType: 文档的MIME类型,如PDF-> "application/pdf" printIntent.putExtra("title", docTitle); //docTitle: 文档标题 startActivity(printIntent);
代码段2:发送打印作业界面
package google.com.android.cloudprint; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.ContentResolver; import android.content.Intent; import android.os.Bundle; import android.util.Base64; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; import java.io.ByteArrayOutputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; public class PrintDialogActivity extends Activity { private static final String PRINT_DIALOG_URL = "https://www.google.com/cloudprint/dialog.html"; private static final String JS_INTERFACE = "AndroidPrintDialog"; private static final String CONTENT_TRANSFER_ENCODING = "base64"; private static final String ZXING_URL = "http://zxing.appspot.com"; private static final int ZXING_SCAN_REQUEST = 65743; /** * Post message that is sent by Print Dialog web page when the printing dialog * needs to be closed. */ private static final String CLOSE_POST_MESSAGE_NAME = "cp-dialog-on-close"; /** * Web view element to show the printing dialog in. */ private WebView dialogWebView; /** * Intent that started the action. */ Intent cloudPrintIntent; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.print_dialog); dialogWebView = (WebView) findViewById(R.id.webview); cloudPrintIntent = this.getIntent(); WebSettings settings = dialogWebView.getSettings(); settings.setJavaScriptEnabled(true); dialogWebView.setWebViewClient(new PrintDialogWebClient()); dialogWebView.addJavascriptInterface( new PrintDialogJavaScriptInterface(), JS_INTERFACE); dialogWebView.loadUrl(PRINT_DIALOG_URL); } @Override public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == ZXING_SCAN_REQUEST && resultCode == RESULT_OK) { dialogWebView.loadUrl(intent.getStringExtra("SCAN_RESULT")); } } final class PrintDialogJavaScriptInterface { public String getType() { return cloudPrintIntent.getType(); } public String getTitle() { return cloudPrintIntent.getExtras().getString("title"); } public String getContent() { try { ContentResolver contentResolver = getContentResolver(); InputStream is = contentResolver.openInputStream(cloudPrintIntent.getData()); ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = is.read(buffer); while (n >= 0) { baos.write(buffer, 0, n); n = is.read(buffer); } is.close(); baos.flush(); return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return ""; } public String getEncoding() { return CONTENT_TRANSFER_ENCODING; } public void onPostMessage(String message) { if (message.startsWith(CLOSE_POST_MESSAGE_NAME)) { finish(); } } } private final class PrintDialogWebClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (url.startsWith(ZXING_URL)) { Intent intentScan = new Intent("com.google.zxing.client.android.SCAN"); intentScan.putExtra("SCAN_MODE", "QR_CODE_MODE"); try { startActivityForResult(intentScan, ZXING_SCAN_REQUEST); } catch (ActivityNotFoundException error) { view.loadUrl(url); } } else { view.loadUrl(url); } return false; } @Override public void onPageFinished(WebView view, String url) { if (PRINT_DIALOG_URL.equals(url)) { // Submit print document. view.loadUrl("javascript:printDialog.setPrintDocument(printDialog.createPrintDocument(" + "window." + JS_INTERFACE + ".getType(),window." + JS_INTERFACE + ".getTitle()," + "window." + JS_INTERFACE + ".getContent(),window." + JS_INTERFACE + ".getEncoding()))"); // Add post messages listener. view.loadUrl("javascript:window.addEventListener('message'," + "function(evt){window." + JS_INTERFACE + ".onPostMessage(evt.data)}, false)"); } } } }
print_dialog.xml布局界面
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <WebView android:id="@+id/webview" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </RelativeLayout>
相关文章推荐
- matlab 获取网卡MAC地址
- PHP中strnatcmp()函数“自然排序算法”进行字符串比较用法分析(对比strcmp函数)
- form图片上传+spring MVC
- python 实例7 为rhel添加系统用户与登录密码,并给root 发送邮件
- 安卓编程当中的快捷键
- Spring MVC 学习笔记(一)
- java:@SuppressWarnings注解
- ThinkPHP里面用原生SQL
- Java加密
- 策略模式 c#
- matlab中 数据保留有效位数
- jasperreport 第一次导出会很慢的原因
- java 自定义注解
- 【c/c++】Vector入门
- Javase-I/O
- java爬虫糗百
- C语言学习之关键字第六讲
- struts2的整体回顾(ACTION、拦截器、值栈、OGNL表达式、ModelDriven方案等)
- Java生成登陆时使用的图片验证码
- 【Python那些事儿之七】Iterators详解