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

Android下载服务器资源文件(支持断点续传)

2016-01-12 17:27 489 查看
这里我使用的是aFinalHttp框架进行下载处理

public void onclick(View view) {
switch (view.getId()) {
case R.id.download:
String url = "http://gdown.baidu.com/data/wisegame/4ae6d2d7378e6cdf/QQ_122.apk";
String savePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/qq.apk";
File file=new File(savePath);
if (file.exists()) {
file.delete();
}
http=new FinalHttp();
handler = http.download(url, new AjaxParams(),savePath,true,new AjaxCallBack<File>() {
@Override
public void onStart() {
super.onStart();
Toast.makeText(getApplicationContext(),"开始下载!",Toast.LENGTH_SHORT).show();
}

@Override
public void onLoading(long count, long current) {
super.onLoading(count, current);
int pr=0;
if (current!=count&¤t!=0){
pr= (int) (current/(float)count*100);
}else {
pr=100;
}
te.setText("速度"+pr+"%");
progressBar.setProgress(pr);
}

@Override
public void onSuccess(File file) {
super.onSuccess(file);
Toast.makeText(getApplicationContext(),"下载成功!",Toast.LENGTH_SHORT).show();
}

@Override
public void onFailure(Throwable t, int errorNo, String strMsg) {
super.onFailure(t, errorNo, strMsg);
Toast.makeText(getApplicationContext(),"下载失败!",Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.stop:

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