[Switch] Use DownloadProvider to complete download tasks

   A colleague is doing an automatic update task, and it's almost the same. A very important part here is the problem of downloading the update package from the website. This is not a big problem for many developers. You can also search for a lot of source code on the Internet and download it, that is to use httpClient. There are many methods. I will not talk about it here. What I want to talk about here is to use 2.3 The DownloadProvider API that has been opened by itself to download can save a lot of code we write, and it is more professional. The most important thing is that this DownloadProvider can realize the function of resuming the download, and it can also be used in some major applications. Seeing its figure, such as browser (browser) and market (market), all call this API, but before 2.3, this API interface was not announced, so it will be more troublesome to use, of course, if it is internal For developers, there is no problem at all, just change some permissions in the XML file of DownloadProvider, I won't go into details here, I only talk about 2.3.

  In fact, there are many articles on the Internet for the announcement of this API, but I found that those articles are all the same, and they do not tell you how to use this API, so I thought of writing an article myself to tell everyone how to use this API.

  Below, I will explain this API in detail:

  First initialize a DownloadManager object

     DownloadManager manger=getSystemService(Context.DOWNLOAD_SERVICE) ;

    Then, define DownloadManager.Request, this object, very simple

 

DownloadManager.Request down=new DownloadManager.Request (Url.parse(http://122.com/222.apk));

 There is a URL in it. For simplicity, I wrote it like this, everyone understands.

DownloadManager.Request setShowRunningNotification(boolean show) //whether to display the download progress prompt
DownloadManager.Request setTitle(CharSequence title) //Set the title of the notification
DownloadManager.Request setVisibleInDownloadsUi(boolean isVisible) //Set the interface of the download management class during processing whether to display

 DownloadManager.Request setAllowedNetworkTypes(int flags) //Set the allowed network types. Android 2.3 has done a good job in this step. There are currently two definitions: NETWORK_MOBILE and NETWORK_WIFI. We can choose to use mobile network or Wifi to download.
DownloadManager.Request setAllowedOverRoaming(boolean allowed) //For download, considering the traffic cost, whether to allow roaming here.
DownloadManager.Request setDescription(CharSequence description) //Set a description, mainly the notification prompt displayed at the end, you can write your own to distinguish
DownloadManager.Request setDestinationInExternalFilesDir(Context context, String dirType, String subPath) //Set the destination to be stored externally Directory, the general location can be obtained with the getExternalFilesDir() method.
 The method setDestinationInExternalFilesDir must be emphasized. If this method is not used properly, there may be problems.

 dirType This parameter is a directory under the external storage, that is, a directory under the SD card, such as DCIM, the name can be taken by yourself, but it must be a directory under the SD card.

The subpath parameter is the name of the last downloaded file in the Url above. As mentioned above, this parameter should be 222.apk.

Now, instantiate down, add some settings you want, remember that the method setDestinationInExternalFilesDir must be implemented, and the others can be chosen by yourself.

 

 

 With this step, we can start to implement the download function.

   manger. enqueue (down); start downloading

  It's easy! ! !

 

  You may have questions, isn't it that there is a function of resuming the upload from a breakpoint? Why did I do as you asked, and there was no continuation after the second boot, are you lying to us?

  To be honest, I'm not, I really didn't lie to you, this function of resuming from a breakpoint is not completed by the system itself, and we also need to complete the function of resuming by ourselves, so that

Only the Downloadprovider will know what we want to resume, and then we can realize the function of this resume.

    It's not finished yet, keep reading

 

  To achieve the function of continued transmission, how to achieve it! It's very simple, as long as you write a database in your own application, and then during the download process, monitor the data in the download database through a service with an interval of X seconds, and then compare the total data in it with the current total. According to the data, see if they are equal. If they are equal, it means that the download has been completed. In this case, we will not explain it. What we want to talk about is the situation where the download has not been completed.

  If it is not downloaded and not equal, we must record the content of this database, such as the data ID in DownProvider, the total size, and the current size to our own database, so that we can manage continued uploads.

  For example, when we download something, suddenly the network is disconnected, and then our own service writes the contents of the database in the current DownProvider to our own database, and then starts the next time. , we will also open this service first, the function is to query whether there are unfinished downloads in our database, that is, when the total size is not equal to the current size,

   At this time, what we have to do is to get the cursor of the unfinished downloaded data, and then get the ID of the unfinished downloaded data record of DownProvder recorded in the database before the last shutdown that we want to know. number, and then, we can use this ID number to pass this ID number back to the manger we defined above to complete the task of continuing the transfer.

  I'm quite busy right now, I'll post the specifics in a couple of days. . .

Transfer: http://blog.csdn.net/poolwater/article/details/6916572

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326481968&siteId=291194637