一、概述
說到Android中的文件下載,Android API中明確要求將耗時的操作放到一個子線程中執行,文件的下載無疑是需要耗費時間的,所以要將文件的下載放到子線程中執行。下面,我們一起來實現一個Android中利用多線程下載文件的小例子。
二、服務端準備
在這個小例子中我以下載有道詞典為例,在網上下載有道詞典的安裝包,在eclipse中新建項目web,將下載的有道詞典安裝包放置在WebContent目錄下,并將項目發布到Tomcat中,具體如下圖所示
	
三、Android實現
1、布局
界面上自上而下放置一個TextView,用來提示文本框中輸入的信息,一個文本框用來輸入網絡中下載文件的路徑,一個Button按鈕,點擊下載文件,一個ProgressBar顯示下載進度,一個TextView顯示下載的百分比。具體布局內容如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="下載路徑" /> <EditText android:id="@+id/ed_path" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="http://192.168.0.170:8080/web/youdao.exe"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下載" android:onClick="download"/> <ProgressBar android:id="@+id/pb" android:layout_width="match_parent" android:layout_height="wrap_content" style="@android:style/Widget.ProgressBar.Horizontal"/> <TextView android:id="@+id/tv_info" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="下載:0%"/> </LinearLayout>
2、自定義ProgressBarListener監聽器接口
新建自定義ProgressBarListener監聽器接口,這個接口中定義兩個方法,void getMax(int length)用來獲取下載文件的長度,void getDownload(int length);用來獲取每次下載的長度,這個方法中主要是在多線程中調用,子線程中獲取到的數據傳遞到這兩個接口方法中,然后在這兩個接口方法中通過Handler將相應的長度信息傳遞到主線程,更新界面顯示信息,具體代碼實現如下:
package com.example.inter;  /**  * 自定義進度條監聽器  * @author liuyazhuang  *  */ public interface ProgressBarListener {  /**  * 獲取文件的長度  * @param length  */  void getMax(int length);  /**  * 獲取每次下載的長度  * @param length  */  void getDownload(int length); } 3、自定義線程類DownloadThread
	          這里通過繼承Thread的方式來實現自定義線程操作,在這個類中主要是實現文件的下載操作,在這個類中,定義了一系列與下載有關的實例變量來控制下載的數據,同時通過自定義監聽器ProgressBarListener中的void getDownload(int length)方法來跟新界面顯示的進度信息。
	具體實現如下:
