Browse Source

1.将静态注册的广播改为动态注册。

20241218TB223FC
wangwanlei 5 days ago
parent
commit
4ac5471dca

+ 0
- 31
app/src/main/java/com/xhly/manageapp/broadcastreceiver/BLEStateReceiver.kt View File

39
             }
39
             }
40
         }
40
         }
41
     }
41
     }
42
-
43
-    companion object {
44
-        private const val BLE_STATE_OFF = "android.bluetooth.BluetoothAdapter.STATE_OFF"
45
-        private const val BLE_STATE_ON = "android.bluetooth.BluetoothAdapter.STATE_ON"
46
-        private val receiver = BLEStateReceiver()
47
-
48
-        /**
49
-         * 注册
50
-         *
51
-         * @param context
52
-         */
53
-        fun register(context: Context?) {
54
-            /*  IntentFilter filter = new IntentFilter();
55
-        filter.setPriority(Integer.MAX_VALUE);
56
-        // 监视蓝牙关闭和打开的状态
57
-        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
58
-        filter.addAction(BLE_STATE_OFF);
59
-        filter.addAction(BLE_STATE_ON);
60
-        filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
61
-        context.registerReceiver(receiver, filter);*/
62
-        }
63
-
64
-        /**
65
-         * 注销
66
-         *
67
-         * @param context
68
-         */
69
-        fun unregister(context: Context?) {
70
-            // context.unregisterReceiver(receiver);
71
-        }
72
-    }
73
 }
42
 }

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

8
 import android.annotation.SuppressLint
8
 import android.annotation.SuppressLint
9
 import android.app.ActivityManager
9
 import android.app.ActivityManager
10
 import android.app.AlertDialog
10
 import android.app.AlertDialog
11
+import android.bluetooth.BluetoothAdapter
11
 import android.content.ComponentName
12
 import android.content.ComponentName
12
 import android.content.Context
13
 import android.content.Context
13
 import android.content.Intent
14
 import android.content.Intent
78
 import com.xhly.manageapp.bean.strategy.StrategyBean
79
 import com.xhly.manageapp.bean.strategy.StrategyBean
79
 import com.xhly.manageapp.bean.user.UserBean
80
 import com.xhly.manageapp.bean.user.UserBean
80
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
81
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
82
+import com.xhly.manageapp.broadcastreceiver.BLEStateReceiver
81
 import com.xhly.manageapp.broadcastreceiver.NetConnectReceiver
83
 import com.xhly.manageapp.broadcastreceiver.NetConnectReceiver
84
+import com.xhly.manageapp.broadcastreceiver.SDReceiver
82
 import com.xhly.manageapp.broadcastreceiver.ScreenReceiver
85
 import com.xhly.manageapp.broadcastreceiver.ScreenReceiver
86
+import com.xhly.manageapp.broadcastreceiver.USBReceiver
87
+import com.xhly.manageapp.broadcastreceiver.XhlyLoginOutReceiver
88
+import com.xhly.manageapp.broadcastreceiver.XhlyUpdeteStrategyReceiver
83
 import com.xhly.manageapp.service.ManageAccessibilityService
89
 import com.xhly.manageapp.service.ManageAccessibilityService
84
 import com.xhly.manageapp.service.websocket.AppSocket
90
 import com.xhly.manageapp.service.websocket.AppSocket
85
 import com.xhly.manageapp.ui.ManageActivity
91
 import com.xhly.manageapp.ui.ManageActivity
109
     private var mMyInstallReceiver: AppInstallReceiver? = null
115
     private var mMyInstallReceiver: AppInstallReceiver? = null
110
     private var netConnectReceiver: NetConnectReceiver? = null
116
     private var netConnectReceiver: NetConnectReceiver? = null
111
     private var mScreenReceiver: ScreenReceiver? = null
117
     private var mScreenReceiver: ScreenReceiver? = null
118
+    private var mBleReceiver: BLEStateReceiver? = null
119
+    private var mSDReceiver: SDReceiver? = null
120
+    private var mUSBReceiver: USBReceiver? = null
121
+    private var mLoginOutReceiver: XhlyLoginOutReceiver? = null
122
+    private var mXhlyUpdeteStrategyReceiver: XhlyUpdeteStrategyReceiver? = null
112
     private var dataList: ArrayList<AppInfo> = arrayListOf()
123
     private var dataList: ArrayList<AppInfo> = arrayListOf()
113
     private var userBean: UserBean? = null
124
     private var userBean: UserBean? = null
114
     private var timer: Timer? = null
