我想在没有用户交互的情况下自动打印

时间:2018-02-02 08:35:51

标签: android

当用户点击UI中的按钮时,PrintManager会生成要打印的文档。然后用户单击“打印”图标以按下打印命令。

以上部分已完成。 但要求是: 我想删除用户必须单击打印图标(图片中显示的打印图标,由PrintDocumentAdapter生成)并自动生成/打印文档。

再次: 我已经实现了打印功能,我只想删除用户交互。生成低于pic时,将自动执行打印命令,无需用户单击打印图标。

enter image description here

3 个答案:

答案 0 :(得分:0)

您可以尝试以下操作:

TradeMessageType

答案 1 :(得分:0)

不要使用PrintManager,您可以使用android.print包中的PdfPrint.java类进行打印。

将此代码放在java / android / print中(在程序包外部)

// an initial pointer (usually its size is 32bit or 64bit, depending on CPU/OS).
// it's value is currently NULL (not pointing anywhere), 
// so we can't do very much with this right now. 
char* p = nullptr;

// for the sake of sanity, check the value (should be zero)
// we have to convert to intptr_t, otherwise we'd get the string
// value being printed out. 
std::cout << "p address = " << intptr_t(p) << std::endl << std::endl;

// lets allocate a few chars to play with
p = new char[10];

// copy in some text value 
std::strcpy(p, "Hello");

// and now if we print the address, the text string, 
// and the char we are pointing at
std::cout << "p address = " << intptr_t(p) << std::endl;
std::cout << "p string = " << p << std::endl;
std::cout << "p dereferenced = " << *p << std::endl << std::endl;

// for fun, lets increment the pointer by 1
++p;

// this should have made a couple of changes here
std::cout << "p address = " << intptr_t(p) << std::endl;
std::cout << "p string = " << p << std::endl;
std::cout << "p dereferenced = " << *p << std::endl << std::endl;

// decrement again (so we can delete the correct memory allocation!)
--p;

// now free the original allocation
delete [] p;

// if we print again, notice it still has the memory location?
std::cout << "p address = " << intptr_t(p) << std::endl;

// This would be bad to access (we've just deleted the memory)
// So as a precaution, set the pointer back to null
p = nullptr;

// should be back where we started
std::cout << "p address = " << intptr_t(p) << std::endl;

您可以使用类似的代码(从网络视图打印)

package android.print;

import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.util.Log;

import java.io.File;

public class PdfPrint {

    private static final String TAG = PdfPrint.class.getSimpleName();
    private final PrintAttributes printAttributes;

    public PdfPrint(PrintAttributes printAttributes) {
        this.printAttributes = printAttributes;
    }

    public void print(final PrintDocumentAdapter printAdapter, final File path, final String fileName) {
        printAdapter.onLayout(null, printAttributes, null, new PrintDocumentAdapter.LayoutResultCallback() {
            @Override
            public void onLayoutFinished(PrintDocumentInfo info, boolean changed) {
                printAdapter.onWrite(new PageRange[]{PageRange.ALL_PAGES}, getOutputFile(path, fileName), new CancellationSignal(), new PrintDocumentAdapter.WriteResultCallback() {
                    @Override
                    public void onWriteFinished(PageRange[] pages) {
                        super.onWriteFinished(pages);
                    }
                });
            }
        }, null);
    }

    private ParcelFileDescriptor getOutputFile(File path, String fileName) {
        if (!path.exists()) {
            path.mkdirs();
        }
        File file = new File(path, fileName);
        try {
            file.createNewFile();
            return ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE);
        } catch (Exception e) {
            Log.e(TAG, "Failed to open ParcelFileDescriptor", e);
        }
        return null;
    }
}

答案 2 :(得分:-1)

对于PrintManager实现,您可以查看以下代码行:

资料来源: https://developer.android.com/training/printing/custom-docs.html

private void doPrint() {
    // Get a PrintManager instance
    PrintManager printManager = (PrintManager) getActivity()
            .getSystemService(Context.PRINT_SERVICE);

    // Set job name, which will be displayed in the print queue
    String jobName = getActivity().getString(R.string.app_name) + " Document";

    // Start a print job, passing in a PrintDocumentAdapter implementation
    // to handle the generation of a print document
    printManager.print(jobName, new MyPrintDocumentAdapter(getActivity()),
            null); //
}