|
|
@@ -30,6 +30,7 @@ import com.example.bpa.message.MessageLooper; |
|
|
|
import com.example.bpa.message.MessageManager; |
|
|
|
|
|
|
|
import org.json.JSONObject; |
|
|
|
|
|
|
|
import java.io.File; |
|
|
|
import java.io.FileNotFoundException; |
|
|
|
import java.io.FileOutputStream; |
|
|
@@ -50,7 +51,7 @@ public class UpdateManager { |
|
|
|
private Context mContext; |
|
|
|
|
|
|
|
/* 下载包安装路径 */ |
|
|
|
private static final String savePath = ConfigName.getInstance().appResRoot+"/Download/"; |
|
|
|
private static final String savePath = ConfigName.getInstance().appResRoot + "/Download/"; |
|
|
|
|
|
|
|
private static final int NEW_VERSION = 3; |
|
|
|
|
|
|
@@ -62,7 +63,7 @@ public class UpdateManager { |
|
|
|
private ColorfulProgressbar mProgress; |
|
|
|
|
|
|
|
/* 新版本apk的远程路径 */ |
|
|
|
private String apkUrl ; |
|
|
|
private String apkUrl; |
|
|
|
|
|
|
|
/* 保存下载的apk的名称为 */ |
|
|
|
private String saveFileName; |
|
|
@@ -75,27 +76,30 @@ public class UpdateManager { |
|
|
|
/* |
|
|
|
* Handler静态化,防止内存泄露 |
|
|
|
*/ |
|
|
|
private static class MHandler extends Handler |
|
|
|
{ |
|
|
|
private static class MHandler extends Handler { |
|
|
|
/* 引用外部类 */ |
|
|
|
private WeakReference<UpdateManager> updatemanager; |
|
|
|
|
|
|
|
public MHandler(UpdateManager activity) |
|
|
|
{ |
|
|
|
public MHandler(UpdateManager activity) { |
|
|
|
updatemanager = new WeakReference<UpdateManager>(activity); |
|
|
|
} |
|
|
|
|
|
|
|
/* 处理线程结果 */ |
|
|
|
@Override |
|
|
|
public void handleMessage(android.os.Message msg) |
|
|
|
{ |
|
|
|
public void handleMessage(android.os.Message msg) { |
|
|
|
UpdateManager theClass = updatemanager.get(); |
|
|
|
switch (msg.what) |
|
|
|
{ |
|
|
|
switch (msg.what) { |
|
|
|
//对比后有新版本 |
|
|
|
case NEW_VERSION: |
|
|
|
JSONObject jsonObject=(JSONObject)msg.obj; |
|
|
|
theClass.showUpdateDialog(jsonObject); |
|
|
|
if (Version != null) |
|
|
|
{ |
|
|
|
VersionResponse jsonObject = (VersionResponse) msg.obj; |
|
|
|
theClass.showUpdateDialogNew(jsonObject); |
|
|
|
}else |
|
|
|
{ |
|
|
|
JSONObject jsonObject=(JSONObject)msg.obj; |
|
|
|
theClass.showUpdateDialog(jsonObject); |
|
|
|
} |
|
|
|
break; |
|
|
|
//正在下载,更新进度条 |
|
|
|
case DOWN_UPDATE: |
|
|
@@ -192,81 +196,81 @@ public class UpdateManager { |
|
|
|
} |
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 检查新版本 |
|
|
|
*/ |
|
|
|
public void checkVersion(VersionResponse mode) |
|
|
|
{ |
|
|
|
// if(compareVersions(mode.versionNo,ConfigName.getInstance().Version))//需要更新 |
|
|
|
// { |
|
|
|
// Message message = mHandler.obtainMessage(NEW_VERSION); |
|
|
|
// message.obj = mode; |
|
|
|
// mHandler.sendMessage(message); |
|
|
|
// }else //不需要更新 |
|
|
|
// { |
|
|
|
// T.show(mContext,"当前无更新内容!"); |
|
|
|
// } |
|
|
|
android.util.Log.i("UpdateManager","checkVersion"); |
|
|
|
|
|
|
|
Runnable checkVersionRunnable = new Runnable(){ |
|
|
|
@Override |
|
|
|
public void run() |
|
|
|
public static VersionResponse Version = null; |
|
|
|
|
|
|
|
public void checkVersion(VersionResponse mode) { |
|
|
|
Version = mode; |
|
|
|
if (Version != null)//第二代 |
|
|
|
{ |
|
|
|
if (compareVersions(mode.versionNo, ConfigName.getInstance().Version))//需要更新 |
|
|
|
{ |
|
|
|
try |
|
|
|
{ |
|
|
|
//远程请求服务器,获取远程服务器上的apk版本;这里我们直接模拟返回的结果 |
|
|
|
JSONObject data = new JSONObject("{" |
|
|
|
+ "\"status\":1," |
|
|
|
+ "\"msg\":{" |
|
|
|
+ "\"versionCode\":2," |
|
|
|
+ "\"versionName\":\"1.1.7\"," |
|
|
|
+ "\"versionUrl\":\"http://124.222.238.75/blnc1.apk\"," |
|
|
|
+ "\"versionDesc\":\"1,修复商品制作;2,优化商品配方管理;3,增加云版本更新;4,增加针对信息推送功能;\"," |
|
|
|
+ "\"versionDate\":\"2023.06.01\"," |
|
|
|
+ "\"versionSize\":\"17.4M\"}}"); |
|
|
|
if(data.length()>0) //有返回数据,则发送消息 |
|
|
|
{ |
|
|
|
int status = data.getInt("status"); |
|
|
|
if(status==1) |
|
|
|
Message message = mHandler.obtainMessage(NEW_VERSION); |
|
|
|
message.obj = mode; |
|
|
|
saveFileName = "hlnc_" + mode.versionNo + ".apk"; |
|
|
|
apkUrl = mode.downloadLink; |
|
|
|
mHandler.sendMessage(message); |
|
|
|
} else //不需要更新 |
|
|
|
{ |
|
|
|
T.show(mContext, "当前无更新内容!"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
Runnable checkVersionRunnable = new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
try { |
|
|
|
//远程请求服务器,获取远程服务器上的apk版本;这里我们直接模拟返回的结果 |
|
|
|
JSONObject data = new JSONObject("{" |
|
|
|
+ "\"status\":1," |
|
|
|
+ "\"msg\":{" |
|
|
|
+ "\"versionCode\":2," |
|
|
|
+ "\"versionName\":\"1.1.7\"," |
|
|
|
+ "\"versionUrl\":\"http://124.222.238.75/blnc1.apk\"," |
|
|
|
+ "\"versionDesc\":\"1,修复商品制作;2,优化商品配方管理;3,增加云版本更新;4,增加针对信息推送功能;\"," |
|
|
|
+ "\"versionDate\":\"2023.06.01\"," |
|
|
|
+ "\"versionSize\":\"17.4M\"}}"); |
|
|
|
if (data.length() > 0) //有返回数据,则发送消息 |
|
|
|
{ |
|
|
|
JSONObject newVersionInfo = data.getJSONObject("msg"); |
|
|
|
int versionCode = newVersionInfo.getInt("versionCode"); |
|
|
|
String versionName = newVersionInfo.getString("versionName"); |
|
|
|
saveFileName = "hlnc_"+versionName+".apk"; |
|
|
|
apkUrl = newVersionInfo.getString("versionUrl"); |
|
|
|
int versionCodeClient = Utils.getversionCode(mContext); |
|
|
|
if(versionCode>versionCodeClient) //服务器版本大于本机版本,则视为有新版本 |
|
|
|
{ |
|
|
|
Message message = mHandler.obtainMessage(NEW_VERSION); |
|
|
|
message.obj = newVersionInfo; |
|
|
|
mHandler.sendMessage(message); |
|
|
|
}else |
|
|
|
{ |
|
|
|
T.show(mContext,"当前无更新内容!"); |
|
|
|
int status = data.getInt("status"); |
|
|
|
if (status == 1) { |
|
|
|
JSONObject newVersionInfo = data.getJSONObject("msg"); |
|
|
|
int versionCode = newVersionInfo.getInt("versionCode"); |
|
|
|
String versionName = newVersionInfo.getString("versionName"); |
|
|
|
saveFileName = "hlnc_" + versionName + ".apk"; |
|
|
|
apkUrl = newVersionInfo.getString("versionUrl"); |
|
|
|
int versionCodeClient = Utils.getversionCode(mContext); |
|
|
|
if (versionCode > versionCodeClient) //服务器版本大于本机版本,则视为有新版本 |
|
|
|
{ |
|
|
|
Message message = mHandler.obtainMessage(NEW_VERSION); |
|
|
|
message.obj = newVersionInfo; |
|
|
|
mHandler.sendMessage(message); |
|
|
|
} else { |
|
|
|
T.show(mContext, "当前无更新内容!"); |
|
|
|
} |
|
|
|
} else { |
|
|
|
T.show(mContext, "当前无更新内容!"); |
|
|
|
} |
|
|
|
}else |
|
|
|
{ |
|
|
|
T.show(mContext,"当前无更新内容!"); |
|
|
|
} else { |
|
|
|
T.show(mContext, "当前无更新内容!"); |
|
|
|
} |
|
|
|
}else |
|
|
|
{ |
|
|
|
T.show(mContext,"当前无更新内容!"); |
|
|
|
} catch (Exception e) { |
|
|
|
android.util.Log.i("UpdateManager", e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
catch(Exception e) |
|
|
|
{ |
|
|
|
android.util.Log.i("UpdateManager",e.toString()); |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
new Thread(checkVersionRunnable).start();; |
|
|
|
}; |
|
|
|
new Thread(checkVersionRunnable).start(); |
|
|
|
; |
|
|
|
} |
|
|
|
android.util.Log.i("UpdateManager", "checkVersion"); |
|
|
|
} |
|
|
|
|
|
|
|
/* 显示进度条 |
|
|
|
* 外部类进行封装以便MHandler进行内部调用 |
|
|
|
*/ |
|
|
|
public void showProgress() |
|
|
|
{ |
|
|
|
public void showProgress() { |
|
|
|
mProgress.setProgress(progress); |
|
|
|
mProgress.setSecondProgress(progress); |
|
|
|
} |
|
|
@@ -274,26 +278,67 @@ public class UpdateManager { |
|
|
|
/* |
|
|
|
* 显示版本更新提示框 |
|
|
|
*/ |
|
|
|
private void showUpdateDialog(JSONObject newVersionInfo){ |
|
|
|
private void showUpdateDialog(JSONObject newVersionInfo) { |
|
|
|
final AlertDialog alg = new AlertDialog.Builder(mContext).create(); |
|
|
|
alg.show(); |
|
|
|
Window win = alg.getWindow(); |
|
|
|
win.setContentView(R.layout.common_dialog_update); |
|
|
|
|
|
|
|
//显示新版本内容 |
|
|
|
TextView title_update = (TextView) win.findViewById(R.id.title_update); |
|
|
|
TextView title_date = (TextView) win.findViewById(R.id.title_date); |
|
|
|
TextView title_size = (TextView) win.findViewById(R.id.title_size); |
|
|
|
TextView versionDesc = (TextView) win.findViewById(R.id.versionDesc); |
|
|
|
try { |
|
|
|
title_update.setText(newVersionInfo.getString("versionName") + "版本升级提示"); |
|
|
|
title_date.setText("发布时间:" + newVersionInfo.getString("versionDate")); |
|
|
|
title_size.setText("大小:" + newVersionInfo.getString("versionSize")); |
|
|
|
//提示内容按";"进行整合,这里拆分到数组中 |
|
|
|
String desc = newVersionInfo.getString("versionDesc").replace(";", "\n"); |
|
|
|
versionDesc.setText(desc); |
|
|
|
Button positiveButton = (Button) win.findViewById(R.id.PositiveButton); |
|
|
|
Button negativeButton = (Button) win.findViewById(R.id.NegativeButton); |
|
|
|
positiveButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
alg.dismiss(); |
|
|
|
showDownloadDialog(); |
|
|
|
} |
|
|
|
}); |
|
|
|
negativeButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
|
// TODO Auto-generated method stub |
|
|
|
alg.dismiss(); |
|
|
|
} |
|
|
|
}); |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void showUpdateDialogNew(VersionResponse newVersionInfo) { |
|
|
|
final AlertDialog alg = new AlertDialog.Builder(mContext).create(); |
|
|
|
alg.show(); |
|
|
|
Window win = alg.getWindow(); |
|
|
|
win.setContentView(R.layout.common_dialog_update); |
|
|
|
|
|
|
|
//显示新版本内容 |
|
|
|
TextView title_update = (TextView)win.findViewById(R.id.title_update); |
|
|
|
TextView title_date = (TextView)win.findViewById(R.id.title_date); |
|
|
|
TextView title_size = (TextView)win.findViewById(R.id.title_size); |
|
|
|
TextView versionDesc = (TextView)win.findViewById(R.id.versionDesc); |
|
|
|
try{ |
|
|
|
title_update.setText(newVersionInfo.getString("versionName")+"版本升级提示"); |
|
|
|
title_date.setText("发布时间:"+newVersionInfo.getString("versionDate")); |
|
|
|
title_size.setText("大小:"+newVersionInfo.getString("versionSize")); |
|
|
|
TextView title_update = (TextView) win.findViewById(R.id.title_update); |
|
|
|
TextView title_date = (TextView) win.findViewById(R.id.title_date); |
|
|
|
TextView title_size = (TextView) win.findViewById(R.id.title_size); |
|
|
|
TextView versionDesc = (TextView) win.findViewById(R.id.versionDesc); |
|
|
|
try { |
|
|
|
VersionResponse mode = new VersionResponse(); |
|
|
|
title_update.setText(newVersionInfo.versionNo + "版本升级提示"); |
|
|
|
title_date.setText("发布时间:" + DataHelper.longToString(Long.parseLong(newVersionInfo.releaseDate), "yyyy-MM-dd HH:mm:ss")); |
|
|
|
//title_size.setText("大小:"+newVersionInfo.getString("versionSize")); |
|
|
|
//提示内容按";"进行整合,这里拆分到数组中 |
|
|
|
String desc= newVersionInfo.getString("versionDesc").replace(";","\n"); |
|
|
|
String desc = newVersionInfo.description.replace(";", "\n"); |
|
|
|
versionDesc.setText(desc); |
|
|
|
Button positiveButton = (Button)win.findViewById(R.id.PositiveButton); |
|
|
|
Button negativeButton = (Button)win.findViewById(R.id.NegativeButton); |
|
|
|
Button positiveButton = (Button) win.findViewById(R.id.PositiveButton); |
|
|
|
Button negativeButton = (Button) win.findViewById(R.id.NegativeButton); |
|
|
|
positiveButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
@@ -309,7 +354,7 @@ public class UpdateManager { |
|
|
|
alg.dismiss(); |
|
|
|
} |
|
|
|
}); |
|
|
|
}catch(Exception e){ |
|
|
|
} catch (Exception e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
@@ -317,15 +362,15 @@ public class UpdateManager { |
|
|
|
/* |
|
|
|
* 显示下载中界面 |
|
|
|
*/ |
|
|
|
private void showDownloadDialog(){ |
|
|
|
private void showDownloadDialog() { |
|
|
|
final AlertDialog alg = new AlertDialog.Builder(mContext).create(); |
|
|
|
alg.show(); |
|
|
|
Window win = alg.getWindow(); |
|
|
|
win.setContentView(R.layout.common_dialog_progress); |
|
|
|
mProgress = (ColorfulProgressbar)win.findViewById(R.id.update_progress); |
|
|
|
mProgress = (ColorfulProgressbar) win.findViewById(R.id.update_progress); |
|
|
|
mProgress.showPercentText(true); |
|
|
|
mProgress.setAnimation(true); |
|
|
|
Button negativeButton = (Button)win.findViewById(R.id.NegativeButton); |
|
|
|
Button negativeButton = (Button) win.findViewById(R.id.NegativeButton); |
|
|
|
negativeButton.setOnClickListener(new View.OnClickListener() { |
|
|
|
@Override |
|
|
|
public void onClick(View v) { |
|
|
@@ -341,13 +386,13 @@ public class UpdateManager { |
|
|
|
* 下载apk |
|
|
|
* @param url |
|
|
|
*/ |
|
|
|
private void downloadApk(){ |
|
|
|
private void downloadApk() { |
|
|
|
Runnable mdownApkRunnable = new Runnable() { |
|
|
|
@Override |
|
|
|
public void run() { |
|
|
|
try { |
|
|
|
URL url = new URL(apkUrl); |
|
|
|
HttpURLConnection conn = (HttpURLConnection)url.openConnection(); |
|
|
|
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); |
|
|
|
conn.setRequestProperty("Accept-Encoding", "identity"); |
|
|
|
conn.connect(); |
|
|
|
int length = conn.getContentLength(); |
|
|
@@ -358,31 +403,31 @@ public class UpdateManager { |
|
|
|
{ |
|
|
|
file.mkdirs(); |
|
|
|
} |
|
|
|
File ApkFile = new File(file,saveFileName); |
|
|
|
File ApkFile = new File(file, saveFileName); |
|
|
|
FileOutputStream fos = new FileOutputStream(ApkFile); |
|
|
|
int count = 0; |
|
|
|
byte buf[] = new byte[1024]; |
|
|
|
|
|
|
|
do{ |
|
|
|
do { |
|
|
|
int numread = is.read(buf); |
|
|
|
count += numread; |
|
|
|
progress =(int)(((float)count / length) * 100); |
|
|
|
progress = (int) (((float) count / length) * 100); |
|
|
|
//更新进度 |
|
|
|
mHandler.sendEmptyMessage(DOWN_UPDATE); |
|
|
|
if(numread <= 0){ |
|
|
|
if (numread <= 0) { |
|
|
|
//下载完成通知安装 |
|
|
|
mHandler.sendEmptyMessage(DOWN_OVER); |
|
|
|
T.show(mContext,"下载完成,正在安装..."); |
|
|
|
T.show(mContext, "下载完成,正在安装..."); |
|
|
|
break; |
|
|
|
} |
|
|
|
fos.write(buf,0,numread); |
|
|
|
}while(!interceptFlag); //点击取消就停止下载. |
|
|
|
fos.write(buf, 0, numread); |
|
|
|
} while (!interceptFlag); //点击取消就停止下载. |
|
|
|
fos.close(); |
|
|
|
is.close(); |
|
|
|
} catch (MalformedURLException e) { |
|
|
|
android.util.Log.i("MalformedURLException",e.toString()); |
|
|
|
} catch(IOException e){ |
|
|
|
android.util.Log.i("IOException",e.toString()); |
|
|
|
android.util.Log.i("MalformedURLException", e.toString()); |
|
|
|
} catch (IOException e) { |
|
|
|
android.util.Log.i("IOException", e.toString()); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
@@ -394,7 +439,7 @@ public class UpdateManager { |
|
|
|
* 安装apk |
|
|
|
* @param url |
|
|
|
*/ |
|
|
|
private void installApk(){ |
|
|
|
private void installApk() { |
|
|
|
try { |
|
|
|
File apkfile = new File(savePath + saveFileName); |
|
|
|
if (!apkfile.exists()) { |
|
|
@@ -406,13 +451,15 @@ public class UpdateManager { |
|
|
|
Uri uri = Uri.parse("file://" + apkfile.toString()); |
|
|
|
i.setDataAndType(uri, "application/vnd.android.package-archive"); |
|
|
|
mContext.startActivity(i); |
|
|
|
} catch(Exception e){ |
|
|
|
T.show(mContext,"错误信息,正在安装..."+e.getMessage()); |
|
|
|
} catch (Exception e) { |
|
|
|
T.show(mContext, "错误信息,正在安装..." + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void installApkNew(){ |
|
|
|
private void installApkNew() { |
|
|
|
try { |
|
|
|
ConfigName.getInstance().Version=Version.versionNo; |
|
|
|
Main.getInstance().SavePZ(); |
|
|
|
|
|
|
|
File apkfile = new File(savePath + saveFileName); |
|
|
|
Intent intent = new Intent(); |
|
|
@@ -429,8 +476,8 @@ public class UpdateManager { |
|
|
|
} |
|
|
|
intent.setDataAndType(uri, "application/vnd.android.package-archive"); |
|
|
|
mContext.startActivity(intent); |
|
|
|
} catch(Exception e){ |
|
|
|
T.show(mContext,"错误信息,正在安装..."+e.getMessage()); |
|
|
|
} catch (Exception e) { |
|
|
|
T.show(mContext, "错误信息,正在安装..." + e.getMessage()); |
|
|
|
} |
|
|
|
} |
|
|
|
} |