Downloadmanager realizes the file download function

preface

About file downloading, it may not be a strange content. Downloading is only one aspect in network operation, but there are still many things to say about file downloading. Today I'll take the way of downloading.

There are many ways to download files, such as multi-threaded download, single threaded download, etc. when I first came into contact with file download, I saw a video about the principle of file multi-threaded download, which talked about multi-threaded download, and then wrote a download class to realize file download, although it can also realize file download in the end, But the effect is not very good. Sometimes the files can't be downloaded successfully. At that time, I thought there should be encapsulated tools to help download. Indeed, after Baidu, I found many third-party jar packages to download. Among them, the afinal framework I have been using is pretty good. I don't want to use third-party things every time. The Android system should have its own tool classes to download, and then the downloadmanager we're going to talk about today.

I came into contact with downloadmanager because I was writing my own music player some time ago and needed to get songs from the network and download them. At this time, I wanted to find a simple download method and then know it. I haven't made a specific study on its use, but I just used it in my own application, Now write it down and share how it was done.

realization

Download the content that I mainly care about in three aspects:

From where to where to complete the subsequent operation

These three aspects are the most important. Let me download a song to explain:

First, we need to get an instance of downloadmanager. Since it is a service provided by the system, we naturally need to use getsystemservice to get it. DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); After getting the manager object, we need a download request to set the downloaded information (i.e. "where to come from" and "where to go"). The download request is an internal class request of downloadmanager, and we have become the object of the request: downloadmanager.request request = new downloadmanager.request (URI. Parse (URL)); You can see that a constructor of request passes in a URI object. We call the static method of URI to get a URI object according to the downloaded URL, which is what we call "where to come from".

The following three methods of three requests need to be called:

setTitle(String title) setDescription(String desc) setNotificationVisibility(int flag)

Settitle is the title of setting download, and setdescription is the description of setting download, as shown in the following figure:

Where "nobody" is the title and "wondergirls" is the description.

The setnotificationvisibility method needs to be discussed. Its parameters include four cases:

Request.VISIBILITY_ Visible: during the download, the notification of the download will always be displayed in the notification bar. When the download is completed, the notification will be removed, which is the default parameter value.

Request.VISIBILITY_ VISIBLE_ NOTIFY_ Completed: the notification of the download will always be displayed in the notification bar during the download process. After the download is completed, the notification will continue to be displayed until the user clicks the notification or eliminates the notification.

Request.VISIBILITY_ VISIBLE_ NOTIFY_ ONLY_ Completion: this notification will be displayed only after the download is completed.

Request.VISIBILITY_ Hidden: do not display the notification of the download request. If you want to use this parameter, you need to add download to the application manifest file_ WITHOUT_ Notification permission.

We can select appropriate parameters as needed. If we don't want to display the progress when downloading, we can set request.visibility_ Hidden, so we can't see the notice when downloading, but the disadvantage is that we can't know whether the download is successful.

The problem of "where to come from" has been solved. Now let's look at the problem of "where to go": the function setdestinationuri is used to set the download path. The parameter is a URI object, which is used as follows:

In this way, we have set the download path.

After the request is set, we can call the manager method to start downloading: manager. Enqueue (request); This method is to put the download request into a queue and start downloading. It has a return value. This return value is a download ID, which is used to uniquely identify this download. We will use this ID below, and we will record it as mdownloadid;

Finally, there is only the situation after the download is completed. After the download is completed, we may have operations, such as opening files after the download is completed. For example, we see some software upgrading. After the APK file is downloaded, we can directly open the installation. These are all operations after the download is completed. So how to implement it?

Originally, the downloadmanager will send a broadcast after the download is completed. We only need to register a broadcast receiver to get the broadcast, and then we can carry out our own operations.

In this way, we registered a broadcast receiver to receive the broadcast sent after the download. After receiving it, we directly opened the file. In the code, I set the APK file to open, that is, set the file type to "application / vnd.android.package-archive" ", where mdownloadid is the download ID we get, so we can download and open the file.

Summary

In this way, the file download is not so complex. Of course, we are just from the perspective of using code. If we let ourselves write the download source code, ha ha, there is nothing we can do.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>