android-Multi-threaded breakpoint download detailed and source download (iv)

Source: Internet
Author: User

The completion of this project is similar to the functions of the download tool such as Thunderbolt, which realizes multi-threaded breakpoint download.
The main design technologies are:
1, the main thread of Android and non-main thread communication mechanism.
2, multi-threaded programming and management.
3, Android Network programming
4, design the implementation of their own design mode-listener mode
5, Activity, Service, database programming
6. android File system
7. Cache

Blog Link:
android-Multi-threaded breakpoint download detailed and source download (a)
android-Multi-threaded breakpoint download detailed and source download (ii)
android-Multi-threaded breakpoint download detailed and source download (c)

This article ends the multi-threaded breakpoint Download, continues the above three article content.
The contents of the activity interface are all given in the above three articles, and the mainactivity code is given below:

 Public  class mainactivity extends Activity {    PrivateEditText Loaderpath;PrivateButton Start,stop;PrivateProgressBar ProgressBar;PrivateTextView Progressnum;Private Final intThreadnum =3;PrivateTask task;Private Static Final intPROGRESS =0;Private Static Final intFAILURE =1;PrivateMultithreadmanager Manager;@SuppressLint("Handlerleak")PrivateHandler Handler =NewHandler () {@Override         Public void Handlemessage(Message msg) {Super. Handlemessage (msg);Switch(msg.what) { CasePROGRESS://Float prog = (float) progressbar.getprogress ()/(float) Progressbar.getmax ();                floatProg1 = ((float) Manager.getdownedlen ()/(float) Manager.getfilelen ()) * -; Progressbar.setprogress ((int) PROG1); Progressnum.settext ((int) (PROG1) +"%"+ -);if(Prog1 >= -) {Toast.maketext (mainactivity. This,"Download Complete", Toast.length_short). Show (); } Break;default: Break; }        }    };@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Requestwindowfeature (Window.feature_no_title);        Setcontentview (R.layout.activity_main);        Init ();    Setlistener (); }Private void Setlistener() {Start.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v) {if(Environment.getexternalstoragestate (). Equals (environment.media_mounted))                    {File Savedir = Environment.getexternalstoragedirectory (); Task =NewTask (Savedir, Loaderpath.gettext (). toString ());NewThread (Task). Start (); }Else{Toast.maketext (mainactivity). This,"no SD card", Toast.length_short). Show (); } start.setclickable (false); Stop.setclickable (true);        }        }); Stop.setonclicklistener (NewOnclicklistener () {@Override             Public void OnClick(View v)                {exit (); Start.setclickable (true); Stop.setclickable (true);    }        }); }Private void Init() {Loaderpath = (EditText) Findviewbyid (R.id.path);        Start = (Button) Findviewbyid (R.id.start_loader);        Stop = (Button) Findviewbyid (R.id.stop_loader);        ProgressBar = (ProgressBar) Findviewbyid (R.id.progressbar);        Progressnum = (TextView) Findviewbyid (r.id.progress_num); Progressbar.setmax ( -); }Private void Exit(){if(Task! =NULL) {task.exit (); }    }Private  class Task implements Runnable{        PrivateString path;PrivateFile Savedir; Public Task(File savedir,string Path) { This. Savedir = Savedir; This. Path = path; System.out.println ("MainActivity.Task.Task () Download path ="+ This. path); } Public void Exit(){if(Manager! =NULL) {Manager.setexist (true); }        }@Override         Public void Run() {Try{manager =NewMultithreadmanager (threadnum, Path, Savedir, mainactivity. This); Progressbar.setmax ( -); Manager.downloader (NewIdownprogressing () {@Override                     Public void Setdownloadernum(intSize) {Message msg =NewMessage ();                        Msg.what = PROGRESS;                        Msg.obj = size;                    Handler.sendmessage (msg);            }                }); }Catch(Exception e)                {E.printstacktrace ();            Handler.sendmessage (Handler.obtainmessage (FAILURE)); }        }    }@Override    protected void OnDestroy() {Super. OnDestroy (); This. exit (); }@Override     Public void onbackpressed() {Super. onbackpressed ();    Finish (); }}

There is nothing to be specific about the main interface, and the comment section is more detailed. The layout file is also very simple:

<Relativelayout xmlns:android="Http://schemas.android.com/apk/res/android"    Xmlns:tools="Http://schemas.android.com/tools"    Android:layout_width="Match_parent"    Android:layout_height="Match_parent"    Android:paddingbottom="@dimen/activity_vertical_margin"    Android:paddingleft="@dimen/activity_horizontal_margin"    Android:paddingright="@dimen/activity_horizontal_margin"    Android:paddingtop="@dimen/activity_vertical_margin"               Tools:context="Com.yin.multithreaddownloader.activity.MainActivity">    <edittext  android:id  = "@+id/path"  android:layout_width  =< Span class= "Hljs-value" > "match_parent"  android:layout_height  = "wrap_content"  android:text  =" Http://192.168.2.63:8080/ds/my.mp3 "/>     <linearlayout  android:id  = "@+id/button"  android:layout_width  =         "match_parent"  android:layout_height  = "wrap_content"  android:layout_below  = "@id/path"  android:orientation  =;         <button  android:id= "@+id/start_loader"  android:layout_width   = "wrap_content"  android:layout_height  = "wrap_content"  android:text  =" start Download " android:textsize  =" 16SP " />         <buttonandroid:id="@+id/stop_loader"android:layout_width="Wrap_ Content "android:layout_height=" Wrap_content "android:text=" Stop Download "  Android:textsize="16sp" />                                                                 </linearlayout>    <progressbar  android:id< /span>= "@+id/progressbar"  style  = android:layout_ Width  = "match_parent"  android:layout_height< /span>= "wrap_content"  android:layout_below  =  "@+id/button"  android:layout_centerhorizontal  = "true"  Span class= "Hljs-attribute" >android:layout_margintop  = "15DP" />     <TextViewandroid:id= "@+id/progress_num"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:layout_below=" @id /progressbar "android:layout_alignparentright=" true "android:layout_margintop ="10DP"/>                                                 </relativelayout>

Here to note is Http://192.168.2.63:8080/ds/my.mp3 this value, this value is the server's address, need to be based on their own configuration to change, so can not be copied!

Summarize:
Multi-threaded breakpoint continuation download involves most of the knowledge points and difficulties of Android application development. In particular, multi-threaded management, network requests, file management, database programming, etc. are very important content. In addition, if you are interested in server development, or want to understand the server development knowledge can learn the SSH framework, of course, there are many server framework, not necessarily to use SSH, but I personally think that SSH is relatively simple, especially the JSP servlet content, need to reach a more familiar degree.
Also for this project, it is worth reminding that the appropriate permissions need to be configured in Androidmanifest.xml:

<uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Voluminous wrote four, do not know whether to say that understand, we take a look, if there is anything wrong place, I hope that we actively correct, mutual exchange study! Handshake

Blog Link:
android-Multi-threaded breakpoint download detailed and source download (a)
android-Multi-threaded breakpoint download detailed and source download (ii)
android-Multi-threaded breakpoint download detailed and source download (c)

Source download (server side and client code)

android-Multi-threaded breakpoint download detailed and source download (iv)

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.