PrintService类打印
PrintService类打印
六级多少分及格系统打印服务框架代码位于包中。系统并没有实现具体打印功能,需要打印机⼚商制作插件接⼊系统打印服务之后,⾃⾏实现
主要类:
PrintDocument:表⽰待打印⽂件
PrintDiscoverySession:⽤于发现打印机和打印机状态更新
PrintJob:代表打印任务
PrintService:接⼊系统打印Service
打印机发现过程:
当⽤户在设置⾥开启你的打印服务插件和进⼊系统打印服务界⾯时,系统会调⽤ PrinterDiscoverySession ⾥的 onStartPrinterDiscovery(List priorityList) 函数,通知你的插件查打印机。具体查⽅式需要⾃⼰实现,可能是查USB接⼝,可能是搜打印机选择过程
当⽤户通过⼀些有打印功能的APP调⽤系统打印服务时,如果选择了你的插件的打印机,那么系统会调⽤ PrinterDiscoverySession ⾥的 onStartPrinterStateTracking(PrinterId printerId) ⽅法。这⾥系统主要希望得到打印机的 PrinterCapabilitiesInfo 和打印过程:
当⽤户在刚刚的系统打印服务界⾯点击右上⾓的打印按钮时,系统会调⽤打印机所属的 PrintService ⾥的 onPrintJobQueued(PrintJob printJob) ⽅法,插件需要处理该 PrintJob 。⾸先需要通过 PrintJob.isQueued() 判断,该PrintJob是否准备好打印,打印服务需要添加的权限:
<uses-permission android:name="android.permission.START_PRINT_SERVICE_CONFIG_ACTIVITY" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
打印类:
public class MyPrintService extends PrintService {
private static final String TAG = "MyPrintService";
@Override
protected PrinterDiscoverySession onCreatePrinterDiscoverySession() {
Log.d(TAG, "onCreatePrinterDiscoverySession()");
return new MyPrintDiscoverySession(this);
}
@Override
protected void onRequestCancelPrintJob(PrintJob printJob) {
Log.d(TAG, "onRequestCancelPrintJob()");
printJob.cancel();
}
@Override
protected void onPrintJobQueued(PrintJob printJob) {
Log.d(TAG, "onPrintJobQueued()");
PrintJobInfo printjobinfo = Info();
PrintDocument printdocument = Document();
if (printJob.isQueued()) {
return;
}
printJob.start();
String filename = "docu.pdf";
File outfile = new FilesDir(), filename);
outfile.delete();
FileInputStream file = new ParcelFileDescriptor.Data());
/
/创建⼀个长度为1024的内存空间
byte[] bbuf = new byte[1024];
//⽤于保存实际读取的字节数
int hasRead = 0;
//使⽤循环来重复读取数据
try {
FileOutputStream outStream = new FileOutputStream(outfile);
while ((hasRead = ad(bbuf)) > 0) {
//将字节数组转换为字符串输出
//System.out.print(new String(bbuf, 0, hasRead));
outStream.write(bbuf);
}
outStream.close();
} catch (IOException e) {
e.printStackTrace();
}finally {
//关闭⽂件输出流,放在finally块⾥更安全
try {
file.close();
} catch (IOException e) {
e.printStackTrace();
}成语疯狂猜全部答案
}
printJobplete();
}
}
PrinterDiscoverySession实现类编写:
public class MyPrintDiscoverySession extends PrinterDiscoverySession {
private static final String TAG = "MyPrintDiscoverySession";
private final MyPrintService myPrintService;
public MyPrintDiscoverySession(MyPrintService myPrintService) {
Log.d(TAG, "MyPrintDiscoverySession()");
}
@Override
public void onStartPrinterDiscovery(List<PrinterId> priorityList) {
Log.d(TAG, "onStartPrinterDiscovery()");
List<PrinterInfo> printers = Printers();
String name = "printer1";
PrinterInfo myprinter = new PrinterInfo
.atePrinterId(name), name, PrinterInfo.STATUS_IDLE)
.build();
printers.add(myprinter);
工科类专业
addPrinters(printers);
}
@Override
public void onStopPrinterDiscovery() {
Log.d(TAG, "onStopPrinterDiscovery()");
}
/**
* 确定这些打印机存在
* @param printerIds
*/
@Override
public void onValidatePrinters(List<PrinterId> printerIds) {
Log.d(TAG, "onValidatePrinters()");
}
/**
* 选择打印机时调⽤该⽅法更新打印机的状态,能⼒
* @param printerId
*/
@Override
public void onStartPrinterStateTracking(PrinterId printerId) {
Log.d(TAG, "onStartPrinterStateTracking()");
PrinterInfo printer = findPrinterInfo(printerId);
if (printer != null) {
PrinterCapabilitiesInfo capabilities =
new PrinterCapabilitiesInfo.Builder(printerId)
.setMinMargins(new PrintAttributes.Margins(200, 200, 200, 200))
.addMediaSize(PrintAttributes.MediaSize.ISO_A4, true)
植物大战僵尸存档位置
//.addMediaSize(PrintAttributes.MediaSize.ISO_A5, false)
.addResolution(new PrintAttributes.Resolution("R1", "200x200", 200, 200), false)                            .addResolution(new PrintAttributes.Resolution("R2", "300x300", 300, 300), true)                            .setColorModes(PrintAttributes.COLOR_MODE_COLOR
| PrintAttributes.COLOR_MODE_MONOCHROME,
PrintAttributes.COLOR_MODE_MONOCHROME)
.build();
printer = new PrinterInfo.Builder(printer)
.setCapabilities(capabilities)
.setStatus(PrinterInfo.STATUS_IDLE)
//        .setDescription("fake print 1!")
.build();
List<PrinterInfo> printers = new ArrayList<PrinterInfo>();
printers.add(printer);
addPrinters(printers);
}
为什么dnf频道进不去}
@Override
public void onStopPrinterStateTracking(PrinterId printerId) {
Log.d(TAG, "onStopPrinterStateTracking()");
}
护肤品什么品牌比较好@Override
public void onDestroy() {
Log.d(TAG, "onDestroy()");
}
private PrinterInfo findPrinterInfo(PrinterId printerId) {
List<PrinterInfo> printers = getPrinters();
final int printerCount = getPrinters().size();
for (int i = 0; i < printerCount; i++) {
PrinterInfo printer = (i);
if (Id().equals(printerId)) {
return printer;
}
}
return null;
}
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。