2017-01-10 20 views
1

私はアンドロイドのPOSアプリケーションを開発しています。 ボタンをクリックすると請求書が印刷されます。 印刷は、アプリケーションとのやりとりなしに、所定のプリンタにサイレントモードで実行する必要があります。プリンタにはIPアドレスが設定されています。 接続は真のWiFiまたはブルートゥース(おそらく無線LAN)で行う必要があります。Androidの印刷フレームワーク - 所定のプリンタへのカスタムPDFの自動印刷

アンドロイドの印刷フレームワーク(Android 4.4(APIレベル19))を使用していますが、これはGUIに固執しているようです(印刷するためのguiは必要ありません。発生する)。プリンタIPの真のコードを設定するオプションが見つかりませんでした。

私は今すぐ持っているコードを作成する簡単なチュートリアルに従ってきました。ここ はそれへのリンクです: http://www.techotopia.com/index.php/An_Android_Custom_Document_Printing_Tutorial

ので復習に、私はコードで設定し所定のプリンタに法案(あるいは単にスターターのために何を)を印刷するために私のカスタムのAndroid POSアプリが必要です。

ロンタのPOSプリンタにはアンドロイドのSDKがあります。 私は他のメーカーもプリンタ用に1台持っている必要があると思います。

+0

Androidの印刷フレームワークは、従来のAndroidシナリオで使用するように設計されています。そこには、印刷ジョブの確認と設定のために、ユーザーがUIを必要とします。私はそれを回避する方法がないことを知っています。将来的には、Android Thingsはダイレクトプリントオプションを可能にし、Androidをベースにする可能性はありますが、現在は存在しないAFAIKが可能です。 プリンタの製造元に相談して、プリンタと直接話す方法(ダイレクトソケット接続など)と独自の印刷システムをロールする方法があります。 – CommonsWare

+0

迅速な対応をありがとうございます。 –

+0

彼らはどのようにしてこの作品を制作したのですか?https://www.youtube.com/watch?v = M_inOqDzqjgこれは、彼らがPOSアプリを見せてくれるクロアチアのビデオのリンクです。かなりの数のプリンタと互換性があります。 –

答えて

0

printint to PDFは、印刷アダプタのライフサイクルメソッドを呼び出すことで可能です。ただし、コールバックはpublicではない抽象クラスであり、nullが指定されている場合はsegfaultがスローされるため、実装するには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(); 
} 
+0

これがどのように質問に答えるか説明してください。 – CommonsWare