Skip to content

Commit 903cdb0

Browse files
author
jingtian
committed
修改版本更新功能
1 parent d203301 commit 903cdb0

File tree

3 files changed

+68
-91
lines changed

3 files changed

+68
-91
lines changed

library/src/main/java/luo/library/base/base/BaseAndroid.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public static void checkUpdate(Context context, int versionCode, String url, Str
6060
//设置参数
6161
UpdateManager.getInstance().setType(type).setUrl(url).setUpdateMessage(updateMessage).setFileName(fileName).setIsDownload(file.exists());
6262
if (type == 1 && !file.exists()) {
63-
UpdateManager.getInstance().downloadFile(context, false);
63+
UpdateManager.getInstance().downloadFile(context);
6464
} else {
6565
UpdateManager.getInstance().showDialog(context);
6666
}

library/src/main/java/luo/library/base/utils/UpdateManager.java

Lines changed: 67 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import android.app.Notification;
44
import android.app.NotificationManager;
5+
import android.app.ProgressDialog;
56
import android.content.Context;
67
import android.content.Intent;
78
import android.content.pm.PackageInfo;
@@ -10,9 +11,9 @@
1011
import android.net.NetworkInfo;
1112
import android.net.Uri;
1213
import android.os.Environment;
14+
import android.support.v7.app.NotificationCompat;
1315
import android.text.TextUtils;
1416
import android.view.View;
15-
import android.widget.RemoteViews;
1617
import android.widget.Toast;
1718

1819
import org.xutils.common.Callback;
@@ -22,25 +23,26 @@
2223

2324
import java.io.File;
2425

25-
import luo.library.R;
2626
import luo.library.base.base.BaseAndroid;
2727
import luo.library.base.widget.BaseDialog;
2828

29+
import static android.content.Context.NOTIFICATION_SERVICE;
30+
2931
/**
3032
* 版本更新
3133
*/
3234

3335
public class UpdateManager {
34-
public String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
35-
public int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
36-
public String url = "";//apk下载地址
37-
public String updateMessage = "";//更新内容
38-
public String fileName = null;//文件名
39-
public boolean isDownload = false;//是否下载
40-
public Notification notification;
41-
public RemoteViews contentView;
42-
public NotificationManager notificationManager;
43-
public BaseDialog dialog;
36+
private String downLoadPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
37+
private int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
38+
private String url = "";//apk下载地址
39+
private String updateMessage = "";//更新内容
40+
private String fileName = null;//文件名
41+
private boolean isDownload = false;//是否下载
42+
private NotificationManager mNotifyManager;
43+
private NotificationCompat.Builder mBuilder;
44+
private BaseDialog dialog;
45+
private ProgressDialog progressDialog;
4446

4547
public static UpdateManager updateManager;
4648

@@ -81,8 +83,12 @@ public void onClick(View view) {
8183
installApk(context, new File(downLoadPath, fileName));
8284
} else {
8385
if (url != null && !TextUtils.isEmpty(url)) {
84-
createNotification(context);
85-
downloadFile(context, true);
86+
if (type == 2) {
87+
createProgress(context);
88+
} else {
89+
createNotification(context);
90+
}
91+
downloadFile(context);
8692
} else {
8793
Toast.makeText(context, "下载地址错误", Toast.LENGTH_SHORT).show();
8894
}
@@ -106,10 +112,8 @@ public void onClick(View view) {
106112
/**
107113
* 下载apk
108114
*
109-
* @param context
110-
* @param installApk 下载完成后是否立即安装
111115
*/
112-
public void downloadFile(final Context context, final boolean installApk) {
116+
public void downloadFile(final Context context) {
113117
RequestParams params = new RequestParams(url);
114118
params.setSaveFilePath(downLoadPath + fileName);
115119
x.http().request(HttpMethod.GET, params, new Callback.ProgressCallback<File>() {
@@ -147,44 +151,64 @@ public void onStarted() {
147151
@Override
148152
public void onLoading(long total, long current, boolean isDownloading) {
149153
//实时更新通知栏进度条
150-
if (type != 1) {
154+
if (type == 0) {
151155
notifyNotification(current, total);
156+
} else if (type == 2) {
157+
progressDialog.setProgress((int) (current * 100 / total));
152158
}
153159
if (total == current) {
154-
notificationManager.cancelAll();
155-
if (installApk) {
156-
installApk(context, new File(downLoadPath, fileName));
157-
} else {
160+
if (type == 0) {
161+
mBuilder.setContentText("下载完成");
162+
mNotifyManager.notify(10086, mBuilder.build());
163+
} else if (type == 2) {
164+
progressDialog.setMessage("下载完成");
165+
}
166+
if (type == 1) {
158167
showDialog(context);
168+
} else {
169+
installApk(context, new File(downLoadPath, fileName));
159170
}
160171
}
161172
}
162173
});
163174
}
164175

176+
/**
177+
* 强制更新时显示在屏幕的进度条
178+
*
179+
*/
180+
private void createProgress(Context context) {
181+
progressDialog = new ProgressDialog(context);
182+
progressDialog.setMax(100);
183+
progressDialog.setCancelable(false);
184+
progressDialog.setMessage("正在下载...");
185+
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
186+
progressDialog.show();
187+
}
165188

166-
@SuppressWarnings("deprecation")
167-
public void createNotification(Context context) {
168-
notification = new Notification(
169-
BaseAndroid.getBaseConfig().getAppLogo(),//应用的图标
170-
"安装包正在下载...",
171-
System.currentTimeMillis());
172-
/*** 自定义 Notification 的显示****/
173-
contentView = new RemoteViews(context.getPackageName(), R.layout.notification_item);
174-
contentView.setProgressBar(R.id.progress, 100, 0, false);
175-
contentView.setTextViewText(R.id.tv_progress, "0%");
176-
contentView.setImageViewResource(R.id.ic_logo, BaseAndroid.getBaseConfig().getAppLogo());
177-
notification.contentView = contentView;
178-
notification.flags = Notification.DEFAULT_ALL;
179-
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
180-
notificationManager.notify(0, notification);
189+
/**
190+
* 创建通知栏进度条
191+
*
192+
*/
193+
private void createNotification(Context context) {
194+
mNotifyManager = (NotificationManager) context.getSystemService(NOTIFICATION_SERVICE);
195+
mBuilder = new NotificationCompat.Builder(context);
196+
mBuilder.setSmallIcon(BaseAndroid.getBaseConfig().getAppLogo());
197+
mBuilder.setContentTitle("版本更新");
198+
mBuilder.setContentText("正在下载...");
199+
mBuilder.setProgress(0, 0, false);
200+
Notification notification = mBuilder.build();
201+
notification.flags = Notification.FLAG_AUTO_CANCEL;
202+
mNotifyManager.notify(10086, notification);
181203
}
182204

183-
public void notifyNotification(long percent, long length) {
184-
contentView.setTextViewText(R.id.tv_progress, (percent * 100 / length) + "%");
185-
contentView.setProgressBar(R.id.progress, (int) length, (int) percent, false);
186-
notification.contentView = contentView;
187-
notificationManager.notify(0, notification);
205+
/**
206+
* 更新通知栏进度条
207+
*
208+
*/
209+
private void notifyNotification(long percent, long length) {
210+
mBuilder.setProgress((int) length, (int) percent, false);
211+
mNotifyManager.notify(10086, mBuilder.build());
188212
}
189213

190214
/**
@@ -193,7 +217,7 @@ public void notifyNotification(long percent, long length) {
193217
* @param context 上下文
194218
* @param file APK文件
195219
*/
196-
public void installApk(Context context, File file) {
220+
private void installApk(Context context, File file) {
197221
Intent intent = new Intent();
198222
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
199223
intent.setAction(Intent.ACTION_VIEW);

library/src/main/res/layout/notification_item.xml

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)