Android Check Update Library Use Tutorial (1)

Checking updates and downloading updates can be said to be a necessary feature of an app.

Since it is a necessary function, there are many things to consider, as follows:

1. Update mode:

If a software has been updated, it needs to be forced to update the software if it is online with new functions or fixes some bug s, or the interface of the server has changed. Otherwise, it will quit the application. However, if it is only for performance optimization or interface change but the data remains unchanged, it often does not need to be forced to update, so there are two kinds of update requirements: forced update and non-forced update.

2. Background download:

Mandatory updates may be relatively simple to implement. Users must download and install the updates before they can use them. The technical requirements are not too high. For non-mandatory updates, users can choose to download updates and switch to the background for downloading. That is to say, to use service s, but background downloads will produce a third requirement.

3. Interface display:

The application downloaded in the background needs to show the progress of real-time download on the interface. For this part, many applications choose to display in the notification bar, and the CheckUpdate Library library is no exception. But it is not enough to show the progress of real-time download. When the application is downloaded, it also needs to install the application, and the user can choose not to install the application, but when he wants to install, you can not let the user again. Download it once, so you need to let the user have a place to click directly to install the application before downloading, and CheckUpdate Library also choose to display a notice in the notification bar that can not be deleted, prompting the user that the download has been completed, click can be installed. On the other hand, if the download process in the software background, due to unpredictable reasons, leading to software download failure, you need to let the software download fail. The user knows that the software download has failed. For this reason, CheckUpdate Library also displays a notice in the notification bar to remind the user that the software download has failed, and the user can click on the notification to reactivate the download Service for re-download.

4. Network State Judgment:

Maybe users use traffic when checking updates, but they do not know that they may download them directly. So from the user's point of view, when an updated Dialog pops up, they need to let users know whether their current network environment is a mobile network environment or a WIFI network environment, so that users can choose whether to use the current network environment for downloading applications.

5. Portability:

Since every application needs to check for updates and download updates, it must be highly portable. We can't write a check for updates in this application, and then paste these codes when other applications need them. This will be very error-prone and cumbersome. Using CheckUpdate Library, we can introduce the library directly in Android Studio. Use, so convenient!!! So fast!!!


Obviously, there are already some libraries on the network that check for updates or download updates, so what are the advantages of CheckUpdate Library over other libraries?

1, zero coupling: CheckUpdateLibrary from check update to download update all use HttpURLConnection,Dialog also customize, did not introduce any other third party library, and other libraries are either using the third party network request framework, or using others wrapped dialog., so developers may Tucao, this is just a simple check update and download update function, but I need more. The introduction of many third-party libraries, which invisibly increased the size of my APK, I am not happy!!! And with this library, developers need not worry about these.

2. Small size: Based on the first point, the CheckUpdate Library library is very small, only 10 kb.

3, compatible with 6.0: Because HttpURLConnection is used, the 6.0 system is also adapted.

4. Extensibility: CheckUpdate Library has built-in an entity class, CheckUpdate Info, to receive json data returned when checking updates, provided that the server agrees and docks the fields. If you don't want to use the built-in entity, developers can customize their entity classes, but you must ensure that the custom entity class contains the new AppVersionCode field, which can be used in the library. CheckUpdate Library only supports the extension of entity classes, and Dialog can only provide it.

5. Other functions: for example, you can customize the storage path of apk files, file name, whether to display real-time download progress in the notification bar when downloading in the background, etc.


So how to use it?

A small partner using Android Studio can quote the following sentence directly in gradle:

compile 'com.qiangxi.checkupdatelibrary:checkupdatelibrary:1.0.2'

Small partners using eclipse can only download library files from github and then introduce them into the project.

The github address of CheckUpdate Library is as follows:

https://github.com/qiangxi/CheckUpdateLibrary


In the code, you just need to do this. The code is as follows:

package com.qiangxi.checkupdatelibrarydemo;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.qiangxi.checkupdatelibrary.bean.CheckUpdateInfo;
import com.qiangxi.checkupdatelibrary.dialog.ForceUpdateDialog;
import com.qiangxi.checkupdatelibrary.dialog.UpdateDialog;

