Download Manager in Android implements version update and monitors download progress instances

Introduction to downloadmanager

Downloadmanager is a downloadmanager provided by Android 2.3 (API level 9) in the form of system service to handle long-term download operations. It contains two static inner classes downloadmanager. Query (used to query download information) and downloadmanager. Request (used to request a download).

Downloadmanager mainly provides the following methods:

Public long enqueue (request request) adds the task to the download queue and returns the downloadid, which can be used to query the download information later. If the network does not meet the conditions, sdcard is being mounted, exceeds the maximum concurrency number and other exceptions, it will wait for download. If it is normal, it will be downloaded directly.

Public int remove (long... IDS) deletes the download. If you cancel the download, the downloaded files and records will be deleted at the same time.

Public cursor query (query query) queries download information, including the total size of downloaded files, the size of downloaded files and download status.

Introduction to contentobserver

Public void contentobserver (handler) all derived classes of contentobserver need to call this construction method. Parameter: handler handler object is used to modify UI in the main thread.

Public void onchange (Boolean selfchange) will call back this method when the content in the observed URI changes. All derived classes of contentobserver need to overload this method to process logic.

The steps to observe a specific URI are as follows:

1. To create our specific contentobserver derived class, we must overload the parent class construction method and the onchange () method to handle the function implementation after the callback.

2. Register a derived class instance of contentobserver for the specified URI. When the given URI changes, call back the instance object for processing, and call the registercontentobserver() method to register the content observer.

3. Because the lifecycle of contentobserver is not synchronized with activities, services, etc. Therefore, you need to manually call unregistercontentobserver () to log off the content observer when it is not needed.

design sketch:

1: Perform Download

Download configuration

add permission

2: Monitor download progress

Register contentobserver

The three parameters are the URI to listen on. False means that the URI is exactly matched, and true means that the derived URI and the derived class instance of contentobserver can be matched.

Query downloaded data size

In order to improve performance, start the scheduled task here, query the data size every 2 seconds and send it to the handle to update the UI.

Activity and service communication

Since we want to update the download progress in the activity in real time, we need the activity to bind the service to establish communication.

Provide an interface Real-Time callback progress value in the service. Isbindservice is used to identify whether the activity has bound a service. When calling the bindservice (serviceconnection conn) method, if the binding is successful, it will return true, otherwise it will return false. Only when it returns true can it be unbound, otherwise an error will be reported.

3: Broadcast monitoring download succeeded

After the download is completed, the installation will be automatic, and the APK storage path will be recorded

After the download is successful, save the APK storage path to the SP, close the timer and open the APK installation interface.

4: Aftermath treatment

1. Turn off timer, thread

When receiving the broadcast of download completion, immediately stop the timer and cancel the thread.

2. Unbind service, log off broadcast, log off contentobserver

When the service is unbound, log off the broadcast that monitors the download completion and the contentobserver that monitors the download progress.

3. Delete apk

After the application is successfully installed, the APK is deleted when it is started again.

5: Specific application

First, upload the current application version number to the server and let the server check whether the version can be updated; If the version can be updated, bind the service and start downloading APK. After downloading, the installation interface will pop up directly, and the APK storage path will be recorded at the same time; Check and delete APK when starting next time.

Source code: downloadmanager_ jb51.rar

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