소스 검색

1.调整任务限制 修复应用必须一分钟内完成安装的BUG

JaycePC 3 달 전
부모
커밋
ac9f84c637
2개의 변경된 파일17개의 추가작업 그리고 5개의 파일을 삭제
  1. 1 1
      app/build.gradle
  2. 16 4
      app/src/main/java/xn/update/receiver/TimeTickReceiver.java

+ 1 - 1
app/build.gradle

@@ -13,7 +13,7 @@ android {
         //noinspection ExpiredTargetSdkVersion
         targetSdk 28
         versionCode 1
-        versionName "1.3"
+        versionName "1.5"
 
         testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
     }

+ 16 - 4
app/src/main/java/xn/update/receiver/TimeTickReceiver.java

@@ -38,6 +38,7 @@ import xn.update.http.bean.response.UpdateTask;
 
 public class TimeTickReceiver extends BroadcastReceiver {
     private volatile boolean isRunning = false;
+    private long lastRunTime;
     private DownloadManager downloadManager;
     private BroadcastReceiver downloadApkReceiver;
     private long downloadId;
@@ -53,8 +54,17 @@ public class TimeTickReceiver extends BroadcastReceiver {
         Intent newIntent = new Intent("XN_ACTION");
         intent.putExtra("heartbeat", "heartbeat");
         context.sendBroadcast(newIntent);
+        // 若是3分钟都没下载完成,则认为下载失败
+        long runTime = (System.currentTimeMillis() - lastRunTime);
+        if (lastRunTime > 0 && runTime > 1000 * 60 * 3) {
+            isRunning = false;
+        } else {
+            LogUtils.d("下载安装app中,已耗时" + runTime);
+        }
         // 空闲中
         if (!isRunning && SPUtils.getInstance().getBoolean(AppConstant.AUTO_UPDATE, true)) {
+            lastRunTime = System.currentTimeMillis();
+            isRunning = true;
             if (null == downloadManager) {
                 downloadManager = (DownloadManager) context.getApplicationContext().getSystemService(Context.DOWNLOAD_SERVICE);
                 downloadApkReceiver = new BroadcastReceiver() {
@@ -109,7 +119,6 @@ public class TimeTickReceiver extends BroadcastReceiver {
                                     }
                                 }
                             }
-                            isRunning = false;
                         }
                     }
                 };
@@ -118,7 +127,6 @@ public class TimeTickReceiver extends BroadcastReceiver {
                 @SuppressLint({"UnspecifiedRegisterReceiverFlag", "SdCardPath"})
                 @Override
                 public Boolean doInBackground() throws Throwable {
-                    isRunning = true;
                     try {
                         Response response = HttpTool.INSTANCE.update();
                         if (response.isSuccessful()) {
@@ -144,23 +152,27 @@ public class TimeTickReceiver extends BroadcastReceiver {
                                     return true;
                                 }
                             } else {
-
                                 LogUtils.e(updateTask.getMessage());
                             }
                         }
                     } catch (Exception e) {
                         LogUtils.e(Log.getStackTraceString(e));
                     }
-                    isRunning = false;
                     return false;
                 }
 
                 @Override
                 public void onSuccess(Boolean result) {
+                    isRunning = result;
                 }
             });
         }
+        // 拉起主应用
+        startMasterApp();
+
+    }
 
+    private void startMasterApp() {
         if (SPUtils.getInstance().getBoolean(AppConstant.AUTO_MASTER, true)) {
             ThreadUtils.executeByCached(new ThreadUtils.SimpleTask<Object>() {
                 @Override