当前位置: 编程技术>移动开发
Android文件下载进度条的实现代码
来源: 互联网 发布时间:2014-10-13
本文导语: main.xml: 代码如下:main.java: 代码如下:package com.pocketdigi.download;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;import org.apache.http.client.ClientProtocolException;import android.app.Activit...
main.xml:
main.java:
package com.pocketdigi.download;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
ProgressBar pb;
TextView tv;
int fileSize;
int downLoadFileSize;
String fileEx,fileNa,filename;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{//定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted())
{
switch (msg.what)
{
case 0:
pb.setMax(fileSize);
case 1:
pb.setProgress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
tv.setText(result + "%");
break;
case 2:
Toast.makeText(main.this, "文件下载完成", 1).show();
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(main.this, error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb=(ProgressBar)findViewById(R.id.down_pb);
tv=(TextView)findViewById(R.id.tv);
new Thread(){
public void run(){
try {
down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");
//下载文件,参数:第一个URL,第二个存放路径
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public void down_file(String url,String path) throws IOException{
//下载函数
filename=url.substring(url.lastIndexOf("/") + 1);
//获取文件名
URL myURL = new URL(/tech-mobile-dev/url/index.html);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();//根据响应获取文件大小
if (this.fileSize
代码如下:
main.java:
代码如下:
package com.pocketdigi.download;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import org.apache.http.client.ClientProtocolException;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
ProgressBar pb;
TextView tv;
int fileSize;
int downLoadFileSize;
String fileEx,fileNa,filename;
private Handler handler = new Handler()
{
@Override
public void handleMessage(Message msg)
{//定义一个Handler,用于处理下载线程与UI间通讯
if (!Thread.currentThread().isInterrupted())
{
switch (msg.what)
{
case 0:
pb.setMax(fileSize);
case 1:
pb.setProgress(downLoadFileSize);
int result = downLoadFileSize * 100 / fileSize;
tv.setText(result + "%");
break;
case 2:
Toast.makeText(main.this, "文件下载完成", 1).show();
break;
case -1:
String error = msg.getData().getString("error");
Toast.makeText(main.this, error, 1).show();
break;
}
}
super.handleMessage(msg);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pb=(ProgressBar)findViewById(R.id.down_pb);
tv=(TextView)findViewById(R.id.tv);
new Thread(){
public void run(){
try {
down_file("http://wallpaper.pocketdigi.com/upload/1/bigImage/1284565196.jpg","/sdcard/");
//下载文件,参数:第一个URL,第二个存放路径
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.start();
}
public void down_file(String url,String path) throws IOException{
//下载函数
filename=url.substring(url.lastIndexOf("/") + 1);
//获取文件名
URL myURL = new URL(/tech-mobile-dev/url/index.html);
URLConnection conn = myURL.openConnection();
conn.connect();
InputStream is = conn.getInputStream();
this.fileSize = conn.getContentLength();//根据响应获取文件大小
if (this.fileSize