AsyncTask实现下载任务

AsyncTask实现下载任务

基本思路是
1 先做好xml文件的编写
2编写类继承AsyncTask类 实现方法
3 在doinbackground里编写下载任务 +文件操作
4 更新progressbar

public class MainActivity extends AppCompatActivity {
    public static final int ZERO = 0;
    public static final String URL = "http://download.sj.qq.com/upload/connAssitantDownload/upload/MobileAssistant_1.apk";
    TextView textView;
    Button button;
    ProgressBar progressBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        initData();
    }

    private void initData() {
        button.setText(R.string.clicldownloading);
        textView.setText(R.string.downloading_ok);
        progressBar.setProgress(ZERO);


    }

    private void initEvent() {
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                DownLoad downLoad=new DownLoad();
                downLoad.execute(URL);

            }
        });
    }

    private void initView() {
         textView=findViewById(R.id.tv_download);
         button=findViewById(R.id.button_download);
         progressBar=findViewById(R.id.progressBar_download);
    }

class DownLoad extends AsyncTask<String,Integer,Boolean>{

        public static final String IMOOC_APK = "imooc.apk";
        private Boolean resout;
        private String mfilepath;

        @Override
        protected void onPostExecute(Boolean aBoolean) {
            super.onPostExecute(aBoolean);
            textView.setText(aBoolean?mfilepath+"下载完成":"下载失败");
            button.setText("下载完成");
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            if (values!=null&&values.length>0) {
                progressBar.setProgress(values[0]);
            }
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Boolean doInBackground(String... strings) {
            if (strings!=null&&strings.length>0){
                String apkurl=strings[0];
                try {
                    //创建url类
                    URL url=new URL(apkurl);
                    //创建连接且打开
                    URLConnection URLConnection=  url.openConnection();
                    //
                    InputStream inputStream=URLConnection.getInputStream();
                    //获取下载总长度
                    int contentlength=URLConnection.getContentLength();
                    //获取文件下载地址
                    mfilepath = Environment.getExternalStorageState()+ File.separator+ IMOOC_APK;
                    File apkFile=new File(mfilepath);
                    if (apkFile.exists()){
                        resout = apkFile.delete();
                        if (!resout){
                            return false;
                        }
                    }
                    //已下载的进度
                    int downloadsize=0;
                    byte[] bytes=new byte[1024];
                    int lenth;
                    //创建一个输出管道
                    OutputStream outputStream=new FileOutputStream(mfilepath);
                    //不断的一车一车去挖土 直到挖没了

                    while ((lenth=inputStream.read(bytes))!=-1){
                        //从o写到length 挖到的放到小车里
                        outputStream.write(bytes,0,lenth);
                        //累加我们的大小
                        downloadsize+=lenth;
                        //发送进度
                        publishProgress(downloadsize*100/contentlength);
                    }inputStream.close();
                    outputStream.close();
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else {
                return false;
            }
            return true;
        }
    }
发布了14 篇原创文章 · 获赞 5 · 访问量 459

猜你喜欢

转载自blog.csdn.net/Professional_6/article/details/101049809