当前位置:操作系统 > 安卓/Android >>

android单线程,多线程下载

单线程下载很简单,就是开启一个线程去下载资源再进行本地保存;

多线程下载是通过RandomAccessFile(随机文件读写操作类)来设置每个线程读取文件的起始点位置,起始点之间的长度即为该线程需要下载的文件大小

下载开始位置:线程id*每条线程下载的数据长度 = ?

下载结束位置:(线程id+1)*每条线程下载的数据长度-1=?

这样比如文件大小:size,线程数:threads,则每个线程的下载量:size/threads;但是这是整除的情况,如果考虑到不能整除的情况:则前threads-1个线程下载量为size/threads,最后一个线程的下载量:size/threads+size%threads
File_downactivity.java代码 
package file.down; 
 
import java.io.File; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLConnection; 
 
import javax.security.auth.PrivateCredentialPermission; 
 
import android.R.integer; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewDebug.FlagToString; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
 
public class File_downActivity extends Activity { 
    private Button but1,but2; 
    private EditText etUrl; 
    private ProgressBar bar1,bar2; 
    private TextView tv1,tv2; 
    private boolean flag = true; 
    private int temp = 0; 
    private int downLoadedSize = 0; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
        etUrl = (EditText) findViewById(R.id.downloadUrl); 
         
        but1 = (Button) findViewById(R.id.but1); 
        but2 = (Button) findViewById(R.id.but2); 
         
        bar1 = (ProgressBar) findViewById(R.id.downloadProgressBar1); 
        bar2 = (ProgressBar) findViewById(R.id.downloadProgressBar2); 
         
        tv1 = (TextView) findViewById(R.id.tv1); 
        tv2 = (TextView) findViewById(R.id.tv2); 
         
         
        //单线程下载 
        but1.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                if(DownUtil.isExistSdCard()){ 
                    if(DownUtil.checkNet(File_downActivity.this)){ 
                        if(DownUtil.creatDir(DownUtil.path1)){ 
                            thread.start(); 
                        } 
                    } 
                } 
            } 
        }); 
        //多线程下载 
        but2.setOnClickListener(new OnClickListener() { 
             
            @Override 
            public void onClick(View v) { 
                mulThread.start(); 
            } 
        }); 
    } 
    @Override 
    protected void onDestroy() { 
        super.onDestroy(); 
        DownUtil.downLoadSize = 0; 
    } 
    /** 
     * 单线程下载 
     */ 
    private Thread thread = new Thread(new Runnable() { 
         
        @Override 
        public void run() { 
                 
                DownUtil.fileDown1(etUrl.getText().toString(), DownUtil.savePath1, handler); 
                bar1.setMax(DownUtil.fileSize); 
        } 
    }); 
    /** 
     * 多线程下载(主线程类) 
     */ 
    private Thread mulThread = new Thread(new Runnable() { 
        private int blockSize,downLoadSizeMore; 
        private int threadNum = 5;//线程数 
         
        @Override 
        public void run() { 
//          MulDownThread[] threads = new MulDownThread[threadNum]; 
            try { 
                URL url = new URL(etUrl.getText().toString()); 补充:移动开发 , Android ,

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,