六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 80|回复: 0

android 下载 并显示 progressDialog

[复制链接]

升级  56%

8

主题

8

主题

8

主题

童生

Rank: 1

积分
28
 楼主| 发表于 2013-1-30 04:07:31 | 显示全部楼层 |阅读模式
在网上看啦一些,自己有修改了一下,主要方法 如下
下载类
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() {@Overridepublic 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();// 关闭ProgressDialogopenFile(file);break;default:progressDialog.dismiss();// 关闭ProgressDialogbreak;}}};
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表