package com.example.download;  import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL;  import com.example.inter.ProgressBarListener;  /**  * 自定義線程類  * @author liuyazhuang  *  */ public class DownloadThread extends Thread {  //下載的線程id  private int threadId;  //下載的文件路徑  private String path;  //保存的文件  private File file;  //下載的進度條更新的監聽器  private ProgressBarListener listener;  //每條線程下載的數據量  private int block;  //下載的開始位置  private int startPosition;  //下載的結束位置  private int endPosition;   public DownloadThread(int threadId, String path, File file, ProgressBarListener listener, int block) {  this.threadId = threadId;  this.path = path;  this.file = file;  this.listener = listener;  this.block = block;    this.startPosition = threadId * block;  this.endPosition = (threadId + 1) * block - 1;  }   @Override  public void run() {  super.run();  try {   //創建RandomAccessFile對象   RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");   //跳轉到開始位置   accessFile.seek(startPosition);   URL url = new URL(path);   //打開http鏈接   HttpURLConnection conn = (HttpURLConnection) url.openConnection();   //設置超時時間   conn.setConnectTimeout(5000);   //指定請求方式為GET方式   conn.setRequestMethod("GET");   //指定下載的位置   conn.setRequestProperty("Range", "bytes="+startPosition + "-" + endPosition);   //不用再去判斷狀態碼是否為200   InputStream in = conn.getInputStream();   byte[] buffer = new byte[1024];   int len = 0;   while((len = in.read(buffer)) != -1){   accessFile.write(buffer, 0, len);   //更新下載進度   listener.getDownload(len);   }   accessFile.close();   in.close();  } catch (Exception e) {   // TODO: handle exception   e.printStackTrace();  }  } } 4、新建DownloadManager類
	         這個類主要是對下載過程的管理,包括下載設置下載后文件要保存的位置,計算多線程中每個線程的數據下載量等等。
	具體實現如下:
package com.example.download;  import java.io.File; import java.io.RandomAccessFile; import java.net.HttpURLConnection; import java.net.URL;  import android.os.Environment;  import com.example.inter.ProgressBarListener;  /**  * 文件下載管理器  * @author liuyazhuang  *  */ public class DownloadManager {  //下載線程的數量  private static final int TREAD_SIZE = 3;  private File file;  /**  * 下載文件的方法  * @param path:下載文件的路徑  * @param listener:自定義的下載文件監聽接口  * @throws Exception  */  public void download(String path, ProgressBarListener listener) throws Exception{  URL url = new URL(path);  HttpURLConnection conn = (HttpURLConnection) url.openConnection();  conn.setConnectTimeout(5000);  conn.setRequestMethod("GET");  if(conn.getResponseCode() == 200){   int filesize = conn.getContentLength();   //設置進度條的最大長度   listener.getMax(filesize);   //創建一個和服務器大小一樣的文件   file = new File(Environment.getExternalStorageDirectory(), this.getFileName(path));   RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");   accessFile.setLength(filesize);   //要關閉RandomAccessFile對象   accessFile.close();     //計算出每條線程下載的數據量   int block = filesize % TREAD_SIZE == 0 ? (filesize / TREAD_SIZE) : (filesize / TREAD_SIZE +1 );     //開啟線程下載   for(int i = 0; i < TREAD_SIZE; i++){   new DownloadThread(i, path, file, listener, block).start();   }  }  }   /**  * 截取路徑中的文件名稱  * @param path:要截取文件名稱的路徑  * @return:截取到的文件名稱  */  private String getFileName(String path){  return path.substring(path.lastIndexOf("/") + 1);  } } 5、完善MainActivity
	      在這個類中首先,找到頁面中的各個控件,實現Button按鈕的onClick事件,在onClick事件中開啟一個線程進行下載操作,同時子線程中獲取到的數據,通過handler與Message機制傳遞到主線程,更新界面顯示。
	具體實現如下:
package com.example.multi;  import android.app.Activity; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast;  import com.example.download.DownloadManager; import com.example.inter.ProgressBarListener;  /**  * MainActivity整個應用程序的入口  * @author liuyazhuang  *  */ public class MainActivity extends Activity {   protected static final int ERROR_DOWNLOAD = 0;  protected static final int SET_PROGRESS_MAX = 1;  protected static final int UPDATE_PROGRESS = 2;   private EditText ed_path;  private ProgressBar pb;  private TextView tv_info;  private DownloadManager manager;  //handler操作  private Handler mHandler = new Handler(){    public void handleMessage(android.os.Message msg) {   switch (msg.what) {   case ERROR_DOWNLOAD:   //提示用戶下載失敗   Toast.makeText(MainActivity.this, "下載失敗", Toast.LENGTH_SHORT).show();   break;   case SET_PROGRESS_MAX:   //得到最大值   int max = (Integer) msg.obj;   //設置進度條的最大值   pb.setMax(max);   break;   case UPDATE_PROGRESS:   //獲取當前下載的長度   int currentprogress = pb.getProgress();   //獲取新下載的長度   int len = (Integer) msg.obj;   //計算當前總下載長度   int crrrentTotalProgress = currentprogress + len;   pb.setProgress(crrrentTotalProgress);      //獲取總大小   int maxProgress = pb.getMax();   //計算百分比   float value = (float)currentprogress / (float)maxProgress;   int percent = (int) (value * 100);   //顯示下載的百分比   tv_info.setText("下載:"+percent+"%");   break;   default:   break;   }  };  };  @Override  protected void onCreate(Bundle savedInstanceState) {  super.onCreate(savedInstanceState);  setContentView(R.layout.activity_main);  this.ed_path = (EditText) super.findViewById(R.id.ed_path);  this.pb = (ProgressBar) super.findViewById(R.id.pb);  this.tv_info = (TextView) super.findViewById(R.id.tv_info);  this.manager = new DownloadManager();    }   @Override  public boolean onCreateOptionsMenu(Menu menu) {  // Inflate the menu; this adds items to the action bar if it is present.  getMenuInflater().inflate(R.menu.main, menu);  return true;  }   public void download(View v){  final String path = ed_path.getText().toString();  //下載  new Thread(new Runnable() {   @Override   public void run() {   // TODO Auto-generated method stub   try {    manager.download(path, new ProgressBarListener() {    @Override    public void getMax(int length) {     // TODO Auto-generated method stub     Message message = new Message();     message.what = SET_PROGRESS_MAX;     message.obj = length;     mHandler.sendMessage(message);    }        @Override    public void getDownload(int length) {     // TODO Auto-generated method stub     Message message = new Message();     message.what = UPDATE_PROGRESS;     message.obj = length;     mHandler.sendMessage(message);    }    });   } catch (Exception e) {    // TODO: handle exception    e.printStackTrace();    Message message = new Message();    message.what = ERROR_DOWNLOAD;    mHandler.sendMessage(message);   }   }  }).start();  } } 6、增加權限
	最后,別忘了給應用授權,這里要用到Android聯網授權和向SD卡中寫入文件的權限。
	具體實現如下:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.multi" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.multi.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
四、運行效果
	
	
	
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答