Detailed explanation of Android downloadprovider source code

Android downloadprovider source code analysis:

The source code compilation of download is divided into two parts. One is downloadprovider Apk, one is downloadproviderui apk.

The source codes of the two apks are located in

packages/providers/DownloadProvider/ui/src packages/providers/DownloadProvider/src

Among them, downloadprovider is the implementation of download logic, and downloadproviderui is the implementation of interface.

Then, although the download in downloadprovider is mainly performed through downloadservice, it involves notification update, download progress display, download management, etc.

Therefore, there are many other classes to operate separately.

Downloadprovider -- encapsulation of database operation, inherited from ContentProvider; Downloadmanager -- most of the logic is to further encapsulate data operations for external calls; Downloadservice -- encapsulates file download, delete and other operations, and manipulates the downloaded norification; Inherited from service; Downloadnotifier -- status bar notification logic; Downloadreceiver -- cooperate with downloadnotifier for file operation and notification; Downloadlist -- download app main interface, file interface interaction;

Downloading usually starts with clicking the link in the browser. Let's take a look at the code in the browser first

In browser's Src / COM / Android / Browser / downloadhandler We can see a complete download call in the Java function. We can also refer to this paragraph when writing our own app:

In this operation, we saw various parameters that were added to request, and finally called the enqueue of DownloadManager to download, and at the beginning, the toast that started downloading was popped up. Manager is an instance of downloadmanager, which exists with frameworks / base / core / Java / Android / APP / downloadmanager java。 You can see the implementation of enqueue as follows:

Enqueue function mainly decomposes the rquest instance into a contentvalues instance and adds it to the database. The function returns the ID returned by the inserted data; ContentResolver. The insert function will be called into the insert function of ContentProvider implemented by downloadprovider. If we check the insert code, we can see that there are many operations. But we only need to focus on a few key parts:

Here, we can see the call of downloaded downloadservice. Because it is a startservice method, we are going to use the oncreate method in downloadservice.

Here, we can see that a handler is started to receive the callback

Then go

Is to register to listen to downloads Impl. ALL_ DOWNLOADS_ CONTENT_ Observer for URI. After oncreate, the onstartcommand method will be called

In the enqueueupdate function, we will send an msg to mupdatehandler_ UPDATE Message,

Received and processed in mupdatecallback:

The focus here is on the updatelocked () function

Let's focus on file downloading. The startdownloadifready function:

In the downloadthread processing, if the HTTP status is OK, the transferdate will be processed.

------

At this point, the process of downloading files is finished, and continue to return to the updatelocked () function of downloadservice; Focus on the updatewith () function of downloadnotifier, which is used to update the notification

Finally, call mNotifManager.. notify(tag,notif); The title and description of different notifications are set according to different states

Thank you for reading, hope to help you, thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>