Downloadmanager Download Management class 2.3 New API Introduction

Source: Internet
Author: User

This article go to: http://www.eoeandroid.com/thread-50236-1-1.html

Starting with Android 2.3, we have a new download management class, which we find android.app.DownloadManager in the SDK documentation. Download management class can deal with multiple HTTP download tasks for a long time, the client only needs to give the requested URI and location of the target file, download management uses a AIDL server so you can rest assured in the background, while instantiating the method needs to use Getsystemservice ( Context.download_service), Android123 once again reminded users with API level 9 to easily implement file downloads on the Android platform via the new API.

The Downloadmanager class provides several ways to handle the

Long Enqueue (downloadmanager.request Request)//Save in queue a new download

Parcelfiledescriptor Opendownloadedfile (Long ID)//Open a downloaded file for reading, the long ID in the parameter is a record in a provider.

Cursor query (downloadmanager.query query)//Queries a download, returns a Cursor

int remove (long ... IDs)//Cancel the download at the same time remove these strips from the download management.

We can see that the methods provided are simpler and give us a way to add, query and remove the final encapsulation of our operations into a provider database, but for the details of querying and adding tasks, We're going to look at the Downloadmanager.request class and the Downloadmanager.query class.

I. Members and definitions of the Downloadmanager.request class

Downloadmanager.request Addrequestheader (string header, String value)//Add an HTTP request header, for these two parameters, the Android Development network gives you a small example, For example, the User-agent value can be Android123 or Windows XP, and so on, primarily to provide identity to the server.
Downloadmanager.request setallowednetworktypes (int flags)//Set the type of network allowed, this step Android 2.3 is doing well and there are currently two definitions of Network_ Mobile and Network_wifi we have the option to use a removable network or WiFi to download.
Downloadmanager.request setallowedoverroaming (Boolean allowed)//for downloads, consider whether roaming is allowed here, taking into account traffic charges.
Downloadmanager.request setdescription (charsequence description)//Set A descriptive information, mainly the final display of the notification hint, you can write your own difference
Downloadmanager.request Setdestinationinexternalfilesdir (context, String Dirtype, String subpath)// The set target is stored in an external directory, and the general location can be obtained by using the Getexternalfilesdir () method.
Downloadmanager.request Setdestinationinexternalpublicdir (String dirtype, String subpath)//Set the public directory for external storage, Generally obtained by Getexternalstoragepublicdirectory () method.
Downloadmanager.request Setdestinationuri (URI uri)//Set the URI that needs to download the target, can be HTTP, ftp and so on.
Downloadmanager.request Setmimetype (String mimetype)//Set MIME type, see server Configuration here, the general state of all is UTF-8 encoding.
Downloadmanager.request Setshowrunningnotification (Boolean show)//whether to display a hint of download progress
Downloadmanager.request Settitle (charsequence title)//Set notification title
Downloadmanager.request Setvisibleindownloadsui (Boolean isVisible)//setting whether the interface of the download management class is displayed during processing

Of course, Google also provides an easy way to instantiate this class, which is Downloadmanager.request (Uri Uri), and we simply fill in a URI, and the above setting uses the default.

Second, Downloadmanager.query class

For the state of the current download, we can use the Downloadmanager.query class to get it, and this class is simpler, offering only two methods.

Downloadmanager.query Setfilterbyid (Long ... IDs)//filter lookup based on ID.
Downloadmanager.query setfilterbystatus (int flags)//lookup according to the status of the task.

The detailed status is defined in the Android.app.DownloadManager class and is currently defined in Android 2.3 as:

int status_failed failed
int status_paused paused
int status_pending wait will begin
int status_running is in process
int status_successful has been downloaded successfully

Finally, the Android Development network reminds you that the query method provided by the Downloadmanager class returns a cursor object that is stored in the Column_status field of the cursor.

1. The completion of the download status is broadcast in the form of notification to everyone, the current API level of 9 defines the following three kinds of intent action

Action_download_complete Download the completed action.
Action_notification_clicked is triggered when a user clicks an item in NOTIFICATION to download the management.
Action_view_downloads View Downloads

2. For an unfinished item, in cursor we look for the Column_reason field, which may have the following definition:

int Error_cannot_resume cannot be continued for some other reason.
int Error_device_not_found External storage device not found, such as SD card not plugged in.
int error_file_already_exists The file you want to download already exists, Android123 prompts you to download an admin class that does not overwrite existing files, so if you need to download them again, delete the previous files first.

The int error_file_error may cause a file error due to the SD card.
The int error_http_data_error has a problem with the HTTP transfer process.
int error_insufficient_space due to insufficient space of SD card

int error_too_many_redirects This HTTP has too many redirects, resulting in the inability to download properly
int Error_unhandled_http_code cannot get the reason for HTTP error, for example, the remote server is not responding.
int Error_unknown unknown type of error.

3. For some states of a pause, the same value for the Column_reason field may be the following definition

int Paused_queued_for_wifi Because of mobile network data problem, wait for WIFI connection can be used before re-enter the download queue.
An int paused_unknown An unknown cause caused a pause in the task download.
int paused_waiting_for_network may not be able to download because there is no network connection, waiting for available network connection recovery ...
int paused_waiting_to_retry The download paused due to numerous reasons, waiting for a retry.

The introduction of the new download management Downloadmanager in Android 2.3 is basically complete, and if you understand the basics of cursor and provider, you can see that this download management class can help us reduce the number of unnecessary code writing.

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.