Android multi-thread download tool core code sharing, android download Tool

Source: Internet
Author: User

Android multi-thread download tool core code sharing, android download Tool

First, we will share with you the core multi-thread download class:

Press Ctrl + C to copy the code Press Ctrl + C to copy the code

The following is the logic code of the interface:

1 package com. example. urltest; 2 3 import android. app. activity; 4 import android. app. alertDialog; 5 import android. app. progressDialog; 6 import android. content. dialogInterface; 7 import android. OS. bundle; 8 import android. OS. message; 9 import android. view. view; 10 import android. view. view. onClickListener; 11 import android. widget. button; 12 import android. widget. editText; 13 import android. widget. P RogressBar; 14 import android. widget. textView; 15 import android. widget. toast; 16 17 import java. io. IOException; 18 import java. util. timer; 19 import java. util. timerTask; 20 21 public class MultiThreadDown extends Activity {22 EditText url, target; 23 Button downButton; 24 ProgressBar bar; 25 ProgressDialog progre; 26 View downView; 27 DownUtil downUtil; 28 private int mDownStatus; 29 pri Vate int threadNum = 6; // default thread count 30 android. OS. handler handler = new android. OS. handler () {31 32 @ Override 33 public void handleMessage (Message msg) {34 if (msg. what = 0x123) {35 bar. setProgress (mDownStatus); 36 if (mDownStatus> = 100) {37 Toast. makeText (MultiThreadDown. this, "download completed", Toast. LENGTH_SHORT ). show (); 38} 39 // Log. I ("csx", "" + mDownStatus); 40 41} 42} 43 44}; 45 46 @ Override 47 protected void onCreate (Bundle savedInstanceState) {48 49 super. onCreate (savedInstanceState); 50 setContentView (R. layout. layout_down); 51 url = (EditText) findViewById (R. id. url); 52 53 downButton = (Button) findViewById (R. id. down); 54 bar = (ProgressBar) findViewById (R. id. bar); 55 progressDialog = new ProgressDialog (this); 56 progressDialog. setTitle ("try to connect"); 57 progressDialog. setMessage ("connected. .. "); 58 downButton. setOnClickListener (new DownButtonOnClickListener (); 59 60} 61 62 private class DownButtonOnClickListener implements OnClickListener {63 64 EditText targetFilePath, fileName; 65 TextView fileSize; 66 Thread connectionThread; 67 68 public Thread instanceOfConnectionThread () {69 return new Thread () {70 71 @ Override 72 public void run () {73 try {74 downUtil. getFileInformatio N (); 75 76} catch (IOException e1) {77 e1.printStackTrace (); 78} 79 80} 81 82 }; 83} 84 85 @ Override 86 public void onClick (View v) {87 88 String urlPath = url. getText (). toString (); 89 if (urlPath = null | urlPath. equals ("") {90 return; 91} 92 progressDialog. show (); 93 downUtil = new DownUtil (urlPath, threadNum); 94 connectionThread = instanceOfConnectionThread (); 95 connectionThread. st Art (); 96 97 int connectionNum = 3; 98 while (! DownUtil. isGetFileInformation () & connectionNum> 0) {// loop request connection. if the connection fails after three times, exit 99 if (! ConnectionThread. isAlive () {100 connectionThread = null; 101 connectionThread = instanceOfConnectionThread (); 102 connectionThread. start (); 103 connectionNum --; 104} 105 106} 107 progressDialog. cancel (); 109 if (! DownUtil. isGetFileInformation () {110 Toast. makeText (MultiThreadDown. this, "request failed! ", Toast. LENGTH_SHORT ). show (); 111 return; 112} 113 downView = getLayoutInflater (). inflate (R. layout. layout_download_view, null); 114 targetFilePath = (EditText) downView. findViewById (R. id. editText_target_path); 115 fileName = (EditText) downView. findViewById (R. id. editText_file_name); 116 fileSize = (TextView) downView. findViewById (R. id. textView_file_size); 117 targetFilePath. setText (downUtil. getDefaultTa RgetPath (); 118 fileName. setText (downUtil. getFileName (); 119 fileSize. append ("" + (double) downUtil. getFileSize ()/1024 + "k"); 120 121 new AlertDialog. builder (MultiThreadDown. this ). setView (downView) 122. setPositiveButton ("OK", new DialogInterface. onClickListener () {123 124 @ Override125 public void onClick (DialogInterface dialog, int which) {126 if (! DownUtil. isGetFileInformation () {127 dialog. dismiss (); 128 return; 129} 130 final String path = targetFilePath. getText (). toString (); 131 final String name = fileName. getText (). toString (); 132 133 new Thread () {134 135 @ Override136 public void run () {137 try {138 downUtil. download (path, name); 139} catch (IOException e) {140 // TODO Auto-generated catch block141 e. printStackTrace (); 142} 143 144 final T Extends timer = new Timer (); 145 TimerTask task = new TimerTask () {146 147 @ Override148 public void run () {149 150 mDownStatus = (int) (downUtil. getCompleteRate () * 100); 151 handler. sendEmptyMessage (0x123); 152 if (mDownStatus> = 100) {153 timer. cancel (); 154} 155 156} 157}; 158 timer. schedule (task, 0,100); 159 160} 161 162 }. start (); 163 164} 165 }). setNegativeButton ("cancel", null) 166. setTitle (downUtil. is GetFileInformation ()? "Link available": "link unavailable"). show (); 167 168} 169} 170 171}

Layout_down.xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <ScrollView 8 android: id = "@ + id/scrollView1" 9 android: layout_width = "match_parent" 10 android: layout_height = "wrap_content"> 11 12 <LinearLayout13 android: layout_width = "match_parent" 14 android: layout_height = "matc H_parent "15 android: orientation =" vertical "> 16 17 <TextView18 android: layout_width =" match_parent "19 android: layout_height =" wrap_content "20 android: text = "URL of the resource to be downloaded:"/> 21 22 <EditText23 android: id = "@ + id/url" 24 android: layout_width = "match_parent" 25 android: layout_height = "wrap_content" 26 android: text = "Enter URL here"/> 27 28 <Button29 android: id = "@ + id/down" 30 android: layout_width = "match_parent" 31 android: Layout_height = "wrap_content" 32 android: text = "Download"/> 33 <! -- Define a horizontal progress bar to display the download progress --> 34 35 <ProgressBar36 android: id = "@ + id/bar" 37 style = "? Android: attr/progressBarStyleHorizontal "38 android: layout_width =" match_parent "39 android: layout_height =" wrap_content "40 android: max = "100"/> 41 </LinearLayout> 42 </ScrollView> 43 44 </LinearLayout>

Below is the layout of the download option dialog: layout_download_view.xml

1 <? Xml version = "1.0" encoding = "UTF-8"?> 2 <LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android" 3 android: layout_width = "match_parent" 4 android: layout_height = "match_parent" 5 android: orientation = "vertical"> 6 7 <TextView 8 android: id = "@ + id/textView1" 9 android: layout_width = "wrap_content" 10 android: layout_height = "wrap_content" 11 android: text = "Download path:"/> 12 13 <EditText14 android: id = "@ + id/editText_target_path" 15 android: layout_width = "match_parent" 16 android: layout_height = "wrap_content" 17 android: EMS = "10"> 18 19 <requestFocus/> 20 </EditText> 21 22 <TextView23 android: id = "@ + id/textView2" 24 android: layout_width = "wrap_content" 25 android: layout_height = "wrap_content" 26 android: text = "File Name: "/> 27 28 <EditText29 android: id =" @ + id/editText_file_name "30 android: layout_width =" match_parent "31 android: layout_height =" wrap_content "32 android: maxLines = "1" 33 android: EMS = "10"/> 34 35 <TextView36 android: id = "@ + id/textView_file_size" 37 android: layout_width = "wrap_content" 38 android: layout_height = "wrap_content" 39 android: text = "file size:"/> 40 41 </LinearLayout>

Enter the URL and click Download. In the dialog box that appears, enter the path and file name and click OK to start download.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.