Просмотр исходного кода

1.添加SIM卡和蓝牙事件处理

master
wangwanlei 11 месяцев назад
Родитель
Сommit
b20d69d3ba

+ 54
- 52
app/src/main/java/com/xhly/manageapp/broadcastreceiver/BLEStateReceiver.kt Просмотреть файл

@@ -1,28 +1,56 @@
1
-package com.xhly.manageapp.broadcastreceiver;
1
+package com.xhly.manageapp.broadcastreceiver
2 2
 
3
-import android.annotation.SuppressLint;
4
-import android.bluetooth.BluetoothAdapter;
5
-import android.content.BroadcastReceiver;
6
-import android.content.Context;
7
-import android.content.Intent;
8
-import android.content.IntentFilter;
9
-import android.util.Log;
10
-import android.widget.Toast;
3
+import android.bluetooth.BluetoothAdapter
4
+import android.content.BroadcastReceiver
5
+import android.content.Context
6
+import android.content.Intent
7
+import android.util.Log
8
+import android.widget.Toast
9
+import com.xhly.corelib.Const
10
+import com.xhly.corelib.eventbus.UIEvent
11
+
12
+class BLEStateReceiver : BroadcastReceiver() {
13
+    override fun onReceive(context: Context, intent: Intent) {
14
+        // 检测蓝牙状态的逻辑
15
+        checkBLEState(context, intent)
16
+    }
11 17
 
12
-public class BLEStateReceiver extends BroadcastReceiver {
13
- 
14
-    private static final String BLE_STATE_OFF = "android.bluetooth.BluetoothAdapter.STATE_OFF";
15
-    private static final String BLE_STATE_ON = "android.bluetooth.BluetoothAdapter.STATE_ON";
16
-    private static BLEStateReceiver receiver = new BLEStateReceiver();
17
- 
18 18
     /**
19
-     * 注册
19
+     * 检测蓝牙状态
20 20
      *
21 21
      * @param context
22
+     * @param intent
22 23
      */
24
+    @Synchronized
25
+    private fun checkBLEState(context: Context, intent: Intent) {
26
+        val BLEState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)
27
+        Log.i("执行监听", "--------蓝牙监听$BLEState")
28
+        when (BLEState) {
29
+            BluetoothAdapter.STATE_ON ->                 // 蓝牙已经打开
30
+            {
31
+                Toast.makeText(context, "蓝牙打开", Toast.LENGTH_LONG).show()
32
+                UIEvent(Const.BLUETOOTHOPEN).post()
33
+            }
34
+
35
+            BluetoothAdapter.STATE_TURNING_OFF ->                 // 蓝牙正在关闭
36
+            {
37
+                Toast.makeText(context, "蓝牙关闭", Toast.LENGTH_LONG).show()
38
+            }
39
+        }
40
+    }
41
+
42
+    companion object {
43
+        private const val BLE_STATE_OFF = "android.bluetooth.BluetoothAdapter.STATE_OFF"
44
+        private const val BLE_STATE_ON = "android.bluetooth.BluetoothAdapter.STATE_ON"
45
+        private val receiver = BLEStateReceiver()
23 46
 
24
-    public static void register(Context context) {
25
-      /*  IntentFilter filter = new IntentFilter();
47
+        /**
48
+         * 注册
49
+         *
50
+         * @param context
51
+         */
52
+        fun register(context: Context?) {
53
+            /*  IntentFilter filter = new IntentFilter();
26 54
         filter.setPriority(Integer.MAX_VALUE);
27 55
         // 监视蓝牙关闭和打开的状态
28 56
         filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
@@ -30,41 +58,15 @@ public class BLEStateReceiver extends BroadcastReceiver {
30 58
         filter.addAction(BLE_STATE_ON);
31 59
         filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY);
32 60
         context.registerReceiver(receiver, filter);*/
33
-    }
34
- 
35
-    /**
36
-     * 注销
37
-     *
38
-     * @param context
39
-     */
40
-    public static void unregister(Context context) {
41
-       // context.unregisterReceiver(receiver);
42
-    }
43
- 
44
-    @Override
45
-    public void onReceive(final Context context, Intent intent) {
46
-        // 检测蓝牙状态的逻辑
47
-        checkBLEState(context, intent);
48
-    }
61
+        }
49 62
 
50
-    /**
51
-     * 检测蓝牙状态
52
-     *
53
-     * @param context
54
-     * @param intent
55
-     */
56
-    private synchronized void checkBLEState(Context context, Intent intent) {
57
-        int BLEState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
58
-        Log.i("执行监听", "--------蓝牙监听" +BLEState);
59
-        switch (BLEState) {
60
-            case BluetoothAdapter.STATE_ON:
61
-                // 蓝牙已经打开
62
-                Toast.makeText(context, "蓝牙打开", Toast.LENGTH_LONG).show();
63
-                break;
64
-            case BluetoothAdapter.STATE_TURNING_OFF:
65
-                // 蓝牙正在关闭
66
-                Toast.makeText(context, "蓝牙关闭" , Toast.LENGTH_LONG).show();
67
-                break;
63
+        /**
64
+         * 注销
65
+         *
66
+         * @param context
67
+         */
68
+        fun unregister(context: Context?) {
69
+            // context.unregisterReceiver(receiver);
68 70
         }
69 71
     }
70 72
 }

+ 36
- 40
app/src/main/java/com/xhly/manageapp/broadcastreceiver/SimStateReceive.kt Просмотреть файл

@@ -1,50 +1,46 @@
1
-package com.xhly.manageapp.broadcastreceiver;
1
+package com.xhly.manageapp.broadcastreceiver
2 2
 
3
-import android.app.Service;
4
-import android.content.BroadcastReceiver;
5
-import android.content.Context;
6
-import android.content.Intent;
7
-import android.telephony.TelephonyManager;
3
+import android.app.Service
4
+import android.content.BroadcastReceiver
5
+import android.content.Context
6
+import android.content.Intent
7
+import android.telephony.TelephonyManager
8
+import com.xhly.corelib.Const
9
+import com.xhly.corelib.eventbus.UIEvent
10
+import com.xhly.corelib.utils.LogShow
8 11
 
9
-import java.util.Objects;
10
-
11
-public class SimStateReceive extends BroadcastReceiver {
12
-    private final static String ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED";
13
-    private final static int SIM_VALID = 0;
14
-    private final static int SIM_INVALID = 1;
15
-    private int simState = SIM_INVALID;
12
+class SimStateReceive : BroadcastReceiver() {
13
+    override fun onReceive(context: Context, intent: Intent) {
14
+        println("sim state changed")
15
+        try {
16
+            if (intent.action == ACTION_SIM_STATE_CHANGED) {
17
+                val tm = context.getSystemService(Service.TELEPHONY_SERVICE) as TelephonyManager
18
+                val state = tm.simState
19
+                when (state) {
20
+                    TelephonyManager.SIM_STATE_READY -> {
21
+                        LogShow("有SIM卡$SIM_VALID"+tm.simSerialNumber)
22
+                        UIEvent(Const.SIMINSTALL).post()
23
+                    }
16 24
 
17
-    public int getSimState() {
18
-        return simState;
19
-    }
25
+                    TelephonyManager.SIM_STATE_UNKNOWN, TelephonyManager.SIM_STATE_ABSENT,
26
+                    TelephonyManager.SIM_STATE_PIN_REQUIRED, TelephonyManager.SIM_STATE_PUK_REQUIRED,
27
+                    TelephonyManager.SIM_STATE_NETWORK_LOCKED -> {
28
+                        LogShow("没有SIM卡$SIM_INVALID")
29
+                    }
20 30
 
21
-    @Override
22
-    public void onReceive(Context context, Intent intent) {
23
-        System.out.println("sim state changed");
24
-        try {
25
-            if (Objects.equals(intent.getAction(), ACTION_SIM_STATE_CHANGED)) {
26
-                TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);
27
-                int state = tm.getSimState();
28
-                switch (state) {
29
-                    case TelephonyManager.SIM_STATE_READY :    //
30
-                        simState = SIM_VALID;
31
-                      /*  Logger.i("有sim卡="+simState);
32
-                        new ReportPhoneNumber().start();*/
33
-                        break;
34
-                    case TelephonyManager.SIM_STATE_UNKNOWN :
35
-                    case TelephonyManager.SIM_STATE_ABSENT :
36
-                    case TelephonyManager.SIM_STATE_PIN_REQUIRED :
37
-                    case TelephonyManager.SIM_STATE_PUK_REQUIRED :
38
-                    case TelephonyManager.SIM_STATE_NETWORK_LOCKED :
39
-                    default:
40
-                        simState = SIM_INVALID;
41
-                      /*  Logger.i("没sim卡="+simState);*/
42
-                        break;
31
+                    else -> {
32
+                        LogShow("没有SIM卡$SIM_INVALID")
33
+                    }
43 34
                 }
44 35
             }
45
-        }catch (Exception e){
46
-            e.printStackTrace();
36
+        } catch (e: Exception) {
37
+            e.printStackTrace()
47 38
         }
48 39
     }
49 40
 
41
+    companion object {
42
+        private const val ACTION_SIM_STATE_CHANGED = "android.intent.action.SIM_STATE_CHANGED"
43
+        private const val SIM_VALID = 0
44
+        private const val SIM_INVALID = 1
45
+    }
50 46
 }

+ 18
- 0
app/src/main/java/com/xhly/manageapp/ui/main/activity/MainActivity.kt Просмотреть файл

@@ -230,6 +230,24 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
230 230
                 data.doEvent = EventLog.SDEJECTEVENT
231 231
                 viewModel.eventLog(data)
232 232
             }
233
+            Const.SIMINSTALL->{
234
+                val data = LogdOperateBean()
235
+                ManageApplication.getDeviceInfo()?.let {
236
+                    data.sn = it
237
+                }
238
+                data.comm = "SIM卡安装"
239
+                data.doEvent = EventLog.SIMEVENT
240
+                viewModel.eventLog(data)
241
+            }
242
+            Const.BLUETOOTHOPEN->{
243
+                val data = LogdOperateBean()
244
+                ManageApplication.getDeviceInfo()?.let {
245
+                    data.sn = it
246
+                }
247
+                data.comm = "蓝牙打开"
248
+                data.doEvent = EventLog.BLUETOOHEVENT
249
+                viewModel.eventLog(data)
250
+            }
233 251
             "策略更新" -> {
234 252
 
235 253
             }

+ 22
- 3
corelib/src/main/java/com/xhly/corelib/Const.kt Просмотреть файл

@@ -1,8 +1,11 @@
1 1
 package com.xhly.corelib
2 2
 
3
+import android.bluetooth.BluetoothManager
4
+
3 5
 
4 6
 object Const {
5
-    const val  isDebug=true
7
+    const val isDebug = true
8
+
6 9
     //接口地址
7 10
     const val URL1: String = "https://mcapitest.xhkjedu.com/"
8 11
 
@@ -10,10 +13,10 @@ object Const {
10 13
     const val WSURL: String = "wss://mcwstest.xhkjedu.com/ws"
11 14
 
12 15
     //标记平板禁用(来源于socket)
13
-    const val DISABLEPAD= "DISABLEPAD"
16
+    const val DISABLEPAD = "DISABLEPAD"
14 17
 
15 18
     //标记平板在禁用的事件范围(来源于策略)
16
-    const val DISABLEPADTIME= "DISABLEPADTIME"
19
+    const val DISABLEPADTIME = "DISABLEPADTIME"
17 20
 
18 21
     //作为存储白名单应用的key
19 22
     const val WHITELISTAPP = "WHITELISTAPP"
@@ -60,6 +63,22 @@ object Const {
60 63
     //标记应用卸载
61 64
     const val APPUNINSTALL = "APPUNINSTALL"
62 65
 
66
+
67
+    //标记SD卡安装
68
+    const val SDINSTALL = "SDINSTALL"
69
+
70
+    //标记SD卡卸载
71
+    const val SDUNINSTALL = "SDUNINSTALL"
72
+
73
+    //标记SIM卡安装事件
74
+    const val SIMINSTALL = "SIMINSTALL"
75
+
76
+    //标记SIM卡卸载事件
77
+    const val SIMUNINSTALL = "SIMUNINSTALL"
78
+
79
+    //标记蓝牙打开
80
+    const val BLUETOOTHOPEN = "BLUETOOTHOPEN"
81
+
63 82
     //标记更新策略
64 83
     const val UPDATESTRATEGY = "UPDATESTRATEGY"
65 84
 

Загрузка…
Отмена
Сохранить