1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package cn.flightfeather.thirdapp.util.updateApp;
 
import android.app.Activity;
import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AlertDialog;
import android.widget.Toast;
 
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.bean.vo.VersionVo;
import cn.flightfeather.thirdapp.httpservice.VersionService;
import cn.flightfeather.thirdapp.util.GlobalConfig;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
 
/**
 * @author riku
 * app更新工具
 */
public class UpdateAppUtil {
 
    private VersionVo versionVo;
    private CommonApplication application;
    private Activity activity;
    private DownloadManager downloadManager;
    private long downloadId;
 
    public UpdateAppUtil(CommonApplication application, Activity activity) {
        this.application = application;
        this.activity = activity;
    }
 
    /**
     * 监测当前app版本是否最新
     */
    public void checkUpdateApp(final boolean needToast) {
        Call<VersionVo> getLatestVersion = application.getRetrofit().create(VersionService.class).getLatestVersion();
        getLatestVersion.enqueue(new Callback<VersionVo>() {
            @Override
            public void onResponse(Call<VersionVo> call, Response<VersionVo> response) {
                if (response.body() != null) {
                    versionVo = response.body();
                    int localVersion = getLocalVersion();
                    //比较服务器和本地的版本号大小
                    if (versionVo.getVersionCode() > localVersion && versionVo.isEnable()) {
                        // TODO: 2019/2/21
                        AlertDialog.Builder dialog = new AlertDialog.Builder(activity);
                        dialog.setMessage("有更新可用,是否更新?");
                        dialog.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                Toast.makeText(application, R.string.downloading, Toast.LENGTH_SHORT).show();
                                downLoadApk();
                            }
                        });
                        dialog.setNegativeButton(R.string.cancel, null);
                        dialog.show();
                    } else {
                        //设置中手动检查更新时,需要提示;开启app自动检查时不需要提示
                        if (needToast) {
                            Toast.makeText(application, "已是最新版本", Toast.LENGTH_SHORT).show();
                        }
                    } 
                }
            }
 
            @Override
            public void onFailure(Call<VersionVo> call, Throwable t) {
                Toast.makeText(application, "网络连接失败", Toast.LENGTH_SHORT).show();
            }
        });
    }
 
    public int getLocalVersion() {
        int localVersion = 0;
        String localVersionName = "";
        try {
            PackageInfo packageInfo = application.getApplicationContext()
                    .getPackageManager()
                    .getPackageInfo(application.getPackageName(), 0);
            localVersion = packageInfo.versionCode;
            localVersionName = packageInfo.versionName;
            GlobalConfig.getInstance().setVersionCode(localVersion).setVersionName(localVersionName);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return localVersion;
    }
 
    public void downLoadApk() {
        try {
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(versionVo.getDownloadUrl()));
            request.setTitle(application.getString(R.string.app_name));
            request.setDescription("正在下载...");
            request.setDestinationInExternalFilesDir(application, Environment.DIRECTORY_DOWNLOADS, "supervision.apk");
            downloadManager = (DownloadManager) application.getSystemService(Context.DOWNLOAD_SERVICE);
            downloadId = downloadManager.enqueue(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    public void removeDownLoad() {
        downloadManager.remove(downloadId);
    }
}