Android App version updates automatically

Source: Internet
Author: User

There is now a feature in many apps that checks for a version. For example, under the settings screen of the bucket Fish TV app:

When we click Check for updates, we will initiate a version detection request to the server. The usual way to do this is to compare the app version returned by the server with the version number of the current phone installation.

(1) If the version number returned by the server is greater than the current app version number, then the app installed on the phone is not the latest version. You can prompt the user to upgrade.

(2) If it is not greater than the current version number, you can prompt the user for the latest version:

Version upgrades are also handled in two ways:

(1) Jump to the app market (for example: 360 mobile phone assistant), then according to the package name in the market location to the app, through the market download update installation.

(2) apk download is implemented in this app, update installation after download is complete.

This blog content will be around how to implement apk download, download completed after the update installation to expand.

Below I will roughly divide the content into the following sections:

(1) App version detection

(2) apk download

(3) APK update installation

(4) Encapsulation of the above functions

Based on the above 4 parts, we start each.

1.APP Version Detection:

To implement the app update download, we described above, as long as the server to save the version number of an app (the usual way is to save Versioncode, of course, you have to compare Versionname also does not matter). When the user to manually detect the version, or go to the homepage detection, the first step is to request the server version number, get the version number and the current app version number (the current version number can be obtained by PackageInfo) for comparison. The server returned a version number greater than the current app version number to prove that the app has been updated, then enter the 2nd step.

2.APK Downloads

The APK file is saved on the server. We can download it to the local phone via an HTTP stream and update the installation. There are many ways to download Android: Httpurlconnection,retrofit,okhttp, and Android native Download tool class Downloadmanager and so on. The way we use it is Google's recommended download tool class Downloadmanager. The use of Downloadmanager is actually very simple, briefly summarized as follows:

(1) obtain Downloadmanager through Getsystemservice.

(2) Initialize the request of Downloadmanager and build the download requests.

(3) Call Downloadmanager's enqueue asynchronous initiate request, which returns the ID of the current download task, which is downloadid.

(4) When the download is complete, the system will issue a broadcast conditional to Android.intent.action.DOWNLOAD_COMPLETE, we can customize the broadcast receiver, and then in the onreceive processing the download completed logic.

Detailed use of the way you can refer to the online tutorial, here will not repeat.

Above by downloading a bunch of wordy. At this point we want to ask a question: When we download is complete, it is not installed. What do I do when the user enters the app again?

A friend will say, then go to download again, and then continue to perform the update installation! Haha, this way is not wrong, but if the user malicious behavior, each download completed does not install, then we do not have to download 100 times, 1000 times. (Then mobile BOOM!!! This approach must not be used. So how do we fix it?

Very simple, when we go to the designated folder to see the download of the APK, and the version of the APK is higher than the version of the app, at this time we go to perform the installation operation. If the above conditions are not true, then proceed to the download operation.

3.APK Update Installation

I believe you are familiar with how to install an APK, the principle is relatively simple.

(1) Obtain the downloaded URI via Downloadid.

(2) Set the URI to Itent's setdataandtype as the launch condition.

(3) Call startactivity to start the corresponding intent can be.

The above 3 steps, you can complete the update function of the app.

The overall process is clear:

Version detection →apk Download (check if there are no installed apk) →apk install → Complete the update

The following is a detailed analysis of the entire process through code:

About app version detection is actually an HTTP request, no more speaking. Let's start with the APK download:

As we mentioned above, we need to detect if there is a downloaded apk before downloading. What to get through? Yes, it must be downloadid.

1> If there is a downloadid, then we get the state status of the current download through Downloadid. Status is divided into two states: success and failure.

(1) When status is successful, the download is complete, and we get the URI of the downloaded file via Downloadid. You can then get the PackageInfo by URI, with the current app's package name and version number of the comparison, when the package name is the same, and the current version number is less than the download apk version number two conditions at the same time, the installation operation is performed directly. Otherwise, execute remove, delete the download task and the file via Downloadid, and proceed with the download.

(2) When status is a failed state, that is, the download is not completed, we will directly perform a re-download.

2> If there is no downloadid, no apk has been downloaded, do the download.

The core code is as follows:

After the download is complete, the system will broadcast, in the broadcast, we compare Downloadid is the same, in the same case, directly through Downloadid to obtain the URI, and then jump to the installation interface, prompting the user to install:

So, don't forget to register the Big Horn (radio receiver) before downloading.

Finally, once the installation is complete, go to the app again and delete its downloaded APK file (the method is placed in the OnCreate lifecycle):

The above gets the address of the downloaded file via downloadapk. The downloadapk address is saved in the broadcast receiver after the download is complete.

With the above steps, we have completed all the work of the app update download installation. I believe that we have a deeper understanding and understanding.

The content of this blog is over, the problem of friends can leave a message to me.

SOURCE download

Android App version updates automatically

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.