大家好我正在为Android安装一个小样本打印应用程序,并想知道是否有办法让系统跳过打印预览阶段。例如,我的示例是加载基本HTML页面并通过打印框架打印它。那么有什么办法可以在不看打印预览屏幕的情况下完成打印作业吗?代码如下。 谢谢 !
public class WifiPrintActivity extends AppCompatActivity {
public static String TAG = "WifiPrintActivity";
public static String mContentType = "text/HTML";
public static String mCharset = "UTF-8";
public WebView mPrintDocumentWebView;
public ArrayList<PrintJob> mPrintJobs = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_wifi_print);
mPrintDocumentWebView = (WebView) findViewById(R.id.web_print_view);
// Create a WebView object specifically for printing
WebView webView = new WebView(this);
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);
mPrintDocumentWebView = null;
}
});
webView.loadUrl("http://developer.android.com/about/index.html");
mPrintDocumentWebView = webView;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_wifi_print, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public String getWebPageContent(){
String htmlDocument = "<html><body><h1>Test Content</h1><p>Testing, " + "testing, testing...</p></body></html>";
return htmlDocument;
}
private void createWebPrintJob(WebView webView) {
// Get a PrintManager instance
PrintManager printManager = (PrintManager) this.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);
}
}