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;
}
}
};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值