处理多线程,断点续传,下载进度条等问题,xUtils框架

来源:互联网 发布:pk10北京赛车开奖源码 编辑:程序博客网 时间:2024/05/29 02:28
注意事项,第一注意你的文件是否是已经下载过的,否则会出现,无法下载的情况
                第二注意看一下你的下载地址是否正确
                第三,非常重要,注意权限问题,这个框架的使用需要1、网络权限,2、SD卡的写入权限




publicclassMainActivityextendsActivity {

   privateTextViewtv_failure;
   privateTextViewtv_progress;
     privateProgressBarpb;

     @Override
   protectedvoidonCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       tv_failure= (TextView) findViewById(R.id.tv_failure);
       tv_progress= (TextView) findViewById(R.id.tv_progress);
       pb= (ProgressBar) findViewById(R.id.pb);
    }

   publicvoidclick(View v){
     HttpUtils utils =newHttpUtils();
        String fileName ="TakeColor.exe";
        //确定下载地址
         String path ="http://192.168.6.136:8080/"+ fileName;
     utils.download(path,//下载地址
              "sdcard/TakeColor.exe",//文件保存路径
              true,//是否支持断点续传
              true,//是否下载支持重命名
              newRequestCallBack<File>() {
                        //下载成功后调用
                        @Override
                        publicvoidonSuccess(ResponseInfo<File> arg0) {
                             //TODOAuto-generated method stub
                             Toast.makeText(MainActivity.this, arg0.result.getPath(), 0).show();
                        }
                        //下载失败后调用
                        @Override
                        publicvoidonFailure(HttpException arg0, String arg1) {
                             //TODOAuto-generated method stub
                             tv_failure.setText(arg1);
                        }
                        
                        @Override
                        publicvoidonLoading(longtotal,longcurrent,
                                  booleanisUploading) {
                             //TODOAuto-generated method stub
                             super.onLoading(total, current, isUploading);
                             pb.setMax((int)total);
                             pb.setProgress((int)current);
                             
                             
                        }
                        
                        
                        
                   });
    }
}
0 0
原创粉丝点击