Android system Download management Downloadmanager feature introduction and use Example

Source: Internet
Author: User
<span id="Label3"></p><p><p>http://www.trinea.cn/android/android-downloadmanager/</p></p><p><p>This article mainly <strong>unifies the <strong>source code to introduce the <strong>Android</strong> </strong> system Download Management Downloadmanager the powerful function and the use</strong> .<br>This is a series of blogs that have been written for a long time, This article mainly introduces Downloadmanager's features and examples, followed by two of the underlying designs that will introduce download management (downloadprovider, downloadmanager, downloadmanagerui), download management for enhancements and bug Fixes.</p></p><p><p></p></p><p><p>Sample APK can be downloaded from these addresses: Google Play, 360 mobile phone assistant, Baidu Mobile assistant, Xiaomi app store, Pea pod</p></p><p><p>Can run code address visible Downloadmanager Demo, as Follows:</p></p><p><p></p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">about the download management crap</span></span></span><p><p></p></p><p><p><strong>first, Downloadmanager Simple Introduction</strong><br>Downloadmanager is a class that is open to Third-party applications and contains two static internal classes Downloadmanager.query and Downloadmanager.request. <strong>Downloadmanager.request is used to request a download, downloadmanager.query used to query the download information</strong> , the specific features of these two classes will be interspersed in the following sections. Downloadmanager source is visible <span class="__cf_email__" data-cfemail="88cce7ffe6e4e7e9ecc5e9e6e9efedfac8cffaedf8ebe7eced">[email protected]</span>.</p></p><p><p></p></p><p><p>Downloadmanager mainly provides the following interfaces:<br>public <strong>long enqueue (request request)</strong> performs the download and returns Downloadid,downloadid can be used for later query download Information. If the network does not meet the conditions, sdcard mount, exceeding the maximum number of concurrent exceptions will wait for the download, normal download directly.<br>The public <strong>int remove (long ... ids)</strong> deletes the download if it is canceled in the Download. Download files and records are also Deleted.<br>public <strong>Cursor</strong> query queries the download Information.</p></p><p><p></p></p><p><p>public static Long Getrecommendedmaxbytesovermobile (the Maximum number of bytes downloaded by the context context via the mobile network<br>Public String getmimetypefordownloadedfile (long Id) to get the downloaded mimetype, how to set it later will be introduced</p></p><p><p></p></p><p><p>Other: by looking at the code we can see that there is also a cursortranslator private static inner class. This class is primarily a proxy for query. Make a mapping between Downloadprovider and Downloadmanager. The more than 10 states in downloadprovider correspond to five states in downloadmanager, the failure in downloadprovider, and the reason for the pause reason conversion for downloadmanager.</p></p><p><p></p></p><p><p><strong>second, Download Management Example</strong><br>The following details are available for download using Downloadmanager.<br><strong>1. Add Permissions in Androidmanifest</strong></p></p><span class="crayon-language"><span class="crayon-language">Java</span></span> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums" data-settings="show">12 </td> <td class="crayon-code"><span class="crayon-o"><<span class="crayon-v">uses<span class="crayon-o">-<span class="crayon-e">permission <span class="crayon-v">android<span class="crayon-o">:<span class="crayon-v">name<span class="crayon-o">= <span class="crayon-s"> "android.permission.INTERNET" <span class="crayon-h"> <span class="crayon-o">/<span class="crayon-o">> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> <span class="crayon-o"><<span class="crayon-v">uses<span class="crayon-o">-<span class="crayon-e">permission <span class="crayon-v">android<span class="crayon-o">:<span class="crayon-v">name<span class="crayon-o">= <span class="crayon-s"> "android.permission.WRITE_EXTERNAL_STORAGE" <span class="crayon-h"> <span class="crayon-o">/<span class="crayon-o">> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></td> </tr> </tbody> </table><p><p>Network access is a must, add sdcard Write permission for Sdcard.</p></p><p><p></p></p><p><p><strong>2. Call downloadmanager.request to start the download</strong></p></p><span class="crayon-language"><span class="crayon-language">Java</span></span> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums " data-settings="show">1234567891011</td> <td class="crayon-code"><span class="crayon-e"><span class="crayon-e">downloadmanager <span class="crayon-v">downloadmanager<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-sy">(<span class="crayon-v">downloadmanager<span class="crayon-sy">)<span class="crayon-e">getsystemservice<span class="crayon-sy">( <span class="crayon-v">download_service<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span><span class="crayon-t"><span class="crayon-t">string<span class="crayon-h"> <span class="crayon-v">apkurl<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-s"> http://img.meilishuo.net/css/images/ androidshare/meilishuo_3.6.1_10006.apk "<span class=" crayon-sy " ; < span> </span> </span> </span> </span> </span> </span> </span></span></span><span class="crayon-v"><span class="crayon-v">downloadmanager<span class="crayon-sy">.<span class="crayon-e"> Request <span class="crayon-v">request<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> Span class= "crayon-r" >new<span class="crayon-h"> <span class="crayon-v">downloadmanager<span class="crayon-e" . <span>request<span class="crayon-sy"> (<span class="crayon-v">uri<span class="crayon-sy">.< Span class= "crayon-e" >parse<span class="crayon-sy"> (<span class="crayon-v">apkurl<span class="crayon-sy">) <span class="crayon-sy" ) <span , < span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span><span class="crayon-v"><span class="crayon-v">request<span class="crayon-sy">. <span class="crayon-e"> Setdestinationinexternalpublicdir<span class="crayon-sy">(<span class="crayon-s">"trinea"<span class="crayon-sy">,<span class="crayon-h"> <span class="crayon-s">"meilishuo.apk"<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span></span></span></span></span><span class="crayon-c"><span class="crayon-c">//request.settitle ("meilishuo");</span></span><span class="crayon-c"><span class="crayon-c">//request.setdescription ("meilishuo desc");</span></span><span class="crayon-c"><span class="crayon-c">//request.setnotificationvisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);</span></span><span class="crayon-c"><span class="crayon-c">//request.setallowednetworktypes (DownloadManager.Request.NETWORK_WIFI);</span></span><span class="crayon-c"><span class="crayon-c">//request.setnotificationvisibility (DownloadManager.Request.VISIBILITY_HIDDEN);</span></span><span class="crayon-c"><span class="crayon-c">//request.setmimetype ("application/cn.trinea.download.file");</span></span><span class="crayon-t"><span class="crayon-t">long<span class="crayon-h"> <span class="crayon-v">downloadid<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-v">downloadmanager<span class="crayon-sy">. <span class="crayon-e"> Enqueue<span class="crayon-sy">(<span class="crayon-v">request<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></td> </tr> </tbody> </table><p><p>The above calls Downloadmanager's Enqueue interface for download and returns a unique DOWNLOADID.</p></p><p><p></p></p><p><p>Downloadmanager.request other settings are optional, except that the URI of the constructor must be the Exception. The following are described individually:<br>Request.setdestinationinexternalpublicdir ("trinea", "meilishuo.apk"), The Trinea folder that is set to the SD card, and the file name is Meilishuo.apk.</p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">Setdestinationinexternalpublicdir Source</span></span></span> <p><p>From the source we can see the download full directory for environment.getexternalstoragepublicdirectory (dirtype). however, file is created by File.mkdir (), which creates a new folder exception if the parent directory does not Exist. So before downloading We'd Better call the File's Mkdirs method recursively to create subdirectories, as Follows:</p></p><span class="crayon-language"><span class="crayon-language">Java</span></span> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums " data-settings="show">12</td> <td class="crayon-code"><span class="crayon-e"><span class="crayon-e">File <span class="crayon-v">folder<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-r">new<span class="crayon-h"> <span class="crayon-e">file<span class="crayon-sy">(<span class="crayon-v">folderName<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span></span></span></span></span></span></span><span class="crayon-st"><span class="crayon-st">return<span class="crayon-h"> <span class="crayon-sy"> (<span class="crayon-v">folder< Span class= "crayon-sy". <span class="crayon-e">exists<span class="crayon-sy"> (<span class="crayon-h" ) <span> <span class="crayon-o">&&<span class="crayon-h"> <span class="crayon-v">folder<span class="crayon-sy">.<span class="crayon-e">isdirectory<span class="crayon-sy"> (<span class="crayon-h" ) <span> <span class="crayon-sy">?<span class="crayon-h"> <span class="crayon-t">true<span class="crayon-h"> <span class="crayon-o">:<span class="crayon-h"> <span class=" Crayon-v ">folder<span class=" crayon-sy ">.<span class=" crayon-e "> Mkdirs<span class="crayon-sy" (<span ) <span ; < span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></td> </tr> </tbody> </table><p><p>otherwise, an exception is reported</p></p> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums" data-settings="show">12 </td> <td class="crayon-code">java<span class="crayon-sy">.lang<span class="crayon-o" . illegalstateexception<span>:<span class="crayon-h"> Unable<span class="crayon-h"> To<span class="crayon-h"> Create<span class="crayon-h"> Directory<span class="crayon-o">:<span class="crayon-h"> <span class=" Crayon-o ">/storage<span class=" crayon-o ">/sdcard0<span class=" crayon-o ">/trinea<span class=" crayon-o ">/AA </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> at<span class="crayon-h"> Android<span class="crayon-sy">.app<span class="crayon-sy" . downloadmanager<span> $Request <span class="crayon-sy">.setdestinationinexternalpublicdir<span class="crayon-sy"> (downloadmanager<span class="crayon-sy">.java<span class="crayon-sy">) </span> </span> </span> </span></span></span></span></td> </tr> </tbody> </table><p><p>Other settings The download path interface is Setdestinationuri,setdestinationinexternalfilesdir,setdestinationtosystemcache. Where Setdestinationtosystemcache is only available for system apps.</p></p><p><p></p></p><p><p><strong>Request.allowscanningbymediascanner ();</strong> Indicates that Mediascanner is allowed to scan to this file, which is not allowed by Default.</p></p><p><p><strong>Request.settitle ("meilishuo");</strong> Set the title of the notification bar prompt in the download<br><strong>request.setdescription ("meilishuo desc");</strong> Setting up notification bar tips in downloads<br><strong>Request.setnotificationvisibility (DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);</strong><br>Indicates whether the download is in progress and the notification bar for download completion is Displayed. Only notifications in the download are displayed by Default. Visibility_visible_notify_completed indicates that a notification bar prompt appears when the download is Complete. Visibility_hidden indicates that no notification bar prompt is displayed, which requires the addition of permission Android.permission.DOWNLOAD_WITHOUT_NOTIFICATION in Androidmainfest.</p></p><p><p></p></p><p><p><strong>Request.setallowednetworktypes (DownloadManager.Request.NETWORK_WIFI);</strong><br>Indicates the type of network allowed for download, which is allowed by default under any Network. There are three kinds of network_mobile, network_wifi, Network_bluetooth and their combinations to choose FROM. If only WiFi is allowed to download and the current network is 3g, the download will wait.<br>Request.setallowedoverroaming (boolean Allow) Whether roaming is allowed in the case of a mobile Network.</p></p><p><p></p></p><p><p><strong>Request.setmimetype ("application/cn.trinea.download.file");</strong><br>Sets the minetype of the downloaded File. Because clicking on a downloaded complete file and downloading is done in the Download admin ui, clicking on the notification bar will open the file according to mimetype, so we can take advantage of this Property. For example, MimeType is set to application/cn.trinea.download.file, we can set an activity intent-filter to application/ cn.trinea.download.file, which is used to respond to clicked Open Files.</p></p><span class="crayon-language"><span class="crayon-language">Java</span></span> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums " data-settings="show">1234567</td> <td class="crayon-code"><span class="crayon-o"><span class="crayon-o"><<span class="crayon-v">Intent<span class="crayon-o">-<span class="crayon-v">filter<span class="crayon-o">></span> </span> </span> </span> </span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-o"><<span class="crayon-e">action <span class="crayon-v">Android<span class="crayon-o">:<span class="crayon-v">name<span class="crayon-o">=<span class="crayon-s">"android.intent.action.VIEW"<span class="crayon-h"> <span class="crayon-o">/<span class="crayon-o">></span> </span></span></span></span></span></span></span></span></span></span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-o"><<span class="crayon-e">category <span class="crayon-v">Android<span class="crayon-o">:<span class="crayon-v">name<span class="crayon-o">=<span class="crayon-s">"android.intent.category.DEFAULT"<span class="crayon-h"> <span class="crayon-o">/ <span class="crayon-o">></span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span> <span class="crayon-h"><span class="crayon-h"> <span class="crayon-o"><<span class="crayon-e">data <span class="crayon-v">android<span class="crayon-o">:<span class="crayon-v">mimeType<span class="crayon-o">=<span class="crayon-s">"application/cn.trinea.download.file"<span class="crayon-h"> <span class="crayon-o">/ <span class="crayon-o"> ></span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span><span class="crayon-o"><span class="crayon-o"><<span class="crayon-o">/<span class="crayon-v">Intent<span class="crayon-o">-<span class="crayon-v">filter<span class="crayon-o">></span> </span> </span> </span> </span> </span></span></td> </tr> </tbody> </table><p><p><strong>Request.addrequestheader (string header, String Value)</strong><br>Add HTTP headers for Web links that request downloads, such as User-agent,gzip compression</p></p><p><p></p></p><p><p><strong>3 Download Progress status monitoring and query</strong></p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">Download Progress status Monitor code</span></span></span> <p><p>Where we will monitor <strong>uri.parse ("content://downloads/my_downloads")</strong>. Then query the download status and progress, send handler to update, handler processing is to set the progress bar and Status.</p></p><p><p>The main code of Downloadmanagerpro.getbytesandstatus is as follows, you can directly introduce <span class="__cf_email__" data-cfemail="0551776c6b6064446b61776a6c61466a68686a6b45426c716d7067">[email protected]</span>(welcome star and fork ^_^) as your project library ( How to pull a replacement code and add a public library):</p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">Download Progress status Query code</span></span></span><p><p>We can see from the above code that we call Downloadmanager.query () to Query. Downloadmanager.query for the download management of the Open information query class, mainly including the following interfaces:</p></p><p><p>Setfilterbyid (long ... ids) filter based on download ID<br>Setfilterbystatus (int Flags) filtering based on download status<br>Setonlyincludevisibleindownloadsui (boolean Value) is filtered based on whether it is visible in the download Ui.</p></p><p><p></p></p><p><p>ORDER BY (String column, int Direction) is sorted by column, but currently only supports Downloadmanager.column_last_modified_ Sort timestamp and Downloadmanager.column_total_size_bytes.</p></p><p><p></p></p><p><p><strong>4 downloads successfully monitored</strong><br>After the download is complete, the download management will issue <strong>Downloadmanager.action_download_complete</strong> this broadcast and pass Downloadid as the Parameter. By accepting the broadcast, we can open the contents of the download to complete the Operation. The code is as Follows:</p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">Download Successful listener</span></span></span><p><p></p></p><p><p><strong>5, respond to the notification bar click</strong><br><strong>(1) in response to the download notification bar click</strong><br>Click on the notification bar prompt in the download and the system will send the ACTION to downloadmanager.action_notification_clicked broadcast separately for the downloaded app. Intent.getdata is content://downloads/all_downloads/29669, and the last one is Downloadid.<br>If you download more than one app at a time, intent contains the Downloadmanager.extra_notification_click_download_ids key, which represents the downloaded Downloadid array. Here the design to download management notification bar display mechanism, will be described in the next article.</p></p><p><p></p></p><p><p><strong>(2) response to download complete the notification bar click</strong><br>After downloading, the following code is called for processing, from which we can find that the system calls the view action to query according to Mimetype. So we can take advantage of the Setmimetype function of the downloadmanager.request we are introducing.</p></p><span class="crayon-title"><span class="crayon-title"><span class="crayon-title">Opendownload Source</span></span></span><p><p></p></p><p><p>If too many elements on the interface need to be updated, and the fast speed of the execution onchange will have a certain impact on page Performance. Recommended Scheduledexecutorservice regular queries, as Follows:</p></p><span class="crayon-language"><span class="crayon-language">Java</span></span> <table class="crayon-table"> <tbody> <tr class="crayon-row"> <td class="crayon-nums " data-settings="show">123456789</td> <td class="crayon-code"><span class="crayon-m"><span class="crayon-m">public<span class="crayon-h"> <span class="crayon-m">static<span class="crayon-h"> <span class="crayon-e">scheduledexecutorservice <span class="crayon-v">scheduledexecutorservice<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-v"> Executors<span class="crayon-sy">. <span class="crayon-e"> Newscheduledthreadpool<span class="crayon-sy">(<span class="crayon-cn">3<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></span></span><span class="crayon-e"><span class="crayon-e">Runnable <span class="crayon-v">command<span class="crayon-h"> <span class="crayon-o">=<span class="crayon-h"> <span class="crayon-r">new<span class="crayon-h"> <span class="crayon-e">Runnable<span class="crayon-sy">(<span class="crayon-sy">)<span class="crayon-h"> <span class="crayon-sy">{</span> </span> </span> </span> </span> </span></span></span></span></span></span></span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-n">@Override</span> </span></span><span class="crayon-h">public <span class="crayon-h"> <span class="crayon-m"><span class="crayon-h"> <span class="crayon-t">void<span class="crayon-h"> <span class="crayon-e">run<span class="crayon-sy">(<span class="crayon-sy">)<span class="crayon-h"> <span class="crayon-sy">{</span> </span> </span> </span> </span> </span> </span> </span></span></span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-e">Updateview<span class="crayon-sy">(<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-sy">}</span></span></span><span class="crayon-h"><span class="crayon-h"> <span class="crayon-sy">}<span class="crayon-sy">;</span></span></span></span><span class="crayon-v"><span class="crayon-v">scheduledexecutorservice<span class="crayon-sy">. <span class="crayon-e"> Scheduleatfixedrate<span class="crayon-sy">(<span class="crayon-v">command<span class="crayon-sy">,<span class="crayon-h"> <span class="crayon-cn">0<span class="crayon-sy">,<span class="crayon-h"> <span class="crayon-cn">3<span class="crayon-sy">,<span class="crayon-h"> <span class="crayon-v">timeunit<span class="crayon-sy">. <span class="crayon-v"> SECONDS<span class="crayon-sy">)<span class="crayon-sy">;</span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span> </span></span></td> </tr> </tbody> </table><p><p>Indicates a 3-second scheduled refresh</p></p><p><p>Android system Download management Downloadmanager feature introduction and use Example</p></p></span>

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.