Android无线连接打印第三方开发的实现

[java]  view plain copy
  1. <span style="font-size:18px;"></span>   

         最近在做关于Android的项目,Android果然不是出于国内,很多东西都是国外已经成熟了或者已经开发好了,国内去效仿。为了找关于Android无线连接打印机并打印的第三方开发方案都非常的困难。由于最近项目需要用到这一块,经过我的组员的努力,找到了一种解决方案,为了能够和大家分享一下,也为了自己以后的参考,在这里稍作总结一下。经验有限,希望有更好方案的可以不吝赐教,我也会在以后的学习中不断修缮自己的方案。

        为了便于大家的参考,本文涉及到的所有的相关工程文件都在本人的csdn下载部分有相关资料下载。

       1.背景:

               很多开发者在工作上或者学习中可能需要通过基于Android系统的手机或者平板终端连接打印机,并驱动打

       印机打印自己发送的图片或者文本。本篇博客主要致力于解决这个问题。

       2.方案:

               本方案需要在android系统中安装一个软件send2print,在我的csdn下载频道中有相应的中文破解版下载链

        接,然后再写自己的程序,来调用其接口进行打印。事先需要在send2print中配置好默认打印机,我们写的程序

        就是调用其默认打印机进行无线打印。

       3.send2print:(我的csdn下载:http://download.csdn.net/detail/kunlong0909/4514502

                这个软件可以安装直接使用,可以搜索开着无线的打印机,并连接进行打印,目前只有国外有卖该产品,

        国内网上可以下载汉化版,我的csdn下载中也有。汉化版有一定的缺陷,经过测试,佳能产品的大部分型号

        都无法实现打印,但是HP LaserJet 1536dnf MFP可以实现打印。

        4.代码实现:(我的csdn工程源码下载:http://download.csdn.net/detail/kunlong0909/4514570

               我也不想贴代码,但是,安装好之后,配置上默认打印机,你只需要两个类就可以轻松实现无线打印了,

        由于代码量不大,也很容易看懂,我就直接贴在下面了,如果需要测试打印的资源文件可以到我csdn下载频道

        下载,这该死的博客怎么没有直接上传资源功能,还是我没有发现啊,谁发现了告诉我一声啊,嘿嘿。

                PrintUtils.java

               

[html]  view plain copy
  1. package com.rcreations.testprint;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.OutputStream;  
  7.   
  8. import android.app.Activity;  
  9. import android.app.AlertDialog;  
  10. import android.content.Context;  
  11. import android.content.DialogInterface;  
  12. import android.content.Intent;  
  13. import android.content.pm.PackageInfo;  
  14. import android.content.pm.PackageManager;  
  15. import android.graphics.Bitmap;  
  16. import android.graphics.Picture;  
  17. import android.net.Uri;  
  18. import android.os.Environment;  
  19. import android.util.Log;  
  20.   
  21.   
  22.   
  23. public class PrintUtils  
  24. {  
  25.     // for logging  
  26.     private static final String TAG = PrintUtils.class.getSimpleName();  
  27.   
  28.     // Send 2 Printer package name  
  29.     private static final String PACKAGE_NAME = "com.rcreations.send2printer";  
  30.       
  31.     // intent action to trigger printing  
  32.     public static final String PRINT_ACTION = "com.rcreations.send2printer.print";  
  33.   
  34.     // content provider for accessing images on local sdcard from within html content  
  35.     // sample img src shoul be something like "content://s2p_localfile/sdcard/logo.gif"  
  36.     public static final String LOCAL_SDCARD_CONTENT_PROVIDER_PREFIX = "content://s2p_localfile";  
  37.   
  38.       
  39.     /**  
  40.      * Returns true if "Send 2 Printer" is installed.   
  41.      */  
  42.     public static boolean isSend2PrinterInstalled( Context context )  
  43.     {  
  44.         boolean output = false;  
  45.         PackageManager pm = context.getPackageManager();  
  46.         try {   
  47.             PackageInfo pi = pm.getPackageInfo( PACKAGE_NAME, 0 );  
  48.             if( pi != null )  
  49.             {  
  50.                 output = true;  
  51.             }  
  52.         } catch (PackageManager.NameNotFoundException e) {}  
  53.         return output;  
  54.     }  
  55.       
  56.       
  57.     /**  
  58.      * Launches the Android Market page for installing "Send 2 Printer"  
  59.      * and calls "finish()" on the given activity.  
  60.      */  
  61.     public static void launchMarketPageForSend2Printer( final Activity context )  
  62.     {  
  63.         AlertDialog dlg = new AlertDialog.Builder( context )  
  64.         .setTitle("Install Send 2 Printer")  
  65.         .setMessage("Before you can print to a network printer, you need to install Send 2 Printer from the Android Market.")  
  66.         .setPositiveButton( android.R.string.ok, new DialogInterface.OnClickListener() {  
  67.             @Override  
  68.             public void onClick( DialogInterface dialog, int which )  
  69.             {  
  70.                 // launch browser  
  71.                 Uri data = Uri.parse( "http://market.android.com/search?q=pname:" + PACKAGE_NAME );  
  72.                 Intent intent = new Intent( android.content.Intent.ACTION_VIEW, data );  
  73.                 intent.setFlags( Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP );  
  74.                 context.startActivity( intent );  
  75.                   
  76.                 // exit  
  77.                 context.finish();  
  78.             }  
  79.         } )  
  80.         .show();      
  81.     }  
  82.       
  83.       
  84.     /**  
  85.      * Save the given picture (contains canvas draw commands) to a file for printing.  
  86.      */  
  87.     public static File saveCanvasPictureToTempFile( Picture picture )  
  88.     {  
  89.         File tempFile = null;  
  90.                   
  91.         // save to temporary file  
  92.         File dir = getTempDir();  
  93.         if( dir != null )  
  94.         {  
  95.             FileOutputStream fos = null;  
  96.             try  
  97.             {  
  98.                 File f = File.createTempFile( "picture", ".stream", dir );  
  99.                 fos = new FileOutputStream( f );  
  100.                 picture.writeToStream( fos );  
  101.                 tempFile = f;  
  102.             }  
  103.             catch( IOException e )  
  104.             {  
  105.                 Log.e( TAG, "failed to save picture", e );  
  106.             }  
  107.             finally  
  108.             {  
  109.                 close( fos );  
  110.             }  
  111.         }         
  112.           
  113.         return tempFile;  
  114.     }  
  115.       
  116.       
  117.     /**  
  118.      * Sends the given picture file (returned from {@link #saveCanvasPictureToTempFile}) for printing.  
  119.      */  
  120.     public static boolean queuePictureStreamForPrinting( Context context, File f )  
  121.     {  
  122.         // send to print activity  
  123.         Uri uri = Uri.fromFile( f );  
  124.         Intent i = new Intent( PRINT_ACTION );  
  125.         i.setDataAndType( uri, "application/x-android-picture-stream" );  
  126.         i.putExtra( "scaleFitToPage", true );  
  127.         context.startActivity( i );  
  128.           
  129.         return true;  
  130.     }  
  131.       
  132.       
  133.     /**  
  134.      * Save the given Bitmap to a file for printing.  
  135.      * Note: Bitmap can be result of canvas draw commands.  
  136.      */  
  137.     public static File saveBitmapToTempFile( Bitmap b, Bitmap.CompressFormat format )  
  138.     throws IOException, UnknownFormatException  
  139.     {  
  140.         File tempFile = null;  
  141.                   
  142.         // save to temporary file  
  143.         File dir = getTempDir();  
  144.         if( dir != null )  
  145.         {  
  146.             FileOutputStream fos = null;  
  147.             try  
  148.             {  
  149.                 String strExt = null;  
  150.                 switch( format )  
  151.                 {  
  152.                     case PNG:  
  153.                         strExt = ".pngx";  
  154.                         break;  
  155.                           
  156.                     case JPEG:  
  157.                         strExt = ".jpgx";  
  158.                         break;  
  159.                           
  160.                     default:  
  161.                         throw new UnknownFormatException( "unknown format: " + format );  
  162.                 }  
  163.                 File f = File.createTempFile( "bitmap", strExt, dir );  
  164.                 fos = new FileOutputStream( f );  
  165.                 b.compress( format, 100, fos );  
  166.                 tempFile = f;  
  167.             }  
  168.             finally  
  169.             {  
  170.                 close( fos );  
  171.             }  
  172.         }         
  173.           
  174.         return tempFile;  
  175.     }  
  176.       
  177.       
  178.     /**  
  179.      * Sends the given image file for printing.  
  180.      */  
  181.     public static boolean queueBitmapForPrinting( Context context, File f, Bitmap.CompressFormat format )  
  182.     throws UnknownFormatException  
  183.     {  
  184.         String strMimeType = null;  
  185.         switch( format )  
  186.         {  
  187.             case PNG:  
  188.                 strMimeType = "image/png";  
  189.                 break;  
  190.                   
  191.             case JPEG:  
  192.                 strMimeType = "image/jpeg";  
  193.                 break;  
  194.                   
  195.             default:  
  196.                 throw new UnknownFormatException( "unknown format: " + format );  
  197.         }  
  198.           
  199.         // send to print activity  
  200.         Uri uri = Uri.fromFile( f );  
  201.         Intent i = new Intent( PRINT_ACTION );  
  202.         i.setDataAndType( uri, strMimeType );  
  203.         i.putExtra( "scaleFitToPage", true );  
  204.         i.putExtra( "deleteAfterPrint", true );  
  205.         context.startActivity( i );  
  206.           
  207.         return true;  
  208.     }  
  209.       
  210.   
  211.     /**  
  212.      * Sends the given text for printing.  
  213.      */  
  214.     public static boolean queueTextForPrinting( Context context, String strContent )  
  215.     {  
  216.         // send to print activity  
  217.         Intent i = new Intent( PRINT_ACTION );  
  218.         i.setType( "text/plain" );  
  219.         i.putExtra( Intent.EXTRA_TEXT, strContent );  
  220.         context.startActivity( i );  
  221.           
  222.         return true;  
  223.     }  
  224.           
  225.       
  226.     /**  
  227.      * Save the given text to a file for printing.  
  228.      */  
  229.     public static File saveTextToTempFile( String text )  
  230.     throws IOException  
  231.     {  
  232.         File tempFile = null;  
  233.                   
  234.         // save to temporary file  
  235.         File dir = getTempDir();  
  236.         if( dir != null )  
  237.         {  
  238.             FileOutputStream fos = null;  
  239.             try  
  240.             {  
  241.                 File f = File.createTempFile( "text", ".txt", dir );  
  242.                 fos = new FileOutputStream( f );  
  243.                 fos.write( text.getBytes() );  
  244.                 tempFile = f;  
  245.             }  
  246.             finally  
  247.             {  
  248.                 close( fos );  
  249.             }  
  250.         }         
  251.           
  252.         return tempFile;  
  253.     }  
  254.       
  255.       
  256.     /**  
  257.      * Sends the given text file for printing.  
  258.      */  
  259.     public static boolean queueTextFileForPrinting( Context context, File f )  
  260.     {  
  261.         // send to print activity  
  262.         Uri uri = Uri.fromFile( f );  
  263.         Intent i = new Intent( PRINT_ACTION );  
  264.         i.setDataAndType( uri, "text/plain" );  
  265.         i.putExtra( "deleteAfterPrint", true );  
  266.         context.startActivity( i );  
  267.           
  268.         return true;  
  269.     }  
  270.       
  271.       
  272.     /**  
  273.      * Sends the given html for printing.  
  274.      *   
  275.      * You can also reference a local image on your sdcard using the "content://s2p_localfile" provider.  
  276.      * For example: <img src="content://s2p_localfile/sdcard/logo.gif">  
  277.      */  
  278.     public static boolean queueHtmlForPrinting( Context context, String strContent )  
  279.     {  
  280.         // send to print activity  
  281.         Intent i = new Intent( PRINT_ACTION );  
  282.         i.setType( "text/html" );  
  283.         i.putExtra( Intent.EXTRA_TEXT, strContent );  
  284.         context.startActivity( i );  
  285.           
  286.         return true;  
  287.     }  
  288.           
  289.       
  290.     /**  
  291.      * Sends the given html URL for printing.  
  292.      *   
  293.      * You can also reference a local file on your sdcard using the "content://s2p_localfile" provider.  
  294.      * For example: "content://s2p_localfile/sdcard/test.html"  
  295.      */  
  296.     public static boolean queueHtmlUrlForPrinting( Context context, String strUrl )  
  297.     {  
  298.         // send to print activity  
  299.         Intent i = new Intent( PRINT_ACTION );  
  300.         //i.setDataAndType( Uri.parse(strUrl), "text/html" );// this crashes!  
  301.         i.setType( "text/html" );  
  302.         i.putExtra( Intent.EXTRA_TEXT, strUrl );  
  303.         context.startActivity( i );  
  304.           
  305.         return true;  
  306.     }  
  307.           
  308.       
  309.     /**  
  310.      * Save the given html content to a file for printing.  
  311.      */  
  312.     public static File saveHtmlToTempFile( String html )  
  313.     throws IOException  
  314.     {  
  315.         File tempFile = null;  
  316.                   
  317.         // save to temporary file  
  318.         File dir = getTempDir();  
  319.         if( dir != null )  
  320.         {  
  321.             FileOutputStream fos = null;  
  322.             try  
  323.             {  
  324.                 File f = File.createTempFile( "html", ".html", dir );  
  325.                 fos = new FileOutputStream( f );  
  326.                 fos.write( html.getBytes() );  
  327.                 tempFile = f;  
  328.             }  
  329.             finally  
  330.             {  
  331.                 close( fos );  
  332.             }  
  333.         }         
  334.           
  335.         return tempFile;  
  336.     }  
  337.       
  338.       
  339.     /**  
  340.      * Sends the given html file for printing.  
  341.      */  
  342.     public static boolean queueHtmlFileForPrinting( Context context, File f )  
  343.     {  
  344.         // send to print activity  
  345.         Uri uri = Uri.fromFile( f );  
  346.         Intent i = new Intent( PRINT_ACTION );  
  347.         i.setDataAndType( uri, "text/html" );  
  348.         i.putExtra( "deleteAfterPrint", true );  
  349.         context.startActivity( i );  
  350.           
  351.         return true;  
  352.     }  
  353.       
  354.       
  355.     /**  
  356.      * Return a temporary directory on the sdcard where files can be saved for printing.  
  357.      * @return null if temporary directory could not be created.  
  358.      */  
  359.     public static File getTempDir()  
  360.     {  
  361.         File dir = new File( Environment.getExternalStorageDirectory(), "temp" );  
  362.         if( dir.exists() == false && dir.mkdirs() == false )  
  363.         {  
  364.             Log.e( TAG, "failed to get/create temp directory" );  
  365.             return null;  
  366.         }  
  367.         return dir;  
  368.     }  
  369.           
  370.       
  371.     /**  
  372.      * Helper method to close given output stream ignoring any exceptions.  
  373.      */  
  374.     public static void close( OutputStream os )  
  375.     {  
  376.         if( os != null )  
  377.         {  
  378.             try  
  379.             {  
  380.                 os.close();  
  381.             }  
  382.             catch( IOException e ) {}  
  383.         }  
  384.     }  
  385.       
  386.   
  387.     /**  
  388.      * Thrown by bitmap methods where the given Bitmap.CompressFormat value is unknown.  
  389.      */  
  390.     public static class UnknownFormatException extends Exception  
  391.     {  
  392.         public UnknownFormatException( String msg )  
  393.         {  
  394.             super( msg );  
  395.         }  
  396.     }  
  397. }  


             以上是工具类,下面的是测试类:

         MainActivity.java

       

[java]  view plain copy
  1. package com.rcreations.testprint;  
  2.   
  3. import java.io.File;  
  4. import android.app.Activity;  
  5. import android.graphics.Bitmap;  
  6. import android.graphics.BitmapFactory;  
  7. import android.graphics.Canvas;  
  8. import android.graphics.Paint;  
  9. import android.graphics.Picture;  
  10. import android.graphics.Rect;  
  11. import android.graphics.Typeface;  
  12. import android.os.Bundle;  
  13. import android.util.Log;  
  14. import android.view.View;  
  15. import android.widget.Button;  
  16.   
  17. /** 
  18.  * Various print samples. 
  19.  */  
  20. public class MainActivity extends Activity  
  21. {  
  22.     private static final String TAG = MainActivity.class.getSimpleName();  
  23.       
  24.     static final String HELLO_WORLD = "Hello World";  
  25.       
  26.       
  27.     /** 
  28.      * Called when the activity is first created. 
  29.      */  
  30.     @Override  
  31.     public void onCreate(Bundle savedInstanceState) {  
  32.         super.onCreate(savedInstanceState);  
  33.         setContentView(R.layout.main);  
  34.   
  35.         //  
  36.         // check for and install Send 2 Printer if needed  
  37.         //  
  38.           
  39.         if( PrintUtils.isSend2PrinterInstalled(this) == false )  
  40.         {  
  41.             PrintUtils.launchMarketPageForSend2Printer( this );  
  42.             return;  
  43.         }  
  44.           
  45.         //  
  46.         // setup GUI buttons  
  47.         //  
  48.           
  49.         Button btnTestCanvas = (Button)findViewById( R.id.btnTestCanvas );  
  50.         btnTestCanvas.setOnClickListener( new View.OnClickListener() {  
  51.             @Override  
  52.             public void onClick(View v)  
  53.             {  
  54.                 printCanvasExample();  
  55.             }  
  56.         });  
  57.           
  58.         Button btnTestCanvasAsBitmap = (Button)findViewById( R.id.btnTestCanvasAsBitmap );  
  59.         btnTestCanvasAsBitmap.setOnClickListener( new View.OnClickListener() {  
  60.             @Override  
  61.             public void onClick(View v)  
  62.             {  
  63.                 printCanvasAsBitmapExample();  
  64.             }  
  65.         });  
  66.   
  67.         Button btnTestText = (Button)findViewById( R.id.btnTestText );  
  68.         btnTestText.setOnClickListener( new View.OnClickListener() {  
  69.             @Override  
  70.             public void onClick(View v)  
  71.             {  
  72.                 printTextExample();  
  73.             }  
  74.         });  
  75.   
  76.         Button btnTestHtml = (Button)findViewById( R.id.btnTestHtml );  
  77.         btnTestHtml.setOnClickListener( new View.OnClickListener() {  
  78.             @Override  
  79.             public void onClick(View v)  
  80.             {  
  81.                 printHtmlExample();  
  82.             }  
  83.         });  
  84.           
  85.         Button btnTestHtmlUrl = (Button)findViewById( R.id.btnTestHtmlUrl );  
  86.         btnTestHtmlUrl.setOnClickListener( new View.OnClickListener() {  
  87.             @Override  
  88.             public void onClick(View v)  
  89.             {  
  90.                 printHtmlUrlExample();  
  91.             }  
  92.         });  
  93.           
  94.         Button btnTestTextFile = (Button)findViewById( R.id.btnTestTextFile );  
  95.         btnTestTextFile.setOnClickListener( new View.OnClickListener() {  
  96.             @Override  
  97.             public void onClick(View v)  
  98.             {  
  99.                 printTextFileExample();  
  100.             }  
  101.         });  
  102.   
  103.         Button btnTestHtmlFile = (Button)findViewById( R.id.btnTestHtmlFile );  
  104.         btnTestHtmlFile.setOnClickListener( new View.OnClickListener() {  
  105.             @Override  
  106.             public void onClick(View v)  
  107.             {  
  108.                 printHtmlFileExample();  
  109.             }  
  110.         });  
  111.     }  
  112.       
  113.    
  114.     /** 
  115.      * Send canvas draw commands for printing. 
  116.      * NOTE: Android 1.5 does not properly support drawBitmap() serialize/deserialize across process boundaries. 
  117.      * If you need to draw bitmaps, then see the {@link #printCanvasAsBitmapExample()} example.  
  118.      */  
  119.     void printCanvasExample()  
  120.     {  
  121.         // create canvas to render on  
  122.         Picture picture = new Picture();  
  123.         Canvas c = picture.beginRecording( 240240 );  
  124.           
  125.         // fill background with WHITE  
  126.         c.drawRGB( 0xFF0xFF0xFF );  
  127.           
  128.         // draw text  
  129.         Paint p = new Paint();  
  130.         Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);  
  131.         p.setTextSize( 18 );  
  132.         p.setTypeface( font );  
  133.         p.setAntiAlias(true);         
  134.         Rect textBounds = new Rect();  
  135.         p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );  
  136.         int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;  
  137.         int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;  
  138.         c.drawText( HELLO_WORLD, x, y, p );  
  139.           
  140.         // draw icon  
  141.         Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );  
  142.         c.drawBitmap( icon, 00null );  
  143.   
  144.         // stop drawing  
  145.         picture.endRecording();  
  146.           
  147.         // queue canvas for printing  
  148.         File f = PrintUtils.saveCanvasPictureToTempFile( picture );  
  149.         if( f != null )  
  150.         {  
  151.             PrintUtils.queuePictureStreamForPrinting( this, f );  
  152.         }  
  153.     }  
  154.       
  155.       
  156.     /** 
  157.      * Draw to a bitmap and then send the bitmap for printing. 
  158.      */  
  159.     void printCanvasAsBitmapExample()  
  160.     {  
  161.         // create canvas to render on  
  162.         Bitmap b = Bitmap.createBitmap( 240240, Bitmap.Config.RGB_565 );  
  163.         Canvas c = new Canvas( b );  
  164.           
  165.         // fill background with WHITE  
  166.         c.drawRGB( 0xFF0xFF0xFF );  
  167.           
  168.         // draw text  
  169.         Paint p = new Paint();  
  170.         Typeface font = Typeface.create(Typeface.SANS_SERIF, Typeface.BOLD);  
  171.         p.setTextSize( 18 );  
  172.         p.setTypeface( font );  
  173.         p.setAntiAlias(true);         
  174.         Rect textBounds = new Rect();  
  175.         p.getTextBounds( HELLO_WORLD, 0, HELLO_WORLD.length(), textBounds );  
  176.         int x = (c.getWidth() - (textBounds.right-textBounds.left)) / 2;  
  177.         int y = (c.getHeight() - (textBounds.bottom-textBounds.top)) / 2;  
  178.         c.drawText( HELLO_WORLD, x, y, p );  
  179.           
  180.         // draw icon  
  181.         Bitmap icon = BitmapFactory.decodeResource( getResources(), R.drawable.icon );  
  182.         c.drawBitmap( icon, 00null );  
  183.   
  184.         // queue bitmap for printing  
  185.         try  
  186.         {  
  187.             File f = PrintUtils.saveBitmapToTempFile( b, Bitmap.CompressFormat.PNG );  
  188.             if( f != null )  
  189.             {  
  190.                 PrintUtils.queueBitmapForPrinting( this, f, Bitmap.CompressFormat.PNG );  
  191.             }  
  192.         }  
  193.         catch( Exception e )  
  194.         {  
  195.             Log.e( TAG, "failed to save/queue bitmap", e );  
  196.         }  
  197.     }  
  198.       
  199.       
  200.     /** 
  201.      * Send text for printing. 
  202.      */  
  203.     void printTextExample()  
  204.     {  
  205.         PrintUtils.queueTextForPrinting( this, HELLO_WORLD );  
  206.     }  
  207.       
  208.       
  209.     /** 
  210.      * Send html for printing. 
  211.      */  
  212.     void printHtmlExample()  
  213.     {  
  214.         StringBuilder buf = new StringBuilder();  
  215.         buf.append( "<html>" );  
  216.         buf.append( "<body>" );  
  217.         buf.append( "<h1>" ).append( HELLO_WORLD ).append( "</h1>" );             
  218.         buf.append( "<p>" ).append( "blah blah blah..." ).append( "</p>" );    
  219.         buf.append( "<p><img src=\"http://www.google.com/intl/en_ALL/images/logo.gif\" /></p>" );  
  220.         // you can also reference a local image on your sdcard using the "content://s2p_localfile" provider (see below)   
  221.         //buf.append( "<p><img src=\"content://s2p_localfile/sdcard/logo.gif\" /></p>" );  
  222.         buf.append( "</body>" );            
  223.         buf.append( "</html>" );  
  224.   
  225.         PrintUtils.queueHtmlForPrinting( this, buf.toString() );  
  226.     }  
  227.       
  228.       
  229.     /** 
  230.      * Send html URL for printing. 
  231.      */  
  232.     void printHtmlUrlExample()  
  233.     {  
  234.         PrintUtils.queueHtmlUrlForPrinting( this"http://www.google.com" );  
  235.     }  
  236.       
  237.       
  238.     /** 
  239.      * Send text file for printing. 
  240.      */  
  241.     void printTextFileExample()  
  242.     {  
  243.         try  
  244.         {  
  245.             File f = PrintUtils.saveTextToTempFile( HELLO_WORLD );  
  246.             if( f != null )  
  247.             {  
  248.                 PrintUtils.queueTextFileForPrinting( this, f );  
  249.             }  
  250.         }  
  251.         catch( Exception e )  
  252.         {  
  253.             Log.e( TAG, "failed to save/queue text", e );  
  254.         }  
  255.     }  
  256.       
  257.       
  258.     /** 
  259.      * Send html file for printing. 
  260.      */  
  261.     void printHtmlFileExample()  
  262.     {  
  263.         try  
  264.         {  
  265.             StringBuilder buf = new StringBuilder();  
  266.             buf.append( "<html>" );  
  267.             buf.append( "<body>" );  
  268.             buf.append( "<h1>" ).append( HELLO_WORLD ).append( "</h1>" );             
  269.             buf.append( "<p>" ).append( "blah blah blah..." ).append( "</p>" );    
  270.             buf.append( "<p><img src=\"http://www.google.com/intl/en_ALL/images/logo.gif\" /></p>" );  
  271.             // you can also reference a local image on your sdcard using the "content://s2p_localfile" provider (see below)   
  272.             //buf.append( "<p><img src=\"content://s2p_localfile/sdcard/logo.gif\" /></p>" );  
  273.             buf.append( "</body>" );            
  274.             buf.append( "</html>" );  
  275.               
  276.             File f = PrintUtils.saveHtmlToTempFile( buf.toString() );  
  277.             if( f != null )  
  278.             {  
  279.                 PrintUtils.queueHtmlFileForPrinting( this, f );  
  280.             }  
  281.         }  
  282.         catch( Exception e )  
  283.         {  
  284.             Log.e( TAG, "failed to save/queue html", e );  
  285.         }  
  286.     }  
  287.       
  288.       
  289. }  


        其实整体上的逻辑很简单,连接无线打印机和驱动打印都是通过send2printer来实现的,我们只是需要调用它,将我们需要打印的信息发送给它就行,so easy!

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Go语言(也称为Golang)是由Google开发的一种静态强类型、编译型的编程语言。它旨在成为一门简单、高效、安全和并发的编程语言,特别适用于构建高性能的服务器和分布式系统。以下是Go语言的一些主要特点和优势: 简洁性:Go语言的语法简单直观,易于学习和使用。它避免了复杂的语法特性,如继承、重载等,转而采用组合和接口来实现代码的复用和扩展。 高性能:Go语言具有出色的性能,可以媲美C和C++。它使用静态类型系统和编译型语言的优势,能够生成高效的机器码。 并发性:Go语言内置了对并发的支持,通过轻量级的goroutine和channel机制,可以轻松实现并发编程。这使得Go语言在构建高性能的服务器和分布式系统时具有天然的优势。 安全性:Go语言具有强大的类型系统和内存管理机制,能够减少运行时错误和内存泄漏等问题。它还支持编译时检查,可以在编译阶段就发现潜在的问题。 标准库:Go语言的标准库非常丰富,包含了大量的实用功能和工具,如网络编程、文件操作、加密解密等。这使得开发者可以更加专注于业务逻辑的实现,而无需花费太多时间在底层功能的实现上。 跨平台:Go语言支持多种操作系统和平台,包括Windows、Linux、macOS等。它使用统一的构建系统(如Go Modules),可以轻松地跨平台编译和运行代码。 开源和社区支持:Go语言是开源的,具有庞大的社区支持和丰富的资源。开发者可以通过社区获取帮助、分享经验和学习资料。 总之,Go语言是一种简单、高效、安全、并发的编程语言,特别适用于构建高性能的服务器和分布式系统。如果你正在寻找一种易于学习和使用的编程语言,并且需要处理大量的并发请求和数据,那么Go语言可能是一个不错的选择。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值