Skip to content

Commit c408114

Browse files
author
jingtian
committed
修改版本更新功能
1 parent b77199e commit c408114

File tree

3 files changed

+77
-60
lines changed

3 files changed

+77
-60
lines changed

app/src/main/java/luo/myapplication/MainActivity.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,6 @@ protected void onCreate(Bundle savedInstanceState) {
3434
setTitleText("BaseAndroid");
3535
hideBack();
3636

37-
//下载更新版本
38-
3937
}
4038

4139
@Event(R.id.btn_webview)

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

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -37,56 +37,33 @@ public static BaseConfig getBaseConfig() {
3737
* @param isForced 是否强制更新
3838
*/
3939
public static void checkUpdate(Context context, View view, int versionCode, String url, String updateMessage, boolean isForced) {
40-
if (versionCode > getVersionCode(context)) {
40+
if (versionCode > UpdateManager.getInstance().getVersionCode(context)) {
4141
int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
42-
if (isWifi(context)) {
42+
if (UpdateManager.getInstance().isWifi(context)) {
4343
type = 1;
4444
}
4545
if (isForced) {
4646
type = 2;
4747
}
48-
UpdateManager.getInstance().setType(type).setUrl(url).setUpdateMessage(updateMessage);
48+
//设置参数
49+
UpdateManager.getInstance().setView(view).setType(type).setUrl(url).setUpdateMessage(updateMessage);
4950
switch (type) {
5051
case 0:
51-
UpdateManager.getInstance().showPop(context, view);
52+
//非wifi情况下,先弹框后下载
53+
UpdateManager.getInstance().showPop(context);
5254
break;
5355
case 1:
54-
UpdateManager.getInstance().downloadFile(context, view, false);
56+
//wifi情况下,先下载后弹框
57+
UpdateManager.getInstance().downloadFile(context, false);
5558
break;
5659
case 2:
57-
UpdateManager.getInstance().showPop(context, view);
60+
//强制更新情况下,无论是否wifi都应该是先弹框
61+
UpdateManager.getInstance().showPop(context);
5862
break;
5963
}
6064
}
6165

6266
}
6367

64-
/**
65-
* @return 当前应用的版本号
66-
*/
67-
public static int getVersionCode(Context context) {
68-
try {
69-
PackageManager manager = context.getPackageManager();
70-
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
71-
int version = info.versionCode;
72-
return version;
73-
} catch (Exception e) {
74-
e.printStackTrace();
75-
return 0;
76-
}
77-
}
7868

79-
/**
80-
* 判断当前网络是否wifi
81-
*/
82-
private static boolean isWifi(Context mContext) {
83-
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
84-
.getSystemService(Context.CONNECTIVITY_SERVICE);
85-
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
86-
if (activeNetInfo != null
87-
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
88-
return true;
89-
}
90-
return false;
91-
}
9269
}

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

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
import android.app.NotificationManager;
55
import android.content.Context;
66
import android.content.Intent;
7+
import android.content.pm.PackageInfo;
8+
import android.content.pm.PackageManager;
9+
import android.net.ConnectivityManager;
10+
import android.net.NetworkInfo;
711
import android.net.Uri;
812
import android.os.Environment;
913
import android.text.TextUtils;
@@ -28,14 +32,15 @@
2832
import luo.library.base.base.BaseAndroid;
2933

3034
/**
31-
* Created by WIN7 on 2017/3/9.
35+
* 版本更新
3236
*/
3337

3438
public class UpdateManager {
3539
public String DOWNLOAD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/downloads/";
3640
public int type = 0;//更新方式,0:引导更新,1:安装更新,2:强制更新
3741
public String url = "";//apk下载地址
3842
public String updateMessage = "";//更新内容
43+
public View view;//activity根布局的view
3944
public String fileName = null;//文件名
4045
public Notification notification;
4146
public RemoteViews contentView;
@@ -50,6 +55,9 @@ public static UpdateManager getInstance() {
5055
return updateManager;
5156
}
5257

58+
private UpdateManager() {
59+
60+
}
5361

5462
public UpdateManager setUrl(String url) {
5563
this.url = url;
@@ -69,15 +77,24 @@ public UpdateManager setUpdateMessage(String updateMessage) {
6977
}
7078

7179

72-
public void showPop(final Context context, View view) {
80+
public UpdateManager setView(View view) {
81+
this.view = view;
82+
return this;
83+
}
84+
85+
/**
86+
* 弹出版本更新提示框
87+
*/
88+
public void showPop(final Context context) {
7389
View contentView = LayoutInflater.from(context).inflate(R.layout.base_dialog, null);
7490
final PopupWindow popupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
91+
final TextView title = (TextView) contentView.findViewById(R.id.tv_title);
7592
TextView left = (TextView) contentView.findViewById(R.id.tv_left);
7693
TextView right = (TextView) contentView.findViewById(R.id.tv_right);
77-
final TextView title = (TextView) contentView.findViewById(R.id.tv_title);
7894
TextView message = (TextView) contentView.findViewById(R.id.tv_message);
7995
RelativeLayout relativeLayout = (RelativeLayout) contentView.findViewById(R.id.rl);
8096

97+
//根据更新方式显示不同的文案
8198
if (type == 0) {
8299
title.setText("发现新版本");
83100
left.setText("立即更新");
@@ -87,14 +104,15 @@ public void showPop(final Context context, View view) {
87104
}
88105
right.setText("取消");
89106
message.setText(updateMessage);
107+
//升级按钮,根据更新方式来做不同的操作,0和2是下载apk并显示通知栏,1是直接安装apk
90108
left.setOnClickListener(new View.OnClickListener() {
91109
@Override
92110
public void onClick(View view) {
93111
popupWindow.dismiss();
94112
if (type == 0 | type == 2) {
95113
if (url != null && !TextUtils.isEmpty(url)) {
96114
createNotification(context);
97-
downloadFile(context, view, true);
115+
downloadFile(context, true);
98116
} else {
99117
Toast.makeText(context, "下载地址错误", Toast.LENGTH_SHORT).show();
100118
}
@@ -104,6 +122,7 @@ public void onClick(View view) {
104122

105123
}
106124
});
125+
//取消按钮,当更新方式为强制更新时,直接退出
107126
right.setOnClickListener(new View.OnClickListener() {
108127
@Override
109128
public void onClick(View view) {
@@ -113,6 +132,7 @@ public void onClick(View view) {
113132
}
114133
}
115134
});
135+
//点击外边区域消失窗口
116136
if (type != 2) {
117137
relativeLayout.setOnClickListener(new View.OnClickListener() {
118138
@Override
@@ -125,21 +145,28 @@ public void onClick(View view) {
125145
}
126146

127147

128-
public void downloadFile(final Context context, final View view, final boolean installApk) {
148+
/**
149+
* 下载apk
150+
* @param context
151+
* @param installApk 下载完成后是否需要安装
152+
*/
153+
public void downloadFile(final Context context, final boolean installApk) {
129154
File dir = new File(DOWNLOAD_PATH);
130155
if (!dir.exists()) {
131156
dir.mkdir();
132157
}
158+
//截取apk的文件名
133159
fileName = url.substring(url.lastIndexOf("/") + 1, url.length());
134160
if (fileName == null && TextUtils.isEmpty(fileName) && !fileName.contains(".apk")) {
135161
fileName = context.getPackageName() + ".apk";
136162
}
137-
File file=new File(DOWNLOAD_PATH + fileName);
138-
if (file.exists()){
163+
//判断是否存在了这个apk,存在了就不下载了,直接安装或者弹框提示
164+
File file = new File(DOWNLOAD_PATH + fileName);
165+
if (file.exists()) {
139166
if (installApk) {
140167
installApk(context, new File(DOWNLOAD_PATH, fileName));
141168
} else {
142-
showPop(context, view);
169+
showPop(context);
143170
}
144171
return;
145172
}
@@ -179,14 +206,15 @@ public void onStarted() {
179206

180207
@Override
181208
public void onLoading(long total, long current, boolean isDownloading) {
209+
//实时更新通知栏进度条
182210
if (type != 1) {
183211
notifyNotification(current, total);
184212
}
185213
if (total == current) {
186214
if (installApk) {
187215
installApk(context, new File(DOWNLOAD_PATH, fileName));
188216
} else {
189-
showPop(context, view);
217+
showPop(context);
190218
}
191219
}
192220
}
@@ -200,29 +228,14 @@ public void createNotification(Context context) {
200228
BaseAndroid.getBaseConfig().getAppLogo(),//应用的图标
201229
"安装包正在下载...",
202230
System.currentTimeMillis());
203-
notification.flags = Notification.FLAG_ONGOING_EVENT;
204-
//notification.flags = Notification.FLAG_AUTO_CANCEL;
205-
206231
/*** 自定义 Notification 的显示****/
207232
contentView = new RemoteViews(context.getPackageName(), R.layout.notification_item);
208233
contentView.setProgressBar(R.id.progress, 100, 0, false);
209234
contentView.setTextViewText(R.id.tv_progress, "0%");
210235
contentView.setImageViewResource(R.id.ic_logo, BaseAndroid.getBaseConfig().getAppLogo());
211236
notification.contentView = contentView;
212-
213-
/*updateIntent = new Intent(this, AboutActivity.class);
214-
updateIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
215-
updateIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
216-
pendingIntent = PendingIntent.getActivity(this, 0, updateIntent, 0);
217-
notification.contentIntent = pendingIntent;*/
218-
notification.flags = Notification.FLAG_AUTO_CANCEL;
237+
notification.flags = Notification.DEFAULT_ALL;
219238
notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
220-
221-
//设置notification的PendingIntent
222-
/*Intent intt = new Intent(this, MainActivity.class);
223-
PendingIntent pi = PendingIntent.getActivity(this,100, intt,Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
224-
notification.contentIntent = pi;*/
225-
226239
notificationManager.notify(R.layout.notification_item, notification);
227240
}
228241

@@ -247,4 +260,33 @@ public void installApk(Context context, File file) {
247260
context.startActivity(intent);
248261
}
249262

263+
/**
264+
* @return 当前应用的版本号
265+
*/
266+
public int getVersionCode(Context context) {
267+
try {
268+
PackageManager manager = context.getPackageManager();
269+
PackageInfo info = manager.getPackageInfo(context.getPackageName(), 0);
270+
int version = info.versionCode;
271+
return version;
272+
} catch (Exception e) {
273+
e.printStackTrace();
274+
return 0;
275+
}
276+
}
277+
278+
/**
279+
* 判断当前网络是否wifi
280+
*/
281+
public boolean isWifi(Context mContext) {
282+
ConnectivityManager connectivityManager = (ConnectivityManager) mContext
283+
.getSystemService(Context.CONNECTIVITY_SERVICE);
284+
NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
285+
if (activeNetInfo != null
286+
&& activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
287+
return true;
288+
}
289+
return false;
290+
}
291+
250292
}

0 commit comments

Comments
 (0)