Browse Source

1.在OnResume中获取上次定时任务时间,超出则重新执行定时任务。

20241218TB223FC(测试jar包)
wangwanlei 8 months ago
parent
commit
93fc206bbc

+ 4
- 0
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt View File

@@ -786,6 +786,10 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(),Download
786 786
 
787 787
     //亮屏,点击,和onresume都执行这个请求策略的方法,达到次数则请求策略。
788 788
     private fun updateResumeStrategy(){
789
+        //在activity中判断,如果连续10分钟都没执行则重启任务。
790
+        if (System.currentTimeMillis()-TimingWorker.lastTime>10*60*1000){
791
+            startWorkManager()
792
+        }
789 793
         lifecycleScope.launch(Dispatchers.IO){
790 794
             try {
791 795
                 val json = spUtils.getFromJson(Const.USERINFO, UserBean().javaClass)

+ 3
- 2
app/src/main/java/com/xhly/manageapp/workmanager/TimingWorker.kt View File

@@ -38,7 +38,8 @@ class TimingWorker(context: Context, workerParams: WorkerParameters) :
38 38
     }
39 39
 
40 40
     companion object {
41
-        private var lastTime = 0L
41
+        //在activity中判断,如果连续10分钟都没执行则重启任务。
42
+        var lastTime = 0L
42 43
 
43 44
         //上次策略请求时间
44 45
         private var StrategyTime = 0L
@@ -127,10 +128,10 @@ class TimingWorker(context: Context, workerParams: WorkerParameters) :
127 128
         val instance = Calendar.getInstance()
128 129
         instance.set(Calendar.MILLISECOND, 0)
129 130
         val currentTime = instance.timeInMillis
131
+        LogShow("上次时间" + lastTime.toFormat() + "|||本次时间" + currentTime.toFormat())
130 132
         if (lastTime == 0L) {
131 133
             lastTime = currentTime
132 134
         } else {
133
-            LogShow("上次时间" + lastTime.toFormat() + "|||本次时间" + currentTime.toFormat())
134 135
             lastTime = currentTime
135 136
         }
136 137
         val timeInMillis = currentTime - 1000 * 60 * 5

Loading…
Cancel
Save