Android实现多线程下载文件的方法

2019-12-10 19:58:34王冬梅

主界面 Acitivity

 

 
  1. public class ThreadDownloadDemo extends Activity {   private TextView downloadurl;  
  2. private EditText downloadnum;   private Button downloadbutton;  
  3. private ProgressBar downloadProgressBar;   private TextView downloadinfo;  
  4. private int downloadedSize = 0;   private int fileSize = 0;  
  5. private long downloadtime;   @Override 
  6. public void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);  
  7. setContentView(R.layout.threaddemo);   downloadurl = (TextView) findViewById(R.id.downloadurl);  
  8. downloadurl.setText("http://www.easck.com/201105110911/AA5CC27CBE34DEB50A194581D1300881/Special_323149/%E8%8D%B7%E5%A1%98%E6%9C%88%E8%89%B2.mp3");   downloadnum = (EditText) findViewById(R.id.downloadnum);  
  9. downloadinfo = (TextView) findViewById(R.id.downloadinfo);   downloadbutton = (Button) findViewById(R.id.downloadbutton);  
  10. downloadProgressBar = (ProgressBar) findViewById(R.id.downloadProgressBar);   downloadProgressBar.setVisibility(View.VISIBLE);  
  11. downloadProgressBar.setMax(100);   downloadProgressBar.setProgress(0);  
  12. downloadbutton.setOnClickListener(new OnClickListener() {   public void onClick(View v) {  
  13. download();   downloadtime = SystemClock.currentThreadTimeMillis();