Browse Source

1.添加定时更新策略。

2.添加协程延迟,防止动画卡顿。
20241218TB223FC(测试jar包)
wangwanlei 8 months ago
parent
commit
d996563c61

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

91
 import com.xhly.manageapp.utils.StrategyUtils
91
 import com.xhly.manageapp.utils.StrategyUtils
92
 import com.xhly.manageapp.workmanager.TimingWorker
92
 import com.xhly.manageapp.workmanager.TimingWorker
93
 import kotlinx.coroutines.Dispatchers
93
 import kotlinx.coroutines.Dispatchers
94
+import kotlinx.coroutines.delay
94
 import kotlinx.coroutines.launch
95
 import kotlinx.coroutines.launch
95
 import kotlinx.coroutines.withContext
96
 import kotlinx.coroutines.withContext
96
 import java.io.File
97
 import java.io.File
139
     //标记是否执行了屏幕打开
140
     //标记是否执行了屏幕打开
140
     private var screenFlag=false
141
     private var screenFlag=false
141
 
142
 
143
+    //标记上次点击时间
144
+    private var lastChangeTime=0L
142
 
145
 
143
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
146
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
144
 
147
 
645
     private fun getChangeCodeListener(): AppAdapter.ChangePwdListener {
648
     private fun getChangeCodeListener(): AppAdapter.ChangePwdListener {
646
         return object : AppAdapter.ChangePwdListener {
649
         return object : AppAdapter.ChangePwdListener {
647
             override fun changeCode() {
650
             override fun changeCode() {
648
-                userBean?.let {
649
-                    viewModel.getSchoolSetData(it.schoolid,2)
651
+                if (System.currentTimeMillis()-lastChangeTime>1000){
652
+                    lastChangeTime=System.currentTimeMillis()
653
+                    lifecycleScope.launch(Dispatchers.IO){
654
+                        delay(500)
655
+                        userBean?.let {
656
+                            viewModel.getSchoolSetData(it.schoolid,2)
657
+                        }
658
+                    }
650
                 }
659
                 }
651
             }
660
             }
652
 
661
 
1160
             }
1169
             }
1161
 
1170
 
1162
             Const.CODE2004.toString() -> {
1171
             Const.CODE2004.toString() -> {
1163
-                postLogPush(4)
1172
+                viewModel.viewModelScope.launch {
1173
+                    delay(500)
1174
+                    postLogPush(4)
1175
+                }
1164
             }
1176
             }
1165
 
1177
 
1166
             Const.CODE2005.toString() -> {
1178
             Const.CODE2005.toString() -> {

+ 1
- 1
app/src/main/java/com/xhly/manageapp/utils/PadInfoUtils.kt View File

34
             padInfoBean.romVersion=Build.DISPLAY
34
             padInfoBean.romVersion=Build.DISPLAY
35
             padInfoBean.sim=simIccID
35
             padInfoBean.sim=simIccID
36
             padInfoBean.totalMemory=FileSizeUtils.getTotalSpace(context)
36
             padInfoBean.totalMemory=FileSizeUtils.getTotalSpace(context)
37
-            padInfoBean.usedMemory=FileSizeUtils.getFreeSpace(context)
37
+            padInfoBean.usedMemory=padInfoBean.totalMemory-FileSizeUtils.getFreeSpace(context)
38
         }
38
         }
39
         return padInfoBean
39
         return padInfoBean
40
     }
40
     }

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

12
 import com.xhly.corelib.utils.AppUtils
12
 import com.xhly.corelib.utils.AppUtils
13
 import com.xhly.corelib.utils.LogShow
13
 import com.xhly.corelib.utils.LogShow
14
 import com.xhly.corelib.utils.SharedPreferencesUtils
14
 import com.xhly.corelib.utils.SharedPreferencesUtils
15
-import com.xhly.corelib.utils.SystemUtil
16
 import com.xhly.corelib.utils.toFormat
15
 import com.xhly.corelib.utils.toFormat
17
 import com.xhly.manageapp.ManageApplication
16
 import com.xhly.manageapp.ManageApplication
18
-import com.xhly.manageapp.bean.PadLocationConst
19
 import com.xhly.manageapp.bean.log.LogAppStartBean
17
 import com.xhly.manageapp.bean.log.LogAppStartBean
