Open source China Oschina Android Client Source Analysis (9) Download apk function

Source: Internet
Author: User

The source code uses the following to download the client class as Downloadservice, is a service. If you do not understand the Android service, it is recommended to check the knowledge of the service. The source code analysis is as follows:

1 First, let's look at a few overriding methods in the service:

1.1onCreate () first declared the custom binder object, and in the custom binder added several interfaces can access the service method, we found in these methods, the actual use of the start () method to start downloading the APK, the others are not used. Gets the notification Manager. Set up the service as a non-foreground service. In the code comment, the fire ant indicates uncertainty. In fact, if the service is set to a front-desk service, bound to a notification, then app application level is relatively high, can effectively reduce the likelihood of service being killed. I used in the actual development of the front desk service.

Gets the correlation variable in the 1.2onBind () function, returning the Binder object. Here, it is possible to optimize, since the binder has been customized and the relevant methods are declared in the binder, we can put the settings of some variables in the method of the binder declaration, so as to ensure that when multiple activity and service binding, convenient processing, such as multiple download tasks.

2 defines the way the download thread implements the Runnable interface. Runnable mdownapkrunnable = new Runnable (); Because the download is relatively time consuming and the service itself is running in the main thread, it is dedicated to downloading with the child thread;

3 defines a message processor Handler Mhandler = new Handler (), when a message from a child thread (download thread) is received, the download progress update is processed, the download completes the install APK, and the notification function is canceled.

4 Core Download Method Downloadupdatefile (), this method has currently completed the basic download function, if the download halfway, disconnected, then the download failed, no processing, it is recommended to catch the exception of the situation through callback or broadcast notify the user download failure, in addition to shutting down the service. If it is more powerful, it is how to achieve the continuation of the breakpoint. Here the fire ant in the sub-thread, every 10 to do an update, to avoid frequent updates to the progress of notifications, the transition consumes resources.

5 Start the Download service method in the Uihelper class of the Opendownloadservice method. There are two ways to start a service in this method, why use two kinds? First of all, we want to communicate with the service, using the binder to access the start download method, all must use the Bindservice (intent, Conn, context.bind_auto_create) method, in addition, the user after the interface to perform the download update, It is possible to jump to another interface, that is, the current and service-bound interface is destroyed, then he removes the binding between the service, if only this interface and the service is bound to the interface destruction, it means that the service and all the binding is lifted, the service will execute the Onbind () method, The OnDestroy () method is then executed, the service is destroyed and the download cannot continue. However, when using the StartService method, a timely visitor exits the access, but the service is still running and can still continue to download.

6 other and download related to a dead mainactivity in Checkupdate (), check the server for updates information. The other is that the Updatemanager class is processed according to the results of the check update.

Open source China Oschina Android Client Source Analysis (9) Download apk function

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.