125
     private var timer: Timer? = null
209
         registerAppInstallReceiver()
220
         registerAppInstallReceiver()
210
         registerNetConnectReceiver()
221
         registerNetConnectReceiver()
211
         registerScreenReceiver()
222
         registerScreenReceiver()
223
+        registerBleStateReceiver()
224
+        registerUSBReceiver()
225
+        registerSDReceiver()
226
+        registerLoginOutReceiver()
227
+        registerXhlyUpdeteStrategyReceiver()
212
         /*
228
         /*
213
          使用情况权限
229
          使用情况权限
214
          val permissionIntent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
230
          val permissionIntent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
627
         }
643
         }
628
     }
644
     }
629
 
645
 
646
+
647
+
630
     private fun startAutoApp(autoAppBean: AutoAppBean) {
648
     private fun startAutoApp(autoAppBean: AutoAppBean) {
631
         try {
649
         try {
632
             val launchIntent = Intent()
650
             val launchIntent = Intent()
1879
         registerReceiver(mScreenReceiver, filter)
1897
         registerReceiver(mScreenReceiver, filter)
1880
     }
1898
     }
1881
 
1899
 
1900
+    private fun registerBleStateReceiver() {
1901
+        val filter = IntentFilter()
1902
+        mBleReceiver= BLEStateReceiver()
1903
+        filter.addAction("android.bluetooth.adapter.action.STATE_CHANGED")
1904
+        filter.addAction("android.bluetooth.BluetoothAdapter.STATE_OFF")
1905
+        filter.addAction("android.bluetooth.BluetoothAdapter.STATE_ON" )
1906
+        registerReceiver(mBleReceiver, filter)
1907
+    }
1908
+
1909
+    private fun registerUSBReceiver() {
1910
+        val filter = IntentFilter()
1911
+        mUSBReceiver = USBReceiver()
1912
+        filter.addAction(USBReceiver.ACTION_USB_STATE)
1913
+        registerReceiver(mUSBReceiver, filter)
1914
+    }
1915
+
1916
+    private fun registerSDReceiver() {
1917
+        val filter = IntentFilter()
1918
+        mSDReceiver = SDReceiver()
1919
+        filter.addAction(Intent.ACTION_MEDIA_CHECKING)
1920
+        filter.addAction(Intent.ACTION_MEDIA_UNMOUNTED)
1921
+        filter.addAction(Intent.ACTION_MEDIA_MOUNTED)
1922
+        filter.addAction(Intent.ACTION_MEDIA_EJECT)
1923
+        registerReceiver(mSDReceiver, filter)
1924
+    }
1925
+    private fun registerLoginOutReceiver() {
1926
+        val filter = IntentFilter()
1927
+        mLoginOutReceiver = XhlyLoginOutReceiver()
1928
+        filter.addAction("com.xhly.manageapp.loginout")
1929
+        registerReceiver(mLoginOutReceiver, filter)
1930
+    }
1931
+
1932
+    private fun registerXhlyUpdeteStrategyReceiver() {
1933
+        val filter = IntentFilter()
1934
+        mXhlyUpdeteStrategyReceiver = XhlyUpdeteStrategyReceiver()
1935
+        filter.addAction("com.xhly.manageapp.updetestrategy")
1936
+        registerReceiver(mXhlyUpdeteStrategyReceiver, filter)
1937
+    }
1938
+
1882
     private fun showEditDialog() {
1939
     private fun showEditDialog() {
1883
         accountDialog = null
1940
         accountDialog = null
1884
         accountDialog = CustomDialog.build()
1941
         accountDialog = CustomDialog.build()
2009
         mScreenReceiver?.let {
2066
         mScreenReceiver?.let {
2010
             unregisterReceiver(it)
2067
             unregisterReceiver(it)
2011
         }
2068
         }
2069
+        mBleReceiver?.let {
2070
+            unregisterReceiver(it)
2071
+        }
2072
+        mUSBReceiver?.let {
2073
+            unregisterReceiver(it)
2074
+        }
2075
+        mSDReceiver?.let {
2076
+            unregisterReceiver(it)
2077
+        }
2078
+        mXhlyUpdeteStrategyReceiver?.let {
2079
+            unregisterReceiver(it)
2080
+        }
2081
+        mLoginOutReceiver.let {
2082
+            unregisterReceiver(it)
2083
+        }
2012
         LogShow("MainActivity被销毁")
2084
         LogShow("MainActivity被销毁")
2013
     }
2085
     }
2014
 
2086
 

Loading…
Cancel
Save