Explorar el Código

1.添加定时更新策略。

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

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

@@ -91,6 +91,7 @@ import com.xhly.manageapp.utils.PadInfoUtils
91 91
 import com.xhly.manageapp.utils.StrategyUtils
92 92
 import com.xhly.manageapp.workmanager.TimingWorker
93 93
 import kotlinx.coroutines.Dispatchers
94
+import kotlinx.coroutines.delay
94 95
 import kotlinx.coroutines.launch
95 96
 import kotlinx.coroutines.withContext
96 97
 import java.io.File
@@ -139,6 +140,8 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(),Download
139 140
     //标记是否执行了屏幕打开
140 141
     private var screenFlag=false
141 142
 
143
+    //标记上次点击时间
144
+    private var lastChangeTime=0L
142 145
 
143 146
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
144 147
 
@@ -645,8 +648,14 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(),Download
645 648
     private fun getChangeCodeListener(): AppAdapter.ChangePwdListener {
646 649
         return object : AppAdapter.ChangePwdListener {
647 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,7 +1169,10 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>(),Download
1160 1169
             }
1161 1170
 
1162 1171
             Const.CODE2004.toString() -> {
1163
-                postLogPush(4)
1172
+                viewModel.viewModelScope.launch {
1173
+                    delay(500)
1174
+                    postLogPush(4)
1175
+                }
1164 1176
             }
1165 1177
 
1166 1178
             Const.CODE2005.toString() -> {

+ 1
- 1
app/src/main/java/com/xhly/manageapp/utils/PadInfoUtils.kt Ver fichero

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

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

@@ -12,12 +12,9 @@ import com.xhly.corelib.network.RetrofitService
12 12
 import com.xhly.corelib.utils.AppUtils
13 13
 import com.xhly.corelib.utils.LogShow
14 14
 import com.xhly.corelib.utils.SharedPreferencesUtils
15
-import com.xhly.corelib.utils.SystemUtil
16 15
 import com.xhly.corelib.utils.toFormat
17 16
 import com.xhly.manageapp.ManageApplication
18
-import com.xhly.manageapp.bean.PadLocationConst
19 17
 import com.xhly.manageapp.bean.log.LogAppStartBean
20
-import com.xhly.manageapp.bean.log.PadLocationBean
21 18
 import com.xhly.manageapp.bean.user.UserBean
22 19
 import com.xhly.manageapp.network.log.LogService
23 20
 import com.xhly.manageapp.service.websocket.AppSocket
@@ -26,7 +23,6 @@ import kotlinx.coroutines.CoroutineScope
26 23
 import kotlinx.coroutines.MainScope
27 24
 import kotlinx.coroutines.cancel
28 25
 import kotlinx.coroutines.launch
29
-import org.json.JSONObject
30 26
 import java.util.Calendar
31 27
 
32 28
 /**
@@ -35,73 +31,76 @@ import java.util.Calendar
35 31
 class TimingWorker(context: Context, workerParams: WorkerParameters) :
36 32
     Worker(context, workerParams) {
37 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 35
     private val serviceScope = CoroutineScope(MainScope().coroutineContext)
40 36
     private val spUtils by lazy {
41 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 47
     override fun doWork(): Result {
49 48
         val result: Result = Result.success()
50 49
         serviceScope.launch {
51 50
             LogShow("执行定时任务吧")
52
-            var userBean:UserBean?=null
51
+            var userBean: UserBean? = null
53 52
             try {
54 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 58
             val appInfo = getAppInfo()
60 59
             if (appInfo.size > 0) {
61 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 84
             UIEvent(Const.STARTLOCATION).post()
86 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 88
                 val postCheckSn = loginService.postCheckSn(map)
90
-                if (postCheckSn.code==0&&postCheckSn.obj){
89
+                if (postCheckSn.code == 0 && postCheckSn.obj) {
91 90
                     UIEvent(Const.ACCOUNTLOGINOUTSUCESS).post()
92 91
                 }
93 92
             }
94 93
             //校验登录
95 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 98
                 try {
100 99
                     val postLoginCheck = loginService.postLoginCheck(map)
101
-                    if (postLoginCheck.code==0&&postLoginCheck.obj.needLogin){
100
+                    if (postLoginCheck.code == 0 && postLoginCheck.obj.needLogin) {
102 101
                         UIEvent(Const.ACCOUNTLOGINOUT).post()
103 102
                     }
104
-                }catch (e:Exception){
103
+                } catch (e: Exception) {
105 104
                     LogShow("解析异常")
106 105
                 }
107 106
             }
@@ -111,6 +110,13 @@ class TimingWorker(context: Context, workerParams: WorkerParameters) :
111 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 121
         return result
116 122
     }
@@ -121,11 +127,11 @@ class TimingWorker(context: Context, workerParams: WorkerParameters) :
121 127
         val instance = Calendar.getInstance()
122 128
         instance.set(Calendar.MILLISECOND, 0)
123 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 136
         val timeInMillis = currentTime - 1000 * 60 * 5
131 137
         val usageManager =

Loading…
Cancelar
Guardar