2016-09-17 261 views
0

我開發了一個使用PrintHelper或Hp移動打印SDK打印照片的Android應用程序。但是,在打印過程之前,Android打印對話框出現在屏幕上。隱藏Hp打印機的Android打印對話框

如何在應用程序打印照片時跳過android打印對話框?

我已經遇到以下問題。簡而言之,答案是「沒有辦法做到這一點。」

不過,我曾嘗試使用HP ePrint的Android應用程序打印任何照片。在這個應用程序中,沒有任何Android打印對話框,它可以直接打印任何文檔,而無需在屏幕上顯示Android Print對話框。

這就是說,有一種方法可以直接打印文檔。

+1

惠普可以直接使用自己的打印機。你不容易,因爲你不是惠普。 – CommonsWare

+0

加入CommonsWare和其他受到重視的答案,它不是微不足道的。你可以按照評論中提供的鏈接[this](http://stackoverflow.com/questions/20005975/hide-android-kitkat-4-4-printing-dialog/31570215#31570215)回答(陳述相同的判決),看看它是否有幫助。 – Onik

回答

0

通過調用打印適配器生命週期方法,可以將printint打印到PDF中。然而,由於回調是非公共抽象類,並且系統在提供null時拋出段錯誤,因此需要使用DexMaker來實現它們。我已經實現了這樣的webView適配器:

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    printAdapter = webView.createPrintDocumentAdapter(); 
} 

@Override 
protected Void doInBackground(Void... voids) { 

    File file = new File(pdfPath); 
    if (file.exists()) { 
     file.delete(); 
    } 
    try { 
     file.createNewFile(); 

     // get file descriptor 
     descriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_WRITE); 

     // create print attributes 
     PrintAttributes attributes = new PrintAttributes.Builder() 
       .setMediaSize(PrintAttributes.MediaSize.ISO_A4) 
       .setResolution(new PrintAttributes.Resolution("id", PRINT_SERVICE, 300, 300)) 
       .setColorMode(PrintAttributes.COLOR_MODE_COLOR) 
       .setMinMargins(new PrintAttributes.Margins(0, 0, 0, 0)) 
       .build(); 
     ranges = new PageRange[]{new PageRange(1, numberPages)}; 

     // dexmaker cache folder 
     cacheFolder = new File(context.getFilesDir() +"/etemp/"); 

     printAdapter.onStart(); 

     printAdapter.onLayout(attributes, attributes, new CancellationSignal(), getLayoutResultCallback(new InvocationHandler() { 
      @Override 
      public Object invoke(Object o, Method method, Object[] objects) throws Throwable { 

       if (method.getName().equals("onLayoutFinished")) { 
        onLayoutSuccess(); 
       } else { 
        Log.e(TAG, "Layout failed"); 
        pdfCallback.onPdfFailed(); 
       } 
       return null; 
      } 
     }, cacheFolder), new Bundle()); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.e(TAG, e != null ? e.getMessage() : "PrintPdfTask unknown error"); 
    } 
    return null; 
} 

private void onLayoutSuccess() throws IOException { 
    PrintDocumentAdapter.WriteResultCallback callback = getWriteResultCallback(new InvocationHandler() { 
     @Override 
     public Object invoke(Object o, Method method, Object[] objects) throws Throwable { 
      if (method.getName().equals("onWriteFinished")) { 
       pdfCallback.onPdfCreated(); 
      } else { 
       Log.e(TAG, "Layout failed"); 
       pdfCallback.onPdfFailed(); 
      } 
      return null; 
     } 
    }, cacheFolder); 
    printAdapter.onWrite(ranges, descriptor, new CancellationSignal(), callback); 
} 


/** 
* Implementation of non public abstract class LayoutResultCallback obtained via DexMaker 
* @param invocationHandler 
* @param dexCacheDir 
* @return LayoutResultCallback 
* @throws IOException 
*/ 
public static PrintDocumentAdapter.LayoutResultCallback getLayoutResultCallback(InvocationHandler invocationHandler, 
                       File dexCacheDir) throws IOException { 
    return ProxyBuilder.forClass(PrintDocumentAdapter.LayoutResultCallback.class) 
      .dexCache(dexCacheDir) 
      .handler(invocationHandler) 
      .build(); 
} 

/** 
* Implementation of non public abstract class WriteResultCallback obtained via DexMaker 
* @param invocationHandler 
* @param dexCacheDir 
* @return LayoutResultCallback 
* @throws IOException 
*/ 
public static PrintDocumentAdapter.WriteResultCallback getWriteResultCallback(InvocationHandler invocationHandler, 
                       File dexCacheDir) throws IOException { 
    return ProxyBuilder.forClass(PrintDocumentAdapter.WriteResultCallback.class) 
      .dexCache(dexCacheDir) 
      .handler(invocationHandler) 
      .build(); 
}