2
這裏我有編碼,以獲得設備列表,我會檢查每個設備的狀態打印機有效性的影響
DocFlavor myFormat = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
PrintService[] services =PrintServiceLookup.lookupPrintServices(myFormat, aset);
System.out.println("The following printers are available");
for (int i=0;i<services.length;i++) {
PrintService printService = services[i];
PrintServiceAttributeSet printServiceAttributes = printService.getAttributes();
PrinterState printerState =
(PrinterState)printServiceAttributes.get(PrinterState.class);
if (printerState != null){
System.out.println(services[i].getName() + " is online");
} else {
System.out.println(services[i].getName() + " is offline");
}
}
但每一次我得到的狀態爲「脫機」,即使該打印機接通問題或關閉
你確定printerState在它離線的情況下將爲空嗎? – banjara
是的,當打印機打開和關閉時它會返回null – Bucks