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

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

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

Loading…
Cancel
Save