您的位置:首页 > 移动开发 > Android开发

Android开发 下载显示进度

2011-11-09 10:50 381 查看
package cn.com.hyronjs.activity;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.net.URL;

import java.net.URLConnection;

import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.view.View;

import android.widget.Button;

import android.widget.ProgressBar;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

private ProgressBar progressBar;

private TextView textView;

private Button start;

private int fileSize;

private int downloadFileSize;

private String filename;

private Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

// 定义一个Handler,用于处理下载线程与UI间通讯

if (!Thread.currentThread().isInterrupted()) {

switch (msg.what) {

case 0:

progressBar.setMax(fileSize);

case 1:

progressBar.setProgress(downloadFileSize);

int result = downloadFileSize * 100 / fileSize;

textView.setText(result + "%");

break;

case 2:

Toast.makeText(MainActivity.this, "文件下载完成", 1).show();

break;

}

}

super.handleMessage(msg);

}

};

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

start = (Button) findViewById(R.id.begin);

start.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

down();

}

});

}

private void down(){

final String uri = this.getResources().getString(R.string.uri);

final String sd = this.getResources().getString(R.string.sd);

progressBar = (ProgressBar) findViewById(R.id.progressbar);

textView = (TextView) findViewById(R.id.textview);

new Thread() {

public void run() {

try {

// 下载文件,参数:第一个URL,第二个存放路径

downFile(uri, sd);

} catch (Exception e) {

e.printStackTrace();

}

}

}.start();

}

public void downFile(String url, String path) throws IOException {

// 下载函数

filename = url.substring(url.lastIndexOf("/") + 1);

// 获取文件名

URL myURL = new URL(url);

URLConnection conn = myURL.openConnection();

conn.connect();

InputStream is = conn.getInputStream();

this.fileSize = conn.getContentLength();// 根据响应获取文件大小

if (this.fileSize <= 0)

throw new RuntimeException(" unknow file size ");

if (is == null)

throw new RuntimeException(" inputstream is null ");

FileOutputStream fos = new FileOutputStream(path + filename);

// 把数据存入路径+文件名

byte buf[] = new byte[1024];

downloadFileSize = 0;

sendData(0);

do {

// 循环读取

int numread = is.read(buf);

if (numread == -1) {

break;

}

fos.write(buf, 0, numread);

downloadFileSize += numread;

sendData(1);// 更新进度条

} while (true);

sendData(2);// 通知下载完成

if(is != null){

is.close();

}

if(fos != null){

fos.close();

}

}

private void sendData(int flag) {

Message msg = new Message();

msg.what = flag;

handler.sendMessage(msg);

}

}





内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: