浏览代码

1.添加SD卡事件处理

master
wangwanlei 1年前
父节点
当前提交
a7f4ea4768

+ 14
- 3
app/src/main/java/com/xhly/manageapp/broadcastreceiver/SDReceiver.kt 查看文件

3
 import android.content.BroadcastReceiver
3
 import android.content.BroadcastReceiver
4
 import android.content.Context
4
 import android.content.Context
5
 import android.content.Intent
5
 import android.content.Intent
6
+import com.xhly.corelib.Const
7
+import com.xhly.corelib.eventbus.UIEvent
6
 import com.xhly.corelib.utils.LogShow
8
 import com.xhly.corelib.utils.LogShow
7
 
9
 
8
 class SDReceiver: BroadcastReceiver() {
10
 class SDReceiver: BroadcastReceiver() {
11
+    companion object{
12
+        var installLastTime=0L
13
+    }
9
     override fun onReceive(context: Context, intent: Intent) {
14
     override fun onReceive(context: Context, intent: Intent) {
10
         val action = intent.action
15
         val action = intent.action
11
         when (action) {
16
         when (action) {
15
             }
20
             }
16
              Intent.ACTION_MEDIA_MOUNTED->
21
              Intent.ACTION_MEDIA_MOUNTED->
17
              {
22
              {
18
-                 //SD卡挂载成功
19
-                 LogShow("sd卡已插入")
23
+                 //SD卡挂载成功,可能会发送两次
24
+                 if (installLastTime==0L||(System.currentTimeMillis()- installLastTime>1000)){
25
+                     installLastTime=System.currentTimeMillis()
26
+                     LogShow("sd卡已插入")
27
+                     UIEvent(Const.SDINSTALL).post()
28
+                 }
29
+                 /*LogShow("sd卡已插入")
30
+                 UIEvent(Const.SDINSTALL).post()*/
20
              }
31
              }
21
             Intent.ACTION_MEDIA_EJECT->{
32
             Intent.ACTION_MEDIA_EJECT->{
22
                 //SD卡拔出
33
                 //SD卡拔出
23
                 LogShow("sd卡已拔出")
34
                 LogShow("sd卡已拔出")
35
+                UIEvent(Const.SDUNINSTALL).post()
24
             }
36
             }
25
             Intent.ACTION_MEDIA_UNMOUNTED->{
37
             Intent.ACTION_MEDIA_UNMOUNTED->{
26
                 //SD卡卸载成功
38
                 //SD卡卸载成功
27
-                LogShow("sd卡已卸载")
28
             }
39
             }
29
         }
40
         }
30
     }
41
     }

+ 24
- 1
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt 查看文件

24
 import com.xhly.corelib.Const
24
 import com.xhly.corelib.Const
25
 import com.xhly.corelib.bean.AppInfo
25
 import com.xhly.corelib.bean.AppInfo
26
 import com.xhly.corelib.eventbus.UIEvent
26
 import com.xhly.corelib.eventbus.UIEvent
27
+import com.xhly.corelib.network.RetrofitService
27
 import com.xhly.corelib.utils.AppUtils
28
 import com.xhly.corelib.utils.AppUtils
28
 import com.xhly.corelib.utils.CustomOSUtils
29
 import com.xhly.corelib.utils.CustomOSUtils
29
 import com.xhly.corelib.utils.LogShow
30
 import com.xhly.corelib.utils.LogShow
30
 import com.xhly.corelib.utils.PhoneInfoUtils
31
 import com.xhly.corelib.utils.PhoneInfoUtils
31
 import com.xhly.manageapp.AppAdapter
32
 import com.xhly.manageapp.AppAdapter
33
+import com.xhly.manageapp.ManageApplication
32
 import com.xhly.manageapp.R
34
 import com.xhly.manageapp.R
35
+import com.xhly.manageapp.bean.EventLog
36
+import com.xhly.manageapp.bean.log.LogdOperateBean
33
 import com.xhly.manageapp.bean.strategy.StrategyBean
37
 import com.xhly.manageapp.bean.strategy.StrategyBean
34
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
38
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
35
 import com.xhly.manageapp.broadcastreceiver.SDReceiver
39
 import com.xhly.manageapp.broadcastreceiver.SDReceiver
36
 import com.xhly.manageapp.databinding.ActivityMainBinding
40
 import com.xhly.manageapp.databinding.ActivityMainBinding
41
+import com.xhly.manageapp.network.log.LogService
37
 import com.xhly.manageapp.service.ManageAccessibilityService
42
 import com.xhly.manageapp.service.ManageAccessibilityService
38
 import com.xhly.manageapp.service.websocket.AppSocketService
43
 import com.xhly.manageapp.service.websocket.AppSocketService
39
 import com.xhly.manageapp.ui.ManageActivity
44
 import com.xhly.manageapp.ui.ManageActivity
51
     private var mMyInstallReceiver: AppInstallReceiver? = null
56
     private var mMyInstallReceiver: AppInstallReceiver? = null
52
     private var mySDReceiver: SDReceiver? = null
57
     private var mySDReceiver: SDReceiver? = null
53
     private var dataList: ArrayList<AppInfo> = arrayListOf()
58
     private var dataList: ArrayList<AppInfo> = arrayListOf()
59
+
54
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
60
     override fun getBinding() = ActivityMainBinding.inflate(layoutInflater)
55
 
61
 
56
     override fun initData() {
62
     override fun initData() {
206
 
212
 
207
                 }
213
                 }
208
             }
214
             }
209
-
215
+            Const.SDINSTALL -> {
216
+                val data = LogdOperateBean()
217
+                ManageApplication.getDeviceInfo()?.let {
218
+                    data.sn = it
219
+                }
220
+                data.comm = "SD卡插入"
221
+                data.doEvent = EventLog.SDEMOUNTEDVENT
222
+               viewModel.eventLog(data)
223
+            }
224
+            Const.SDUNINSTALL->{
225
+                val data = LogdOperateBean()
226
+                ManageApplication.getDeviceInfo()?.let {
227
+                    data.sn = it
228
+                }
229
+                data.comm = "SD卡拔出"
230
+                data.doEvent = EventLog.SDEJECTEVENT
231
+                viewModel.eventLog(data)
232
+            }
210
             "策略更新" -> {
233
             "策略更新" -> {
211
 
234
 
212
             }
235
             }

+ 10
- 0
app/src/main/java/com/xhly/manageapp/ui/main/viewmodel/MainViewModel.kt 查看文件

6
 import com.xhly.corelib.network.RetrofitService
6
 import com.xhly.corelib.network.RetrofitService
7
 import com.xhly.manageapp.bean.AppModel
7
 import com.xhly.manageapp.bean.AppModel
8
 import com.xhly.manageapp.bean.ListAppBean
8
 import com.xhly.manageapp.bean.ListAppBean
9
+import com.xhly.manageapp.bean.log.LogdOperateBean
9
 import com.xhly.manageapp.bean.strategy.StrategyBean
10
 import com.xhly.manageapp.bean.strategy.StrategyBean
10
 import com.xhly.manageapp.network.app.AppService
11
 import com.xhly.manageapp.network.app.AppService
12
+import com.xhly.manageapp.network.log.LogService
11
 import com.xhly.manageapp.network.strategy.StrategyService
13
 import com.xhly.manageapp.network.strategy.StrategyService
14
+import kotlinx.coroutines.launch
12
 
15
 
13
 class MainViewModel : CommonBaseViewModel() {
16
 class MainViewModel : CommonBaseViewModel() {
14
     private val appService by lazy { RetrofitService.create<AppService>() }
17
     private val appService by lazy { RetrofitService.create<AppService>() }
15
     private val strategyService by lazy { RetrofitService.create<StrategyService>() }
18
     private val strategyService by lazy { RetrofitService.create<StrategyService>() }
19
+    private var logService = RetrofitService.create<LogService>()
16
     private val listData = MutableLiveData<ArrayList<AppModel>>()
20
     private val listData = MutableLiveData<ArrayList<AppModel>>()
17
     val listAppData: LiveData<ArrayList<AppModel>> = listData
21
     val listAppData: LiveData<ArrayList<AppModel>> = listData
18
     private val strategy=MutableLiveData<StrategyBean>()
22
     private val strategy=MutableLiveData<StrategyBean>()
48
               }
52
               }
49
           }
53
           }
50
     }
54
     }
55
+
56
+     fun eventLog(operateBean: LogdOperateBean) {
57
+         launchUI {
58
+             logService.postLogdoperateAdd(operateBean)
59
+         }
60
+    }
51
 }
61
 }

正在加载...
取消
保存