44import android .app .NotificationManager ;
55import android .content .Context ;
66import 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 ;
711import android .net .Uri ;
812import android .os .Environment ;
913import android .text .TextUtils ;
2832import luo .library .base .base .BaseAndroid ;
2933
3034/**
31- * Created by WIN7 on 2017/3/9.
35+ * 版本更新
3236 */
3337
3438public 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