UpdateChecker.java
7.2 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package com.hjx.personalcenter.update;
/**
* Created by l on 2017/7/17.
*/
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.os.ResultReceiver;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import com.hjx.personalcenter.http.HttpCode;
import com.hjx.personalcenter.http.HttpManager;
import com.hjx.personalcenter.model.AppVersion;
import com.mylhyl.circledialog.CircleDialog;
import java.io.File;
import java.util.ArrayList;
public class UpdateChecker{
private static final String TAG = "UpdateChecker";
private Context mContext;
//检查版本信息的线程
private Thread mThread;
private ArrayList<AppVersion> mAppVersion;
//下载apk的对话框
private ProgressDialog mProgressDialog;
private File apkFile;
private int visncode;
private String downUrl;
private String msgs;
private String qiangzi;
Handler handler = new Handler(){
public void handleMessage(Message msg) {
if (msg.what == HttpCode.APPUPDATE_SUCESS) {
mAppVersion = (ArrayList<AppVersion>) msg.obj;
for (AppVersion appVersion : mAppVersion){
visncode = appVersion.getVersioncode();
downUrl = appVersion.getUrl();
msgs = appVersion.getMsg();
qiangzi = appVersion.getForceupdate();
}
try{
int versionCode = mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
if (visncode > versionCode) {
if("true".equals(qiangzi)){
showForceUpdateDialog();
}else{
showUpdateDialog();
}
} else {
//Toast.makeText(mContext, "已经是最新版本", Toast.LENGTH_SHORT).show();
}
}catch (PackageManager.NameNotFoundException ignored) {
//
}
}
}
};
public UpdateChecker(Context context) {
mContext = context;
// instantiate it within the onCreate method
mProgressDialog = new ProgressDialog(context);
mProgressDialog.setMessage("正在下载");
mProgressDialog.setIndeterminate(false);
mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
mProgressDialog.setCancelable(true);
mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
}
});
mProgressDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialog) {
// TODO Auto-generated method stub
}
});
}
public void checkForUpdates() {
// mThread = new Thread() {
// @Override
// public void run() {
// try {
HttpManager.getInstance().updateAPP(mContext,"com.hjx.personalcenter","个人中心",handler);
// }catch (Exception e){
// e.printStackTrace();
// }
//
// }
// };
// mThread.start();
}
private void showForceUpdateDialog() {
new CircleDialog.Builder((FragmentActivity)mContext)
.setCanceledOnTouchOutside(false)
.setCancelable(false)
.setWidth(0.5f)
.setTitle("升级提示")
.setText(msgs)
.setNegative("取消", new View.OnClickListener() {
@Override
public void onClick(View v) {
((Activity)mContext).finish();
}
})
.setPositive("确定", new View.OnClickListener() {
@Override
public void onClick(View v) {
downLoadApk();
}
})
.show();
}
private void showUpdateDialog() {
new CircleDialog.Builder((FragmentActivity)mContext)
.setCanceledOnTouchOutside(false)
.setCancelable(false)
.setWidth(0.5f)
.setTitle("升级提示")
.setText(msgs)
.setNegative("取消", null)
.setPositive("确定", new View.OnClickListener() {
@Override
public void onClick(View v) {
downLoadApk();
}
})
.show();
}
private void downLoadApk() {
String apkUrl = downUrl;
String dir = mContext.getExternalFilesDir( "apk").getAbsolutePath();
File folder = Environment.getExternalStoragePublicDirectory(dir);
if(folder.exists() && folder.isDirectory()) {
//do nothing
}else {
folder.mkdirs();
}
String filename = apkUrl.substring(apkUrl.lastIndexOf("/"),apkUrl.length());
String destinationFilePath = dir + "/" + filename;
apkFile = new File(destinationFilePath);
if("true".equals(qiangzi)){
mProgressDialog.setCancelable(false);
}
mProgressDialog.show();
Intent intent = new Intent(mContext, DownloadService.class);
intent.putExtra("url", apkUrl);
intent.putExtra("dest", destinationFilePath);
intent.putExtra("receiver", new DownloadReceiver(new Handler()));
mContext.startService(intent);
}
private class DownloadReceiver extends ResultReceiver{
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
if (resultCode == DownloadService.UPDATE_PROGRESS) {
int progress = resultData.getInt("progress");
mProgressDialog.setProgress(progress);
if (progress == 100) {
mProgressDialog.dismiss();
//如果没有设置SDCard写权限,或者没有sdcard,apk文件保存在内存中,需要授予权限才能安装
String[] command = {"chmod","777",apkFile.toString()};
try{
ProcessBuilder builder = new ProcessBuilder(command);
builder.start();
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(apkFile), "application/vnd.android.package-archive");
mContext.startActivity(intent);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
}
}