Android Development--the principle of breakpoint continuation and its implementation
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong><span style="color:rgb(153, 51, 0);">0.</span> <span style="color:rgb(153, 51, 0);">Preface</span></strong></p></p><p><p><span style="color:#555555;">in the</span> <span style="color:#555555;">Android</span> <span style="color:#555555;">in development, the breakpoint continues to sound very easy, when downloading a file, click on the Pause task pause, Click Start will continue to download the File. But the real realization of the knowledge point is still quite a lot, so there is time today to achieve a bit, and to Record. This article original, reproduced please indicate the source for Seu_calvin Blog. </span></p></p><p><p><span style="color:#555555;"></span></p></p><p><p><strong><span style="color:rgb(153, 51, 0);">1.</span> The principle of the continuation of the <span style="color:rgb(153, 51, 0);">breakpoint</span></strong></p></p><p><p><span style="color:#555555;">to use the database during the local download process</span> where <span style="color:red;">real-time storage is stored to a file</span> , <span style="color:#555555;">so that when you click Start to continue delivery, you can</span> <span style="color:#555555;">HTTP</span> <span style="color:#555555;">of the</span> <span style="color:#555555;">GET</span> <span style="color:#555555;">in the request</span> <span style="color:red;">Setrequestproperty ()</span> the <span style="color:#555555;">method can tell the server where the data begins and where it ends. </span><span style="color:red;">randomaccessfile</span> <span style="color:red;">'s</span> <span style="color:red;">seek ()</span> <span style="color:#555555;">method also supports write operations at any location in the file while local file writes are in Progress. Also</span> <span style="color:#ff0000;">broadcast</span> The progress of the <span style="color:#555555;">child thread</span> to the <span style="color:#555555;">Activity</span> <span style="color:#555555;">'s</span> <span style="color:#555555;">processbar</span><span style="color:#555555;">. </span></p></p><p><p><strong><span style="color:rgb(153, 51, 0);"></span></strong></p></p><p><p><strong><span style="color:rgb(153, 51, 0);">2. Activity</span> <span style="color:rgb(153, 51, 0);">'s button response</span></strong></p></p><p><p><span style="color:#555555;">When you click the start button, the</span> <span style="color:#555555;">URL</span> <span style="color:#555555;">written in the</span> <span style="color:#555555;">FileInfo</span> <span style="color:#555555;">object of the class</span> <span style="color:#555555;">Info</span> <span style="color:#555555;">in and through</span> <span style="color:#555555;">Intent</span> <span style="color:#555555;">from</span> <span style="color:#555555;">Activity</span> <span style="color:#555555;">passed to the</span> <span style="color:#555555;">Service</span> <span style="color:#555555;">The. here, use</span> <span style="color:#555555;">setaction ()</span> <span style="color:#555555;">to distinguish between a start button or a pause Button. </span></p></p><p><p></p></p><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_1_7517706" name="code" class="java" style="color: rgb(85, 85, 85);"><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_1_7517706" name="code" class="java" style="color: rgb(85, 85, 85);">public class FileInfo implements serializable{ private String url;//url private int length;//length or end position private int start; Start position private int now;//current progress//constructor method, set/get slightly}//start button logic, Stop logic is roughly the same strat.setonclicklistener (new View.onclicklistener () { @Override public void OnClick (view view) { Intent Intent = new Intent ( mainactivity.this,downloadservice.class); Intent.setaction (downloadservice.action_start); Intent.putextra ("fileUrl", info); StartService (intent); }});</pre></pre><span style="color: rgb(153, 51, 0); "><span style="color: rgb(153, 51, 0); "><strong></strong></span></span><p><p></p></p><p><p><span style="color:#555555;"><span style=" color: rgb(153, 51, 0);"><strong><br></strong></span></span></p></p><p style="color: rgb(85, 85, 85);"><p style="color:rgb (" ) "," <strong> <span style="color:rgb (153, wuyi, 0);">3. </span> <span style=" Color:rgb (153, 51, 0); "> in </span> <span style="color:rgb (153, wuyi, 0);">service </span> <span style="color:rgb (153, 51, 0);" get file size in child threads < span></span></p></p><p><p><span style="color: rgb(85, 85, 85);">in the</span> <span style="color: rgb(85, 85, 85);">Service</span> <span style="color: rgb(85, 85, 85);">in the</span> <span style="color:#ff0000;">Onstartcommand ()</span> <span style="color: rgb(85, 85, 85);">, you will</span> <span style="color: rgb(85, 85, 85);">FileInfo</span> <span style="color: rgb(85, 85, 85);">object from</span> <span style="color: rgb(85, 85, 85);">Intent</span> <span style="color: rgb(85, 85, 85);">If it is the start command, then</span> <span style="color:#ff0000;">open a thread to get the size of the file to download according to the URL</span> <span style="color: rgb(85, 85, 85);">, writes the size to the object and passes the</span> <span style="color: rgb(85, 85, 85);">Handler</span> <span style="color: rgb(85, 85, 85);">Pass Back</span> <span style="color: rgb(85, 85, 85);">Service</span> <span style="color: rgb(85, 85, 85);">, and a local file of the same size is created Locally. The pause command will be discussed at the End. </span></p></p><span style="color: rgb(153, 51, 0); "><span style="color: rgb(153, 51, 0); "><strong></strong></span></span><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_2_9405080" name="code" class="java"> public void Run () {httpurlconnection urlconnection = null; Randomaccessfile Randomfile = null; Try {url url = new URL (fileinfo.geturl ()); URLConnection = (httpurlconnection) url.openconnection (); Urlconnection.setconnecttimeout (3000); Urlconnection.setrequestmethod ("GET"); int length =-1; If (urlconnection.getresponsecode () = = Httpstatus.sc_ok) {//get file length = Urlconn Ection.getcontentlength (); } if (length <= 0) {return; }//create local files of the same size file dir = new file (download_path); If (!dir.exists ()) {dir.mkdir (); } File File = new file (dir, file_name); Randomfile = new Randomaccessfile (file, "rwd"); Randomfile.setlength (length); Length to FileInfo object Fileinfo.setlength (length); Pass the object to service Mhandle.obtainmessage (0, FileInfo) via Handler. sendtotarget (); } catch (Exception E) {e.printstacktrace (); } finally {//stream recycle logic}}}</pre><br><p><p><strong><span style="color:rgb(153, 51, 0);">4.</span> <span style="color:rgb(153, 51, 0);">database operation Encapsulation</span></strong></p></p><p><p><span style="color:#555555;">in the</span> <span style="color:#555555;">Service</span> <span style="color:#555555;">of the</span> <span style="color:#555555;">handlemessage ()</span> <span style="color:#555555;">method to get a</span> <span style="color:#555555;">length</span> <span style="color:#555555;">property of</span> <span style="color:#555555;">FileInfo</span> <span style="color:#555555;">object, and use the custom</span> <span style="color:#555555;">Downloadutil</span> <span style="color:#555555;">class for specific file download Logic. The context is passed in because the database processing operation needs to be used. </span></p></p><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_3_8227008" name="code" class="java"><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_3_8227008" name="code" class="java">Downloadutil = new Downloadutil (downloadservice.this,info);d ownloadutil.download ();</pre></pre><p><p> <span style="color: #555555;"> Here's an interface for database operations </span> <span style="color: #555555;">threaddao </span> <span style=" Color: #555555; ">, There are </span> <span style="color:red" , "" "," "and other logical < span> <span style=" color: #555555; " to record the download task information. customize a < span> <span style="color: #555555;">threaddaoimpl </span> <span style="color: #555555;" the class will implement the logic here, internal database creation about inheriting < span> <span style="color: #555555;">sqliteopenhelper </span> <span style="color: #555555;"> The logic of the custom class is not pasted, it is relatively simple, the class will be in the </span> <span style="color: #555555;">threaddaoimpl </span> <span style="color: #555555;" creates an instance in the constructor method of a class. < span> <span style="color:red;"> Complete encapsulation of the underlying database operation </span> <span style="color: #555555;" . < span></span></span></span></span></span></p></p><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_4_3352985" name="code" class="java"><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_4_3352985" name="code" class="java">Public interface Threaddao { //insert a data public void Insert (FileInfo info); Deletes a data public void Delete (String url) based on the URL ; Update a progress public void update based on the URL (String url,int finished); Finds a data public list<fileinfo> get (String url) based on the URL ; Whether there is a public boolean isexits (String url);}</pre></pre><br><p><p><strong><span style="color:rgb(153, 51, 0);">5.</span> <span style="color:rgb(153, 51, 0);">specific file download logic</span></strong></p></p><pre code_snippet_id="2061552" snippet_file_name="blog_20161219_5_1510605" name="code" class="java">public class Downloadutil {//constructor method slightly public void download () {list<fileinfo> lists = Threaddao.get (filein Fo.geturl ()); FileInfo info = null; If (lists.size () = = 0) {//first download, create sub-thread download new MyThread (fileInfo). start (); }else{//middle Start info = lists.get (0); New MyThread (info). Start (); }} class MyThread extends thread{private FileInfo info = null; Public MyThread (FileInfo Threadinfo) {this.info = threadinfo; } @Override public void run () {//add thread information to the database if (!threaddao.isexits (info.geturl ())) { Threaddao.insert (info); } httpurlconnection URLConnection = null; Randomaccessfile Randomfile =null; InputStream InputStream = null; Try {url url = new URL (info.geturl ()); URLConnection = (httpurlconnection) url.openconnection (); Urlconnection.setconnecttimeout (3000); Urlconnection.setrequestmethod ("GET"); Set download location int start = Info.getstart () + info.getnow (); Urlconnection.setrequestproperty ("Range", "bytes=" + start + "-" + info.getlength ()); Sets the file write location of the files = new file (download_path,file_name); Randomfile = new Randomaccessfile (file, "rwd"); Randomfile.seek (start); Broadcast to activity Intent Intent = new Intent (action_update); Finished + = Info.getnow (); If (urlconnection.getresponsecode () = = Httpstatus.sc_partial_content) {//get file stream Inpu TStream = Urlconnection.getinputstream (); byte[] buffer = new byte[512]; int Len =-1; Long time = System.currenttimemillis (); While (len = Inputstream.read (buffer))! =-1) { Write File Randomfile.write (buffer,0,len); Send progress to activity finished + = len; See time interval, interval greater than 500ms if (system.currenttimemillis ()-time >500) {tim E = System.currenttimemillis (); Intent.putextra ("now", finished *100/fileinfo.getlength ()); Context.sendbroadcast (intent); }//determine if it is a paused state if (ispause) {threaddao.update (info. GETURL (), finished); Return End Loop}}//delete Thread Information Threaddao.delete (info. GETURL ()); }}catch (Exception E) {e.printstacktrace (); }finally {//recovery Work slightly}}}}</pre><p><p><span style="color:#555555;">The above also mentions the use of custom</span> <span style="color:#555555;">Downloadutil</span> <span style="color:#555555;">class to</span> <span style="color:red;">specific file download logic</span> <span style="color:#555555;">, which is also the most critical part, in the construction method of the class</span> <span style="color:#555555;">Threaddaoimpl</span> <span style="color:#555555;">the creation of the Instance. And in</span> <span style="color:#555555;">download ()</span> <span style="color:#555555;">through the operation of the database query,</span> <span style="color:red;">Determine whether it is the first time to start the download task</span> <span style="color:#555555;">, if it is, then open a child thread</span> <span style="color:#555555;">MyThread </span> <span style="color:#555555;">Perform the download task, otherwise take the progress information out of the database and pass that information to</span> <span style="color:#555555;">MyThread</span><span style="color:#555555;">. </span></p></p><p><span style="color:#555555;"><span style="color:#555555;">in the</span></span><span style="color:#555555;"><span style="color:#555555;">MyThread</span></span><span style="color:#555555;"><span style="color:#555555;">in which</span></span><span style="color:red;"><span style="color:red;">through</span></span><span style="color:red;"><span style="color:red;">Info.getstart () + info.getnow ()</span></span><span style="color:red;"><span style="color:red;">set where to start the download</span></span><span style="color:#555555;"><span style="color:#555555;">, if it is the first time to download two numbers will be</span></span><span style="color:#555555;"><span style="color:#555555;">0</span></span><span style="color:#555555;"><span style="color:#555555;">, if it is paused and then downloaded, then</span></span><span style="color:#555555;"><span style="color:#555555;">Info.getnow ()</span></span><span style="color:#555555;"><span style="color:#555555;">will take out non-</span></span><span style="color:#555555;"><span style="color:#555555;">0</span></span><span style="color:#555555;"><span style="color:#555555;">value from the value of the</span></span><span style="color:red;"><span style="color:red;">Database Storage</span></span><span style="color:#555555;"><span style="color:#555555;">. Use</span></span><span style="color:#555555;"><span style="color:#555555;">Setrequestproperty</span></span><span style="color:red;"><span style="color:red;">Tell the server</span></span><span style="color:#555555;"><span style="color:#555555;">where to start passing data, where to end, local use</span></span><span style="color:red;"><span style="color:red;">Randomaccessfile</span></span><span style="color:red;"><span style="color:red;">of the</span></span><span style="color:red;"><span style="color:red;">Seek ()</span></span><span style="color:red;"><span style="color:red;">method for local storage of data</span></span><span style="color:#555555;"><span style="color:#555555;">. Use</span></span><span style="color:red;">the <span style="color:red;">broadcast passes the percentage of progress to</span></span><span style="color:red;"><span style="color:red;">Activity</span></span><span style="color:#555555;"><span style="color:#555555;">,</span></span><span style="color:#555555;"><span style="color:#555555;">Activity</span></span><span style="color:#555555;"><span style="color:#555555;">Change Again</span></span><span style="color:#555555;"><span style="color:#555555;">Processbar</span></span><span style="color:#555555;"><span style="color:#555555;">make</span></span><span style="color:#555555;"><span style="color:#555555;">UI</span></span><span style="color:#555555;"><span style="color:#555555;">Adjustment. </span></span></p><p><p>the <span style="color:#555555;">key point here is that the user clicks</span> <span style="color:red;">when paused, it</span> is <span style="color:red;">Service</span> <span style="color:red;">called in</span> <span style="color:red;">Downloadutil.ispause = True</span> <span style="color:#555555;">, so the above</span> <span style="color:#555555;"></span> while <span style="color:#555555;">the Loop ends, stops the download, and</span> <span style="color:red;">through the database</span> <span style="color:red;">Update ()</span> <span style="color:red;">Save Progress Values</span> <span style="color:#555555;">. This value is then fetched at the time of the continuation, and the download task request for the starting point of the file is restarted to the server, and the write operation continues at the corresponding location in the local File. <span style="color: rgb(85, 85, 85);">This article original, reproduced please indicate the source for</span> Seu_calvin's blog <span style="color: rgb(85, 85, 85);">. </span></span></p></p><p><p><br></p></p><span style=" color: rgb(153, 51, 0);"><span style=" color: rgb(153, 51, 0);"><strong>6.</strong></span></span><span style=" color: rgb(153, 51, 0);"><span style=" color: rgb(153, 51, 0);"><strong>the effect is as</strong></span> follows</span><br><br><br><br><p><p>Android Development--the principle of breakpoint continuation and its implementation</p></p></span>
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