Multi‑thread breakpoint download implementation in Android 2. Use the open-source framework XUtils and Android xutils
The use of open-source frameworks can greatly reduce the difficulty of development, reduce the development cycle, and have fewer bugs, making the software more stable to run.
Introduction to xUtils
XUtils contains many practical android tools.
XUtils supports large file uploads, more comprehensive http request protocol support (10 predicates), more flexible ORM, more event annotations supported and not affected by obfuscation...
Least compatible with android 2.2 (api level 8)
: Https://github.com/wyouflf/xUtils
The following is a demo:
Public class MainActivity extends Activity {private EditText et_path; private TextView TV _info; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); et_path = (EditText) findViewById (R. id. et_path); TV _info = (TextView) findViewById (R. id. TV _info);} public void download (View view) {String path = et_path.getText (). ToString (). trim (); if (TextUtils. isEmpty (path) {Toast. makeText (this, "Enter the download path", 0 ). show (); return;} else {HttpUtils http = new HttpUtils (); HttpHandler handler = http. download (path, "/sdcard/xxx.zip", true, // if the target file exists, download the unfinished part. When the server does not support RANGE, it will be downloaded again. True, // if the file name is obtained from the returned information of the request, it is automatically renamed after the download is complete. New RequestCallBack <File> () {@ Override public void onStart () {TV _info.setText ("conn... ") ;}@ Override public void onLoading (long total, long current, boolean isUploading) {TV _info.setText (current +"/"+ total );} @ Override public void onSuccess (ResponseInfo <File> responseInfo) {TV _info.setText ("downloaded:" + responseInfo. result. getPath () ;}@ Override public void onFailure (HttpException error, String msg) {TV _info.setText (msg );}});}}}
The use of open-source frameworks is far more beneficial!