public class MainActivity extends AppCompatActivity {
    private CheckUpdateInfo mCheckUpdateInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
    }

    private void initData() {
        //The annotated code is used to check for updates. Here, for convenience, simulate some false data.
//        Q.checkUpdate("post", getVersionCode(), "checkUpdateUrl", new CheckUpdateCallback() {
//            @Override
//            public void onCheckUpdateSuccess(String result, boolean hasUpdate) {
//                // result: json returned by the server
//                CheckUpdateInfo checkUpdateInfo = new Gson().fromJson(result, CheckUpdateInfo.class);
//                // Update, display dialog, etc.
//                if (hasUpdate) {
//                    // Mandatory updates, where 0 means mandatory updates. In practice, you can agree with the server what number means mandatory updates.
//                    if (checkUpdateInfo.getIsForceUpdate() == 0) {
//                        //show ForceUpdateDialog
//                    }
//                    // Unmandatory updates
//                    else {
//                        //show UpdateDialog
//                    }
//                } else {
//                    // No updates, tips are the latest version, etc.
//                }
//            }
//
//            @Override
//            public void onCheckUpdateFailure(String failureMessage, int errorCode) {
//                // Failure Message: Abnormal information captured by try{}catch(){}
//                // errorCode: Not useful for the time being
//            }
//        });
        //Simulate some false data
        mCheckUpdateInfo = new CheckUpdateInfo();
        mCheckUpdateInfo.setAppName("android Check the update library")
                .setIsForceUpdate(1)//Set whether the update is mandatory or not. The parameters of this method are as long as the server agrees on what number to represent the mandatory update.
                .setNewAppReleaseTime("2016-10-14 12:37")//Software release time
                .setNewAppSize(12.3f)//The unit is M.
                .setNewAppUrl("http://shouji.360tpcdn.com/160914/c5164dfbbf98a443f72f32da936e1379/com.tencent.mobileqq_410.apk")
                .setNewAppVersionCode(20)//VersionCode for the new app
                .setNewAppVersionName("1.0.2")
                .setNewAppUpdateDesc("1,Optimizing download logic\n2,Fix some bug\n3,Complete implementation of mandatory and non-mandatory update logic\n4,Download without mandatory update,Download in the background by default\n5,When the download is successful,A notification will be displayed in the notification bar,Click on the notification,Enter the Installation Application Interface\n6,When the download fails,A notification will be displayed in the notification bar,Click on the notification,The application will be re-downloaded\n7,When downloaded,Real-time download progress will be displayed in the notification bar,But the former summary. dialog.setShowProgress(true).");
    }

    /**
     * Mandatory update
     */
    public void forceUpdateDialogClick(View view) {
        mCheckUpdateInfo.setIsForceUpdate(0);
        if (mCheckUpdateInfo.getIsForceUpdate() == 0) {
            ForceUpdateDialog dialog = new ForceUpdateDialog(MainActivity.this);
            dialog.setAppSize(mCheckUpdateInfo.getNewAppSize())
                    .setDownloadUrl(mCheckUpdateInfo.getNewAppUrl())
                    .setTitle(mCheckUpdateInfo.getAppName() + "It's updated.")
                    .setReleaseTime(mCheckUpdateInfo.getNewAppReleaseTime())
                    .setVersionName(mCheckUpdateInfo.getNewAppVersionName())
                    .setUpdateDesc(mCheckUpdateInfo.getNewAppUpdateDesc())
                    .setFileName("This is QQ.apk")
                    .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/checkupdatelib").show();
        }
    }

    /**
     * Unmandatory updates
     */
    public void UpdateDialogClick(View view) {
        mCheckUpdateInfo.setIsForceUpdate(1);
        if (mCheckUpdateInfo.getIsForceUpdate() == 1) {
            UpdateDialog dialog = new UpdateDialog(MainActivity.this);
            dialog.setAppSize(mCheckUpdateInfo.getNewAppSize())
                    .setDownloadUrl(mCheckUpdateInfo.getNewAppUrl())
                    .setTitle(mCheckUpdateInfo.getAppName() + "It's updated.")
                    .setReleaseTime(mCheckUpdateInfo.getNewAppReleaseTime())
                    .setVersionName(mCheckUpdateInfo.getNewAppVersionName())
                    .setUpdateDesc(mCheckUpdateInfo.getNewAppUpdateDesc())
                    .setFileName("This is QQ.apk")
                    .setFilePath(Environment.getExternalStorageDirectory().getPath() + "/checkupdatelib")
                    //This method needs to be set to true before the download progress can be displayed in the notification bar. The default is false, that is, it will not be displayed.
                    //This method only controls the display of download progress, and the notification displayed when the download is completed or the download fails is not affected by this method.
                    //Whether the method is set to false or true, a notification will be displayed in the notification bar when the download is completed or the download fails.
                    .setShowProgress(true)
                    .setIconResId(R.mipmap.ic_launcher)
                    .setAppName(mCheckUpdateInfo.getAppName()).show();
        }
    }

    /**
     * Get the current application version number
     */
    private int getVersionCode() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return packageInfo.versionCode;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return -1;
        }
    }
}

Here's an effect map:

Non-mandatory updates


Mandatory updates:



Finally, paste the github address of CheckUpdate Library again:

https://github.com/qiangxi/CheckUpdateLibrary

Keywords: Android network github JSON

Added by AShain on Wed, 29 May 2019 23:59:50 +0300