android 下载 并显示 progressDialog

在网上看啦一些,自己有修改了一下,主要方法 如下
下载类
package com.example.anrdroidlancher.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.widget.Toast;

/**
 * 下载apk
 * 
 * @author qishoudong
 * @2012-10-11
 */
public class LoadingUtile {

	public Context context;
	public Handler handler;
	/**
	 * 文件一共的大小
	 */
	public static  int fileSize = 0;
	/**
	 * 已经下载的大小
	 */
	public int downloadSize = 0;
	public static int loading_size;
	public static final int DOWNLOAD_PREPARE = 0;
	public static final int DOWNLOAD_WORK = 1;
	public static final int DOWNLOAD_OK = 2;
	public static final int DOWNLOAD_ERROR = 3;

	public LoadingUtile(Context context) {
		this.context = context;
	}

	public LoadingUtile(Context context, Handler handler) {
		this.context = context;
		this.handler = handler;
	}

	public final static String loadingFile = "thtf_apk";
	public static String FILEPATH = null;

	public File downLoadFile(String httpUrl) throws Exception {
		String[] tempStr = httpUrl.split(".");
		final String fileName = "ebank.apk";
		FileHelper fileHelper = new FileHelper(context);
		FILEPATH = fileHelper.sysPath(context) + loadingFile;
		File tmpFile = new File(FILEPATH);
		if (!tmpFile.exists()) {
			tmpFile.mkdir();
		}
		final File file = new File(FILEPATH + "/" + fileName);
		try {
			URL url = new URL(httpUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			InputStream is = conn.getInputStream();
			FileOutputStream fos = new FileOutputStream(file);
			byte[] buf = new byte[256];
			conn.connect();
			fileSize=conn.getContentLength();
			if (fileSize < 1 || is == null) {
				sendMessage(DOWNLOAD_ERROR);
			} else {
				sendMessage(DOWNLOAD_PREPARE);
				byte[] bytes = new byte[1024];
				int len = -1;
				while ((len = is.read(bytes)) != -1) {
					fos.write(bytes, 0, len);
					downloadSize += len;
					loading_size=downloadSize;
					sendMessage(DOWNLOAD_WORK);
				}
				sendMessage(DOWNLOAD_OK);
			}
			conn.disconnect();
			fos.close();
			is.close();
		} catch (IOException e) {
			sendMessage(DOWNLOAD_ERROR);
			e.printStackTrace();
		}
		return file;
	}

	/**
	 * 给hand发送消息
	 * 
	 * @param what
	 */
	private void sendMessage(int what) {
		Message m = new Message();
		m.what = what;
		handler.sendMessage(m);
	}

}


progressDialog  handler 事件 
	/**
	 * handle
	 */
	 Handler handler = new Handler() {
		@Override
		public void handleMessage(Message msg) {// handler接收到消息后就会执行此方法
			switch (msg.what) {
			case LoadingUtile.DOWNLOAD_ERROR:
				progressDialog.setMessage("下载出错");
				progressDialog.dismiss();
				break;
			case LoadingUtile.DOWNLOAD_PREPARE:
//				progressDialog.setMax(LoadingUtile.fileSize);
				break;
			case LoadingUtile.DOWNLOAD_WORK:
				int res = LoadingUtile.loading_size * 100 / LoadingUtile.fileSize;
//				progressDialog.setMessage("已经下载  "+res+"%");
				progressDialog.setProgress(res);
				break;
			case LoadingUtile.DOWNLOAD_OK:
				progressDialog.dismiss();// 关闭ProgressDialog
				openFile(file);
				break;
			default:
				progressDialog.dismiss();// 关闭ProgressDialog
				break;
			}
		}
	};

猜你喜欢

转载自yingzi201172.iteye.com/blog/1698644