我在标题中怎么说,我需要在我的应用程序上实现系统打印,我添加此类并实现此代码,但是我不明白为什么当我点击代表“打印”的“ Stampa”时,纸张会出来但没有印任何东西。 我该如何解决? (我使用Printooth库) 我也尝试使用互联网上的不同代码,我发现每个人都遇到同样的问题 代码:
package com.example.testaapplication0209;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;
import com.mazenrashed.printooth.Printooth;
import com.mazenrashed.printooth.data.printable.Printable;
import com.mazenrashed.printooth.data.printable.RawPrintable;
import com.mazenrashed.printooth.data.printable.TextPrintable;
import com.mazenrashed.printooth.data.printer.DefaultPrinter;
import com.mazenrashed.printooth.ui.ScanningActivity;
import com.mazenrashed.printooth.utilities.Printing;
import com.mazenrashed.printooth.utilities.PrintingCallback;
import java.util.ArrayList;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class PrintActivity extends AppCompatActivity implements PrintingCallback {
Printing printing;
Button btn_unpair_pair, btn_print;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_print);
Printooth.INSTANCE.init(this);
printing = Printooth.INSTANCE.printer();
initViews();
}
private void initViews() {
btn_print = findViewById(R.id.btn_print);
btn_unpair_pair = findViewById(R.id.btn_pair_unpair);
if (printing != null)
printing.setPrintingCallback(this);
btn_print.setOnClickListener(view -> {
if (!Printooth.INSTANCE.hasPairedPrinter())
startActivityForResult(new Intent(PrintActivity.this, ScanningActivity.class), ScanningActivity.SCANNING_FOR_PRINTER);
else
printText();
});
changePairAndUnpair();
}
private void printText() {
ArrayList<Printable> printables = new ArrayList<>();
printables.add(new RawPrintable.Builder(new byte[]{27, 100, 4}).build());
//Add Text
printables.add(new TextPrintable.Builder().setText("Hello World: Jagadeesh")
.setCharacterCode(DefaultPrinter.Companion.getCHARCODE_PC1252())
.setNewLinesAfter(1)
.build());
//Custom Text
printables.add(new TextPrintable.Builder()
.setText("Hello World")
.setLineSpacing(DefaultPrinter.Companion.getLINE_SPACING_60())
.setAlignment(DefaultPrinter.Companion.getALIGNMENT_CENTER())
.setEmphasizedMode(DefaultPrinter.Companion.getEMPHASIZED_MODE_BOLD())
.setUnderlined(DefaultPrinter.Companion.getUNDERLINED_MODE_ON())
.setNewLinesAfter(1)
.build());
printing.print(printables);
}
private void changePairAndUnpair() {
if (Printooth.INSTANCE.hasPairedPrinter())
btn_unpair_pair.setText(new StringBuilder("Unpair ").append(Printooth.INSTANCE
.getPairedPrinter().getName()).toString());
else
btn_unpair_pair.setText("Pair with Printer");
}
@Override
public void connectingWithPrinter() {
Toast.makeText(this, "Connecting to Printer", Toast.LENGTH_SHORT).show();
}
@Override
public void connectionFailed(String s) {
Toast.makeText(this, "Failed: " + s, Toast.LENGTH_SHORT).show();
}
@Override
public void onError(String s) {
Toast.makeText(this, "Error: " + s, Toast.LENGTH_SHORT).show();
}
@Override
public void onMessage(String s) {
Toast.makeText(this, "Message: " + s, Toast.LENGTH_SHORT).show();
}
@Override
public void printingOrderSentSuccessfully() {
Toast.makeText(this, "Order sent to printer", Toast.LENGTH_SHORT).show();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ScanningActivity.SCANNING_FOR_PRINTER && resultCode == Activity.RESULT_OK)
initPrinting();
changePairAndUnpair();
}
private void initPrinting() {
if (!Printooth.INSTANCE.hasPairedPrinter())
printing = Printooth.INSTANCE.printer();
if (printing != null)
printing.setPrintingCallback(this);
}
}
答案 0 :(得分:0)
更改代码的这一行:
printing.print(printables);
对此:
Printooth.INSTANCE.printer().print(printables);