天天看点

android多任务同时下载

学习android快两个月了,一直坚持从迷茫中寻找可以得到的尽可能多的东西

想做一个下载功能,当然理想的功能要支持多任务同时下载,断点续传的功能,我想一步一步来,首先困难摆在了多任务这里

开始我的思路是在一个service中启动下载的流操作,然后通过service中声明一个activity中的handler更新ui(比如进度条。。。)

可是我发现在service中声明一个activity中的handler是做不到的(可能只是我做不到吧,无法申请内存)

于是,我决定在activity中直接启动线程,让其运行,调用自身的handler来更新ui,没想到在这个下载activity onpause()的时候,线程还是活的,也就是说可以继续下载,下例是我做的一个两个任务同时下载的小例子,后面会把理想中的功能都陆续添加上的。。。

package onerain.multhreaddownload;

import java.io.file;

import java.io.fileoutputstream;

import java.io.ioexception;

import java.io.inputstream;

import java.io.outputstream;

import java.net.httpurlconnection;

import java.net.malformedurlexception;

import java.net.url;

import onerain.multhreaddownload.downloadservicer.downloadthread;

import android.app.activity;

import android.content.intent;

import android.os.bundle;

import android.os.environment;

import android.os.handler;

import android.os.looper;

import android.os.message;

import android.widget.progressbar;

import android.widget.textview;

public class multhreaddownload extends activity

{

    /** called when the activity is first created. */

    private progressbar pb1 = null;

    private textview tv1 = null;

    private progressbar pb2 = null;

    private textview tv2 = null;

    private string root = environment.getexternalstoragedirectory().getabsolutepath() + file.separator;

    private string downloadfile = "http://192.168.1.5/android/test.zip";

    private string downloadfile1 = "http://192.168.1.5/android/test1.exe";

    //声明已经读过的长度变量

    private int hasread = 0;

    public void oncreate(bundle savedinstancestate)

    {

        super.oncreate(savedinstancestate);

        setcontentview(r.layout.main);

        pb1 = (progressbar)findviewbyid(r.id.progressbar1);

        tv1 = (textview)findviewbyid(r.id.textview1);

        pb2 = (progressbar)findviewbyid(r.id.progressbar2);

        tv2 = (textview)findviewbyid(r.id.textview2);

        download(downloadfile, root, pb1, tv1);

        download(downloadfile1, root, pb2, tv2);

    }

    private void download(string url, string targetpath, progressbar pb, textview tv)

        downloadthread dt = new downloadthread(url, targetpath, pb, tv);

        dt.start();

  //自定义一个handler类,处理线程消息

    public class myhandler extends handler

    {     

        private progressbar progressbar;

        private textview textview;

        //通过构造函数来确定给哪个progressbar刷新

        public myhandler(progressbar progressbar, textview textview)

        {

            this.progressbar = progressbar;

            this.textview = textview;

        }

        //万恶的动词和名词

        public void handlemessage(message msg)

        {       

            this.progressbar.setprogress(msg.arg1);

            this.textview.settext(msg.arg1 + "%");

            super.handlemessage(msg);   

    //下载线程

    public class downloadthread extends thread

        private string url = "";

        private string targetpath = "";

        private int hasdownload = 0;

        private int len = -1;

        private byte buffer[] = new byte[4 * 1024];

        private int size = 0;

        private int rate = 0;

        private myhandler myhandler = null;

        private message msg = null;

        private progressbar pb = null;

        private textview tv = null;

        public downloadthread(string url, string targetpath, progressbar pb, textview tv)

            this.url = url;

            this.targetpath = targetpath;

            this.pb = pb;

            this.tv = tv;

            myhandler = new myhandler(this.pb, this.tv);

        public void run()

            string targetfilename = this.targetpath + this.url.substring(this.url.lastindexof("/")+1, this.url.length());

            file downloadfile = new file(targetfilename);

            if(!downloadfile.exists()) 

            {

                try

                {

                    downloadfile.createnewfile();

                }

                catch (ioexception e)

                    // todo auto-generated catch block

                    e.printstacktrace();

            }

            try

                url fileurl = new url(this.url);

                httpurlconnection conn = (httpurlconnection)fileurl.openconnection();

                //获取文件大小

                size = conn.getcontentlength();

                inputstream is = conn.getinputstream();

                outputstream os = new fileoutputstream(targetfilename);

                while((len=is.read(buffer)) != -1)

                    os.write(buffer);

                    hasdownload += len;

                    rate = (hasdownload*100/size);

                    msg = new message();

                    msg.arg1 = rate;

                    myhandler.sendmessage(msg);

                    system.out.println(rate + "%");

                } 

            catch (malformedurlexception e)

                // todo auto-generated catch block

                e.printstacktrace();

            catch (ioexception e)

            } 

}

继续阅读