Browse Source

app启动时自动检测新版本,若有新版本则提示并且开始倒计时,倒计时结束则直接升级

weizhengliang 3 năm trước cách đây
mục cha
commit
b89114f698

+ 14 - 8
callingdoor/src/main/java/com/wdkl/app/ncs/callingdoor/activity/CallingdoorActivity.kt

@@ -571,8 +571,8 @@ class CallingdoorActivity :BaseActivity<CallingdoorActivityPresenter, Callingdoo
 
 
 
 
         //检查版本
         //检查版本
-        //Constant.silentUpdate = true
-        //checkAppVersion()
+        Constant.silentUpdate = true
+        checkAppVersion()
     }
     }
 
 
     override fun setPartSettings(partSetting: PartSettingDO) {
     override fun setPartSettings(partSetting: PartSettingDO) {
@@ -638,15 +638,21 @@ class CallingdoorActivity :BaseActivity<CallingdoorActivityPresenter, Callingdoo
         showMessage("获取版本成功,当前版本: " + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE + ", 服务器版本: " + newAppVersion + "_" + appInfo.versionNo)
         showMessage("获取版本成功,当前版本: " + BuildConfig.VERSION_NAME + "_" + BuildConfig.VERSION_CODE + ", 服务器版本: " + newAppVersion + "_" + appInfo.versionNo)
 
 
         if (BuildConfig.VERSION_CODE < appInfo.versionNo) {
         if (BuildConfig.VERSION_CODE < appInfo.versionNo) {
-            if (!Constant.APP_UPDATING) {
-                showMessage("正在升级...")
+            if (Constant.silentUpdate) {
                 Constant.APP_UPDATING = true
                 Constant.APP_UPDATING = true
                 Constant.APP_PATH = appInfo.appPath
                 Constant.APP_PATH = appInfo.appPath
-                AppTool.Time.delay(200) {
-                    push("/callingdoor/update")
-                }
+                UpdateTipsDialogHelper.showDialog(activity)
             } else {
             } else {
-                showMessage("正在升级中...")
+                if (!Constant.APP_UPDATING) {
+                    showMessage("正在升级...")
+                    Constant.APP_UPDATING = true
+                    Constant.APP_PATH = appInfo.appPath
+                    AppTool.Time.delay(200) {
+                        push("/callingdoor/update")
+                    }
+                } else {
+                    showMessage("正在升级中...")
+                }
             }
             }
         } else {
         } else {
             showMessage("当前已是最新版本")
             showMessage("当前已是最新版本")

+ 99 - 0
callingdoor/src/main/java/com/wdkl/app/ncs/callingdoor/helper/UpdateTipsDialogHelper.java

@@ -0,0 +1,99 @@
+package com.wdkl.app.ncs.callingdoor.helper;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.Intent;
+import android.os.CountDownTimer;
+import android.view.Gravity;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.Window;
+import android.view.WindowManager;
+import android.widget.Button;
+
+import com.wdkl.app.ncs.callingdoor.R;
+import com.wdkl.app.ncs.callingdoor.activity.AppUpdateActivity;
+import com.wdkl.ncs.android.middleware.common.Constant;
+
+public class UpdateTipsDialogHelper {
+
+    private static AlertDialog dialog;
+
+    public static void showDialog(Activity activity) {
+        if (dialog != null && dialog.isShowing()) {
+            return;
+        }
+
+        View contentView = LayoutInflater.from(activity).inflate(R.layout.update_tips_dialog, null);
+        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+        builder.setView(contentView);
+        Button buttonCancel = contentView.findViewById(R.id.cancel_button);
+        Button buttonConfirm = contentView.findViewById(R.id.confirm_button);
+        final CountDownTimer timer = new CountDownTimer(15000, 1000) {
+
+            @Override
+            public void onTick(long millisUntilFinished) {
+                buttonConfirm.setText("确定(" + millisUntilFinished/1000 + "s)");
+            }
+
+            @Override
+            public void onFinish() {
+                Intent intent = new Intent();
+                intent.setClass(activity, AppUpdateActivity.class);
+                activity.startActivity(intent);
+
+                if (dialog != null) {
+                    dialog.dismiss();
+                }
+            }
+        };
+        timer.start();
+
+        buttonCancel.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                Constant.APP_UPDATING = false;
+                if (timer != null) {
+                    timer.cancel();
+                }
+                if (dialog != null) {
+                    dialog.dismiss();
+                }
+            }
+        });
+
+        buttonConfirm.setOnClickListener(new View.OnClickListener() {
+            @Override
+            public void onClick(View v) {
+                if (timer != null) {
+                    timer.cancel();
+                }
+
+                Intent intent = new Intent();
+                intent.setClass(activity, AppUpdateActivity.class);
+                activity.startActivity(intent);
+
+                if (dialog != null) {
+                    dialog.dismiss();
+                }
+            }
+        });
+
+        dialog = builder.create();
+        dialog.setCanceledOnTouchOutside(false);
+        dialog.setCancelable(false);
+        dialog.show();
+
+        //设置dialog宽高及位置
+        try {
+            Window window = dialog.getWindow();
+            WindowManager.LayoutParams lp = window.getAttributes();
+            lp.width = 600;
+            lp.height = 320;
+            lp.gravity = Gravity.CENTER;
+            window.setAttributes(lp);
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+}

+ 41 - 0
callingdoor/src/main/res/layout/update_tips_dialog.xml

@@ -0,0 +1,41 @@
+<?xml version="1.0" encoding="utf-8"?>
+<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="match_parent"
+    android:gravity="center"
+    android:orientation="vertical">
+
+    <TextView
+        android:id="@+id/update_tip_text"
+        android:layout_width="match_parent"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="20dp"
+        android:padding="8dp"
+        android:text="检测到新版本, 是否升级?"
+        android:textColor="@color/red_color"
+        android:textSize="32sp"
+        android:gravity="center" />
+
+    <Button
+        android:id="@+id/confirm_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="32dp"
+        android:background="@mipmap/bg_bottom_btn"
+        android:padding="8dp"
+        android:text="确定"
+        android:textSize="20sp"
+        android:textColor="@drawable/selector_bottom_btn_text_color"/>
+
+    <Button
+        android:id="@+id/cancel_button"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:layout_marginTop="32dp"
+        android:background="@mipmap/bg_bottom_btn"
+        android:padding="8dp"
+        android:text="取消"
+        android:textSize="20sp"
+        android:textColor="@drawable/selector_bottom_btn_text_color"/>
+
+</LinearLayout>