20
-import com.xhly.manageapp.bean.log.PadLocationBean
21
 import com.xhly.manageapp.bean.user.UserBean
18
 import com.xhly.manageapp.bean.user.UserBean
22
 import com.xhly.manageapp.network.log.LogService
19
 import com.xhly.manageapp.network.log.LogService
23
 import com.xhly.manageapp.service.websocket.AppSocket
20
 import com.xhly.manageapp.service.websocket.AppSocket
26
 import kotlinx.coroutines.MainScope
23
 import kotlinx.coroutines.MainScope
27
 import kotlinx.coroutines.cancel
24
 import kotlinx.coroutines.cancel
28
 import kotlinx.coroutines.launch
25
 import kotlinx.coroutines.launch
29
-import org.json.JSONObject
30
 import java.util.Calendar
26
 import java.util.Calendar
31
 
27
 
32
 /**
28
 /**
35
 class TimingWorker(context: Context, workerParams: WorkerParameters) :
31
 class TimingWorker(context: Context, workerParams: WorkerParameters) :
36
     Worker(context, workerParams) {
32
     Worker(context, workerParams) {
37
     private val loginService by lazy { RetrofitService.create<LoginService>() }
33
     private val loginService by lazy { RetrofitService.create<LoginService>() }
38
-    private val logService by lazy {  RetrofitService.create<LogService>() }
34
+    private val logService by lazy { RetrofitService.create<LogService>() }
39
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
35
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
40
     private val spUtils by lazy {
36
     private val spUtils by lazy {
41
         SharedPreferencesUtils.getInstance(context)
37
         SharedPreferencesUtils.getInstance(context)
42
     }
38
     }
43
 
39
 
44
-    companion object{
45
-        private var lastTime=0L
40
+    companion object {
41
+        private var lastTime = 0L
42
+
43
+        //上次策略请求时间
44
+        private var StrategyTime = 0L
46
     }
45
     }
47
 
46
 
48
     override fun doWork(): Result {
47
     override fun doWork(): Result {
49
         val result: Result = Result.success()
48
         val result: Result = Result.success()
50
         serviceScope.launch {
49
         serviceScope.launch {
51
             LogShow("执行定时任务吧")
50
             LogShow("执行定时任务吧")
52
-            var userBean:UserBean?=null
51
+            var userBean: UserBean? = null
53
             try {
52
             try {
54
                 val fromJson = spUtils.getFromJson(Const.USERINFO, UserBean().javaClass)
53
                 val fromJson = spUtils.getFromJson(Const.USERINFO, UserBean().javaClass)
55
-                userBean= fromJson as UserBean?
56
-            }catch (e:Exception){
54
+                userBean = fromJson as UserBean?
55
+            } catch (e: Exception) {
57
 
56
 
58
             }
57
             }
59
             val appInfo = getAppInfo()
58
             val appInfo = getAppInfo()
60
             if (appInfo.size > 0) {
59
             if (appInfo.size > 0) {
61
                 logService.postLogAppStartAdd(appInfo)
60
                 logService.postLogAppStartAdd(appInfo)
62
             }
61
             }
63
-/*
64
-            try {
65
-                val location = PadLocationConst.currentLocation
66
-                val lastLocation = PadLocationConst.lastLocation
67
-                if (location.lat!=null&&location.lon!=null&&(lastLocation.lon!=location.lon&&lastLocation.lat!=location.lat)){
68
-                    lastLocation.lat=location.lat
69
-                    lastLocation.lon=location.lon
70
-                    ManageApplication.getDeviceInfo()?.let {
71
-                        location.sn=it
72
-                    }
73
-                    userBean?.let {
74
-                        location.userid=it.userid
75
-                    }
76
-                    logService.postPadLocation(location)
77
-                    LogShow("真没有位置")
78
-                }else{
79
-                    LogShow("没有位置")
80
-                }
81
-            }catch (e:Exception){
82
-                LogShow("没有位置问题"+e.toString())
83
-            }
84
-*/
62
+            /*
63
+                        try {
64
+                            val location = PadLocationConst.currentLocation
65
+                            val lastLocation = PadLocationConst.lastLocation
66
+                            if (location.lat!=null&&location.lon!=null&&(lastLocation.lon!=location.lon&&lastLocation.lat!=location.lat)){
67
+                                lastLocation.lat=location.lat
68
+                                lastLocation.lon=location.lon
69
+                                ManageApplication.getDeviceInfo()?.let {
70
+                                    location.sn=it
71
+                                }
72
+                                userBean?.let {
73
+                                    location.userid=it.userid
74
+                                }
75
+                                logService.postPadLocation(location)
76
+                                LogShow("真没有位置")
77
+                            }else{
78
+                                LogShow("没有位置")
79
+                            }
80
+                        }catch (e:Exception){
81
+                            LogShow("没有位置问题"+e.toString())
82
+                        }
83
+            */
85
             UIEvent(Const.STARTLOCATION).post()
