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);
}
}
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);
}
}
相关文章推荐
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android开发之文件下载,状态时显示下载进度,点击自动安装
- Android开发 下载显示进度
- android开发教程教您用于下载文件和显示现在进度的线程类
- Android开发之文件下载(通知栏中显示进度条,状态信息等)
- Android 下载模块,使用Xutils3 的下载功能,Activity和services数据交互,listview显示下载进度
- Android开发模板------WebView加载时显示ProgressBar进度条
- Android 下载文件至SD卡,并用progressBar显示下载进度
- Android 下载文件通知栏显示进度条功能的实例代码
- android通过service实现更新并显示下载进度条,最后自动安装实例
- android多线程断点下载-带进度条和百分比进度显示
- Android 下载网络url文件并显示进度
- 【android】自定义ProgressDialog实现暂时隐藏进度值并显示等待状态(附源码下载)
- Android使用AsyncTask下载图片并显示进度条功能
- android-下载保存网络图片并显示下载进度条
- Android 下载文件 显示进度条
- iOS开发之网络编程--4、NSURLSessionDataTask实现文件下载(离线断点续传下载) <进度值显示优化>
- Android中AsyncTask进行后台下载文件并在下拉菜单显示下载进度
- android异步下载图片并显示水平进度条