AsyncTask异步线程下载

来源:互联网 发布:英文图纸翻译软件 编辑:程序博客网 时间:2024/05/23 01:46

异步线程下载


MainActivity

package com.example.he.toby.asynctaskdemoactivity;import android.os.AsyncTask;import android.os.Environment;import android.preference.PreferenceActivity;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.ProgressBar;import android.widget.Toast;import org.apache.http.Header;import org.apache.http.HttpResponse;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicHeader;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.io.RandomAccessFile;import java.net.MalformedURLException;public class MainActivity extends AppCompatActivity {    private ProgressBar progressBar;    //下载路径    private String downloadPath = Environment.getExternalStorageDirectory().getPath() + File.separator + "AAAdownload";    private DownloadAsyncTask downloadTask;    private String tag = getClass().getSimpleName();    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_async_task_demo);        progressBar = (ProgressBar) findViewById(R.id.progressbar);        //开始下载        findViewById(R.id.begin).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                /**                 * 一个AsyncTask只能被执行一次,否则会抛异常                 * java.lang.IllegalStateException: Cannot execute task: the task is already running.                 * 如果要重新开始任务的话要重新创建AsyncTask对象                 */                if (downloadTask != null && !downloadTask.isCancelled()) {                    return;                }                downloadTask = new DownloadAsyncTask("http://192.168.11.39:8080/Tracer/GigasetTracers1.0.apk");                downloadTask.execute();                Log.e(tag,"begin......");            }        });        //暂停下载        findViewById(R.id.end).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if (downloadTask != null && downloadTask.getStatus() == AsyncTask.Status.RUNNING) {                    downloadTask.cancel(true);                }            }        });    }    /**     * 下载的AsyncTask     */    private class DownloadAsyncTask extends AsyncTask<String, Integer, Long> {        private static final String TAG = "DownloadAsyncTask";        private String mUrl;        public DownloadAsyncTask(String url) {            this.mUrl = url;        }        @Override        protected Long doInBackground(String... params) {            Log.i(TAG, "downloading");            if(mUrl == null) {                return null;            }            HttpClient client = new DefaultHttpClient();            HttpGet request = new HttpGet(mUrl);            HttpResponse response = null;            InputStream is = null;            RandomAccessFile fos = null;            OutputStream output = null;            Log.e(TAG, " HttpClient client");            try {                //创建存储文件夹                File dir = new File(downloadPath);                          if(dir.exists()) {                    dir.mkdir();                    Log.e(tag, "dir exist -->" + dir.exists());                }                //本地文件                File file = new File(downloadPath + File.separator + mUrl.substring(mUrl.lastIndexOf("/") + 1));                if(!file.exists()){                  //  创建文件,然后输出到该文件中                    file.createNewFile();                    Log.e(tag,"file exist -->"+file.exists());                    //创建文件输出流                    output = new FileOutputStream(file);                    //获取下载输入流                    response = client.execute(request);                    is = response.getEntity().getContent();                    //写入本地                    file.createNewFile();                    byte buffer [] = new byte[1024];                    int inputSize = -1;                    //获取文件总大小,用于计算进度                    long total = response.getEntity().getContentLength();                    int count = 0; //已下载大小                    while((inputSize = is.read(buffer)) != -1) {                        output.write(buffer, 0, inputSize);                        count += inputSize;                        //更新进度                        this.publishProgress((int) ((count / (float) total) * 100));                        Log.e(TAG, "download publishProgress--->");                        //一旦任务被取消则退出循环,否则一直执行,直到结束                        if(isCancelled()) {                            output.flush();                            return null;                        }                    }                    output.flush();                } else {                    long readedSize = file.length(); //文件大小,即已下载大小                    //设置下载的数据位置XX字节到XX字节                Header header_size = new BasicHeader("Range", "bytes=" + readedSize + "-");                    request.addHeader(header_size);                    //执行请求获取下载输入流                    response = client.execute(request);                    is = response.getEntity().getContent();                    //文件总大小=已下载大小+未下载大小                    long total = readedSize + response.getEntity().getContentLength();                    //创建文件输出流                    fos = new RandomAccessFile(file, "rw");                    //从文件的size以后的位置开始写入,其实也不用,直接往后写就可以。有时候多线程下载需要用                    fos.seek(readedSize);                    //这里用RandomAccessFileFileOutputStream都可以,只是使用FileOutputStream的时候要传入第二哥参数true,表示从后面填充//                    output = new FileOutputStream(file, true);                    byte buffer [] = new byte[1024];                    int inputSize = -1;                    int count = (int)readedSize;                    while((inputSize = is.read(buffer)) != -1) {                        fos.write(buffer, 0, inputSize);//                        output.write(buffer, 0, inputSize);                        count += inputSize;                        this.publishProgress((int) ((count / (float) total) * 100));                        if(isCancelled()) {//                            output.flush();                            return null;                        }                    }//                    output.flush();                }            } catch (MalformedURLException e) {                Log.e(TAG, e.getMessage());            } catch (IOException e) {                Log.e(TAG, e.getMessage());            } finally{                try{                    if(is != null) {                        is.close();                    }                    if(output != null) {                        output.close();                    }                    if(fos != null) {                        fos.close();                    }                } catch(Exception e) {                    e.printStackTrace();                }            }            return null;        }        @Override        protected void onPreExecute() {            Log.e(TAG, "download begin ----->");            Toast.makeText(MainActivity.this, "开始下载", Toast.LENGTH_SHORT).show();            super.onPreExecute();        }        @Override        protected void onProgressUpdate(Integer... values) {            super.onProgressUpdate(values);            Log.e(TAG, "downloading---->  " + values[0]);            //更新界面进度条            progressBar.setProgress(values[0]);        }        @Override        protected void onPostExecute(Long aLong) {            Log.e(TAG, "download success " + aLong);            Toast.makeText(MainActivity.this, "下载结束", Toast.LENGTH_SHORT).show();            super.onPostExecute(aLong);        }    }}

activity_async_task_demo.xml

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".AsyncTaskDemoActivity"    android:orientation="vertical">    <Button        android:id="@+id/begin"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="开始下载"/>    <Button        android:id="@+id/end"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="暂停下载"/>    <ProgressBar        android:id="@+id/progressbar"        style="?android:attr/progressBarStyleHorizontal"        android:layout_width="fill_parent"        android:layout_height="3dp"        android:layout_marginTop="10dp"        android:max="100"        android:indeterminate="false" /></LinearLayout>



0 0
原创粉丝点击