84
             UIEvent(Const.STARTLOCATION).post()
86
             userBean?.let {
85
             userBean?.let {
87
-                val map=HashMap<String,Any>()
88
-                map["sn"]=ManageApplication.getDeviceInfo()?:""
86
+                val map = HashMap<String, Any>()
87
+                map["sn"] = ManageApplication.getDeviceInfo() ?: ""
89
                 val postCheckSn = loginService.postCheckSn(map)
88
                 val postCheckSn = loginService.postCheckSn(map)
90
-                if (postCheckSn.code==0&&postCheckSn.obj){
89
+                if (postCheckSn.code == 0 && postCheckSn.obj) {
91
                     UIEvent(Const.ACCOUNTLOGINOUTSUCESS).post()
90
                     UIEvent(Const.ACCOUNTLOGINOUTSUCESS).post()
92
                 }
91
                 }
93
             }
92
             }
94
             //校验登录
93
             //校验登录
95
             userBean?.let {
94
             userBean?.let {
96
-                val map=HashMap<String,Any>()
97
-                map["userid"]=userBean.userid
98
-                map["loginpwd"]=userBean.loginpwd
95
+                val map = HashMap<String, Any>()
96
+                map["userid"] = userBean.userid
97
+                map["loginpwd"] = userBean.loginpwd
99
                 try {
98
                 try {
100
                     val postLoginCheck = loginService.postLoginCheck(map)
99
                     val postLoginCheck = loginService.postLoginCheck(map)
101
-                    if (postLoginCheck.code==0&&postLoginCheck.obj.needLogin){
100
+                    if (postLoginCheck.code == 0 && postLoginCheck.obj.needLogin) {
102
                         UIEvent(Const.ACCOUNTLOGINOUT).post()
101
                         UIEvent(Const.ACCOUNTLOGINOUT).post()
103
                     }
102
                     }
104
-                }catch (e:Exception){
103
+                } catch (e: Exception) {
105
                     LogShow("解析异常")
104
                     LogShow("解析异常")
106
                 }
105
                 }
107
             }
106
             }
111
                     AppSocket.startSocket(it)
110
                     AppSocket.startSocket(it)
112
                 }
111
                 }
113
             }
112
             }
113
+            //超出30分钟则请求策略
114
+            if (StrategyTime == 0L) {
115
+                StrategyTime = System.currentTimeMillis()
116
+            } else if (System.currentTimeMillis() - StrategyTime >= 30 * 60 * 1000) {
117
+                StrategyTime = System.currentTimeMillis()
118
+                UIEvent(Const.CODE2004.toString()).post()
119
+            }
114
         }
120
         }
115
         return result
121
         return result
116
     }
122
     }
121
         val instance = Calendar.getInstance()
127
         val instance = Calendar.getInstance()
122
         instance.set(Calendar.MILLISECOND, 0)
128
         instance.set(Calendar.MILLISECOND, 0)
123
         val currentTime = instance.timeInMillis
129
         val currentTime = instance.timeInMillis
124
-        if (lastTime==0L){
125
-            lastTime=currentTime
126
-        }else{
127
-            LogShow("上次时间"+ lastTime.toFormat()+"|||本次时间"+currentTime.toFormat())
128
-            lastTime=currentTime
130
+        if (lastTime == 0L) {
131
+            lastTime = currentTime
132
+        } else {
133
+            LogShow("上次时间" + lastTime.toFormat() + "|||本次时间" + currentTime.toFormat())
134
+            lastTime = currentTime
129
         }
135
         }
130
         val timeInMillis = currentTime - 1000 * 60 * 5
136
         val timeInMillis = currentTime - 1000 * 60 * 5
131
         val usageManager =
137
         val usageManager =

Loading…
Cancel
Save