歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Android開發:從Tomcat上下載MP3 帶百分比進度條

Android開發:從Tomcat上下載MP3 帶百分比進度條

  1. package com.src.fpkj.android;  
  2.   
  3. import com.src.fpkj.android.down.DownFielToSdcard;  
  4. import android.app.Activity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. /**  
  10.  * 實現一個帶進度條的下載dialog顯示百分比,很喜歡這效果,感覺很真切  
  11.  * @author 1314HWL  
  12.  *  2011/10/10/23:01  
  13.  */  
  14. public class MainActivity extends Activity implements OnClickListener {  
  15.     Button btn_downMp3;  
  16.     String httpUrl = "http://10.0.2.2:8080/webdav/missyou.mp3";  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.         btn_downMp3 = (Button) findViewById(R.id.btn_down);  
  21.         btn_downMp3.setOnClickListener(this);  
  22.     }  
  23.   
  24.     public void onClick(View v) {  
  25.         switch (v.getId()) {  
  26.         case R.id.btn_down:  
  27.             DownFielToSdcard filedown = new DownFielToSdcard(MainActivity.this);  
  28.             try {  
  29.                 //httpUrl:tomcat 下載地址 test/sdcard中得路徑      
  30.                   
  31.                 filedown.LoadToSdcard(httpUrl, "test/", "missyou.mp3");  
  32.             } catch (Exception e1) {  
  33.                 e1.printStackTrace();  
  34.             }  
  35.             break;  
  36.         }  
  37.   
  38.     }  
  39. }  
  40.   
  41.   
  42. package com.src.fpkj.android.down;  
  43.   
  44. import java.io.File;  
  45. import java.io.FileOutputStream;  
  46. import java.io.InputStream;  
  47. import java.io.OutputStream;  
  48. import java.net.HttpURLConnection;  
  49. import java.net.URL;  
  50. import com.src.fpkj.android.R;  
  51. import android.app.AlertDialog;  
  52. import android.app.Dialog;  
  53. import android.content.Context;  
  54. import android.os.Environment;  
  55. import android.os.Handler;  
  56. import android.os.Message;  
  57. import android.util.Log;  
  58. import android.view.LayoutInflater;  
  59. import android.view.View;  
  60. import android.widget.ProgressBar;  
  61. import android.widget.TextView;  
  62. import android.widget.Toast;  
  63.   
  64. public class DownFielToSdcard {  
  65.   
  66.     private static String SDPath;  
  67.     ProgressBar pb;  
  68.     TextView tv_percent;  
  69.     int downLoadFileSize, tatalsize;  //downLoadFileSize下載了多少, tatalsize總大小  
  70.     Dialog dialog;  
  71.     Context context;  
  72.   
  73.     public DownFielToSdcard(Context context) {  
  74.         super();  
  75.         this.context = context;  
  76.         SDPath = Environment.getExternalStorageDirectory() + "/";// 得到的是/sdcard/  
  77.     }  
  78.   
  79.     /**  
  80.      * 在sdcard中創建文件  
  81.      *   
  82.      * @param fileName  
  83.      * @return  
  84.      * @throws Exception  
  85.      */  
  86.     public File CreateFile(String fileName) throws Exception {  
  87.         File file = new File(SDPath + fileName);  
  88.         file.createNewFile();  
  89.         return file;  
  90.     }  
  91.   
  92.     /**  
  93.      * 創建目錄  
  94.      *   
  95.      * @param fileName  
  96.      * @return  
  97.      * @throws Exception  
  98.      */  
  99.     public File CreateFileSdDir(String dirName) throws Exception {  
  100.         File sdDir = new File(SDPath + dirName);  
  101.         sdDir.mkdir();  
  102.         return sdDir;  
  103.     }  
  104.   
  105.     /**  
  106.      * 判斷文件是否存在  
  107.      *   
  108.      * @param fileName  
  109.      * @return  
  110.      */  
  111.   
  112.     public boolean FileExist(String fileName) {  
  113.         File file = new File(SDPath + fileName);  
  114.         return file.exists();  
  115.     }  
  116.   
  117.     /**  
  118.      * 思路:要下載文件,先得創建目錄  
  119.      */  
  120.   
  121.     public void LoadToSdcard(final String strUrl, final String path,  
  122.             final String fileName) throws Exception {  
  123.         if (FileExist("test/missyou.mp3")) {  
  124.             Toast.makeText(context, R.string.filehaved, Toast.LENGTH_LONG)  
  125.                     .show();  
  126.         } else {  
  127.   
  128.             View view = LayoutInflater.from(context).inflate(  
  129.                     R.layout.download_dialog_xml, null);  
  130.             pb = (ProgressBar) view.findViewById(R.id.down_pb);  
  131.             tv_percent = (TextView) view.findViewById(R.id.pro_int);  
  132.             dialog = AlertDialogUtil(view, context,  
  133.                     context.getString(R.string.waittingloading));  
  134.             new Thread(new Runnable() {  
  135.                 public void run() {  
  136.                     try {  
  137.                         URL url = new URL(strUrl);  
  138.                         HttpURLConnection conection = (HttpURLConnection) url  
  139.                                 .openConnection();  
  140.                         tatalsize = conection.getContentLength();  
  141.                         InputStream input = conection.getInputStream();  
  142.                         File file = null;  
  143.                         OutputStream outputstream = null;  
  144.                         CreateFileSdDir(path);  
  145.                         file = CreateFile(path + fileName);  
  146.                         outputstream = new FileOutputStream(file);  
  147.                         byte data[] = new byte[1024 * 4];  
  148.                         sentMassage(0);  
  149.                         while (true) {  
  150.                             int temp = input.read(data);  
  151.                             if (temp == -1) {  
  152.                                 break;  
  153.                             }  
  154.                             outputstream.write(data, 0, temp);  
  155.                             downLoadFileSize += temp;  
  156.                             sentMassage(1);  
  157.                         }  
  158.                         sentMassage(2);  
  159.                         outputstream.flush();  
  160.                         outputstream.close();  
  161.                         input.close();  
  162.                     } catch (Exception e) {  
  163.                         Toast.makeText(context, R.string.app_falls,  
  164.                                 Toast.LENGTH_LONG).show();  
  165.                         e.printStackTrace();  
  166.                     }  
  167.                 }  
  168.   
  169.             }).start();  
  170.         }  
  171.     }  
  172.   
  173.     /**  
  174.      * 返回一個dialog  
  175.      *   
  176.      * @param view  
  177.      * @param context  
  178.      * @param string  
  179.      * @return  
  180.      */  
  181.     private Dialog AlertDialogUtil(View view, Context context, String string) {  
  182.         AlertDialog.Builder builder = new AlertDialog.Builder(context);  
  183.         builder.setTitle(string);  
  184.         builder.setIcon(R.drawable.icon);  
  185.         builder.setView(view);  
  186.         builder.create();  
  187.         return builder.show();  
  188.     }  
  189.   
  190.     /**  
  191.      * handler 處理動作  
  192.      */  
  193.     Handler handler = new Handler() {  
  194.         public void handleMessage(Message msg) {  
  195.             super.handleMessage(msg);  
  196.             switch (msg.what) {  
  197.             case 0:  
  198.                 pb.setMax(tatalsize);  
  199.                 break;  
  200.             case 1:  
  201.                 pb.setProgress(downLoadFileSize);  
  202.                 int result = downLoadFileSize * 100 / tatalsize;  
  203.                 tv_percent.setText(context.getString(R.string.fileload)  
  204.                         + result + "%");  
  205.                 break;  
  206.             case 2:  
  207.                 dialog.dismiss();  
  208.                 Toast.makeText(context, R.string.loagsucces, Toast.LENGTH_LONG)  
  209.                         .show();  
  210.                 Log.v("test", "--->>>  " + "end");  
  211.                 break;  
  212.             }  
  213.   
  214.         }  
  215.     };  
  216.   
  217.     /**  
  218.      *   
  219.      * @param flag  
  220.      *            消息類型  
  221.      */  
  222.   
  223.     public void sentMassage(int flag) {  
  224.         Message msg = new Message();  
  225.         msg.what = flag;  
  226.         handler.sendMessage(msg);  
  227.     }  
  228.   
  229. }   
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="hello">Hello World, MainActivity!</string>  
  4.     <string name="app_name">LoadDownFile</string>  
  5.     <string name="loagsucces">下載成功</string>  
  6.     <string name="app_falls">下載失敗</string>  
  7.     <string name="waittingloading">正在下載請稍後……</string>  
  8.     <string name="filehaved">文件已存在</string>  
  9.     <string name="fileload">文件下載</string>  
  10. </resources>  
Copyright © Linux教程網 All Rights Reserved