diff --git a/app/src/main/java/com/example/bpa/MainActivity.java b/app/src/main/java/com/example/bpa/MainActivity.java index 84b8e1e..d53f1bc 100644 --- a/app/src/main/java/com/example/bpa/MainActivity.java +++ b/app/src/main/java/com/example/bpa/MainActivity.java @@ -68,7 +68,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen //region 界面变量 //标题 - private TextView clist_title; + private TextView clist_title,version; //返回 云端 系统功能 系统设置 系统帮助 private ImageView CloudUpdates, SystemCapabilities, SystemSettings, SystemHelp, HomeMain, ColseMain, RealTimeMonitoring; //页面 @@ -122,6 +122,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen */ private void Init() { clist_title = findViewById(R.id.clist_title); + version= findViewById(R.id.version); CloudUpdates = findViewById(R.id.CloudUpdates); SystemCapabilities = findViewById(R.id.SystemCapabilities); SystemSettings = findViewById(R.id.SystemSettings); @@ -131,6 +132,7 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen fragment_container = findViewById(R.id.fragment_container); RealTimeMonitoring = findViewById(R.id.RealTimeMonitoring); clist_title.setText(ConfigName.getInstance().Shop_Name); + version.setText(ConfigName.getInstance().Version); ShowFragment(homeFragment, "系统主页"); DataBus.getInstance().UpdateMainGoods(); mUpdateManager = new UpdateManager(MainActivity.this); @@ -178,8 +180,8 @@ public class MainActivity extends FragmentActivity implements View.OnClickListen */ public void UpdateVersion() { //检测版本更新 - //Main.getInstance().UpdateVersion(); - mUpdateManager.checkVersion(null); + Main.getInstance().UpdateVersion(); + //mUpdateManager.checkVersion(null); } //endregion diff --git a/app/src/main/java/com/example/bpa/helper/DataHelper.java b/app/src/main/java/com/example/bpa/helper/DataHelper.java new file mode 100644 index 0000000..3b91509 --- /dev/null +++ b/app/src/main/java/com/example/bpa/helper/DataHelper.java @@ -0,0 +1,63 @@ +package com.example.bpa.helper; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +public class DataHelper { + + // formatType格式为yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 + // data Date类型的时间 + public static String dateToString(Date data, String formatType) { + return new SimpleDateFormat(formatType).format(data); + } + + // currentTime要转换的long类型的时间 + // formatType要转换的string类型的时间格式 + public static String longToString(long currentTime, String formatType) + throws ParseException { + Date date = longToDate(currentTime, formatType); // long类型转成Date类型 + String strTime = dateToString(date, formatType); // date类型转成String + return strTime; + } + + // strTime要转换的string类型的时间,formatType要转换的格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 + // HH时mm分ss秒, + // strTime的时间格式必须要与formatType的时间格式相同 + public static Date stringToDate(String strTime, String formatType) + throws ParseException { + SimpleDateFormat formatter = new SimpleDateFormat(formatType); + Date date = null; + date = formatter.parse(strTime); + return date; + } + + // currentTime要转换的long类型的时间 + // formatType要转换的时间格式yyyy-MM-dd HH:mm:ss//yyyy年MM月dd日 HH时mm分ss秒 + public static Date longToDate(long currentTime, String formatType) + throws ParseException { + Date dateOld = new Date(currentTime); // 根据long类型的毫秒数生命一个date类型的时间 + String sDateTime = dateToString(dateOld, formatType); // 把date类型的时间转换为string + Date date = stringToDate(sDateTime, formatType); // 把String类型转换为Date类型 + return date; + } + + // strTime要转换的String类型的时间 + // formatType时间格式 + // strTime的时间格式和formatType的时间格式必须相同 + public static long stringToLong(String strTime, String formatType) + throws ParseException { + Date date = stringToDate(strTime, formatType); // String类型转成date类型 + if (date == null) { + return 0; + } else { + long currentTime = dateToLong(date); // date类型转成long类型 + return currentTime; + } + } + + // date要转换的date类型的时间 + public static long dateToLong(Date date) { + return date.getTime(); + } +} diff --git a/app/src/main/java/com/example/bpa/helper/UpdateManager.java b/app/src/main/java/com/example/bpa/helper/UpdateManager.java index 3512cc9..da5ed90 100644 --- a/app/src/main/java/com/example/bpa/helper/UpdateManager.java +++ b/app/src/main/java/com/example/bpa/helper/UpdateManager.java @@ -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; - public MHandler(UpdateManager activity) - { + public MHandler(UpdateManager activity) { updatemanager = new WeakReference(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()); } } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7bbf60f..f99897a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -12,16 +12,29 @@ android:layout_width="match_parent" android:layout_height="35dp" android:background="@color/test"> - + android:layout_marginLeft="20dp"> + + + +