Browse Source

1.添加网络状态广播

20241218TB223FC(测试jar包)
wangwanlei 10 months ago
parent
commit
37b4b8f920

+ 2
- 2
app/src/main/AndroidManifest.xml View File

@@ -108,7 +108,7 @@
108 108
                 <action android:name="android.bluetooth.BluetoothAdapter.STATE_ON" />
109 109
             </intent-filter>
110 110
         </receiver>
111
-        <receiver
111
+<!--        <receiver
112 112
             android:name="com.xhly.manageapp.broadcastreceiver.NetConnectReceiver"
113 113
             android:enabled="true"
114 114
             android:exported="true">
@@ -117,7 +117,7 @@
117 117
                 <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
118 118
                 <action android:name="android.net.wifi.STATE_CHANGE" />
119 119
             </intent-filter>
120
-        </receiver>
120
+        </receiver>-->
121 121
         <receiver
122 122
             android:name="com.xhly.manageapp.broadcastreceiver.SimStateReceive"
123 123
             android:enabled="true"

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

@@ -632,7 +632,7 @@ class ManageApplication : MultiDexApplication() {
632 632
                 //给自己加入网络白名单
633 633
                 addAppWhiteRule(arrayListOf(Const.CURRENTAPPPKG))
634 634
                 //禁止状态栏下拉,屏蔽底部虚拟键。
635
-                disableStatusBarPanel(true)
635
+                disableStatusBarPanel(false)
636 636
                 hideHomeSoftKey(true)
637 637
                 hideMenuSoftKey(true)
638 638
                 enableAccessibility()

+ 16
- 26
app/src/main/java/com/xhly/manageapp/broadcastreceiver/NetConnectReceiver.kt View File

@@ -1,34 +1,24 @@
1
-package com.xhly.manageapp.broadcastreceiver;
1
+package com.xhly.manageapp.broadcastreceiver
2 2
 
3
-import android.content.BroadcastReceiver;
4
-import android.content.Context;
5
-import android.content.Intent;
6
-import android.net.ConnectivityManager;
7
-import android.net.NetworkInfo;
3
+import android.content.BroadcastReceiver
4
+import android.content.Context
5
+import android.content.Intent
6
+import android.net.ConnectivityManager
7
+import com.xhly.corelib.Const
8
+import com.xhly.corelib.eventbus.UIEvent
9
+import com.xhly.corelib.utils.LogShow
8 10
 
9
-import com.xhly.corelib.Const;
10
-import com.xhly.corelib.eventbus.UIEvent;
11
-
12
-class NetConnectReceiver extends BroadcastReceiver {
13
-    @Override
14
-    public void onReceive(Context context, Intent intent) {
15
-        ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
16
-        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
17
-
18
-        if (networkInfo != null && networkInfo.isConnected()) {
11
+class NetConnectReceiver : BroadcastReceiver() {
12
+    override fun onReceive(context: Context, intent: Intent) {
13
+        val connMgr = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
14
+        val networkInfo = connMgr.activeNetworkInfo
15
+        if (networkInfo != null && networkInfo.isConnected) {
19 16
             // 网络连接已建立
20
-            new UIEvent(Const.STARTSOCKET).post();
17
+            UIEvent(Const.STARTSOCKET).post()
18
+            LogShow("网络连接")
21 19
         } else {
22 20
             // 网络连接断开
23
-
21
+            LogShow("网络断开")
24 22
         }
25 23
     }
26
-/*  override fun onReceive(context: Context?, intent: Intent?) {
27
-    Log.e("qingshan", "网络状态改变")
28
-    context?.getSystemService(ConnectivityManager::class.java)?.allNetworkInfo?.filter {
29
-      it.typeName == "MOBILE" || it.typeName == "WIFI"
30
-    }?.forEach {networkInfo ->
31
-      Log.e("qingshan", "${networkInfo?.typeName}, isConnect= ${networkInfo?.isConnected}")
32
-    }
33
-  }*/
34 24
 }

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

@@ -11,6 +11,7 @@ import android.content.pm.PackageManager
11 11
 import android.graphics.Color
12 12
 import android.graphics.drawable.BitmapDrawable
13 13
 import android.graphics.drawable.Drawable
14
+import android.net.ConnectivityManager
14 15
 import android.os.Build
15 16
 import android.provider.Settings
16 17
 import android.view.View
@@ -60,6 +61,7 @@ import com.xhly.manageapp.bean.school.SchoolDeviceSetBean
60 61
 import com.xhly.manageapp.bean.strategy.StrategyBean
61 62
 import com.xhly.manageapp.bean.user.UserBean
62 63
 import com.xhly.manageapp.broadcastreceiver.AppInstallReceiver
64
+import com.xhly.manageapp.broadcastreceiver.NetConnectReceiver
63 65
 import com.xhly.manageapp.service.ManageAccessibilityService
64 66
 import com.xhly.manageapp.service.websocket.AppSocket
65 67
 import com.xhly.manageapp.ui.ManageActivity
@@ -85,6 +87,7 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
85 87
     var adapter: AppAdapter? = null
86 88
 
87 89
     private var mMyInstallReceiver: AppInstallReceiver? = null
90
+    private var netConnectReceiver:NetConnectReceiver?=null
88 91
     private var dataList: ArrayList<AppInfo> = arrayListOf()
89 92
     private var userBean: UserBean? = null
90 93
     private var timer: Timer? = null
@@ -134,7 +137,9 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
134 137
         mBinding.btn.setOnClickListener {
135 138
             startIntentActivity(ManageActivity().javaClass)
136 139
         }
137
-        registerAppInstallReceiver()/*
140
+        registerAppInstallReceiver()
141
+        registerNetConnectReceiver()
142
+        /*
138 143
          使用情况权限
139 144
          val permissionIntent = Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS)
140 145
           startActivity(permissionIntent)*/
@@ -1055,7 +1060,12 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
1055 1060
         intentFilter.addAction(Intent.ACTION_PACKAGE_REPLACED)
1056 1061
         intentFilter.addDataScheme("package")
1057 1062
         mMyInstallReceiver = AppInstallReceiver()
1058
-        registerReceiver(mMyInstallReceiver, intentFilter);
1063
+        registerReceiver(mMyInstallReceiver, intentFilter)
1064
+    }
1065
+
1066
+    private fun registerNetConnectReceiver(){
1067
+        netConnectReceiver = NetConnectReceiver()
1068
+        registerReceiver(netConnectReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
1059 1069
     }
1060 1070
 
1061 1071
 
@@ -1151,6 +1161,9 @@ class MainActivity : BaseActivity<MainViewModel, ActivityMainBinding>() {
1151 1161
         mMyInstallReceiver?.let {
1152 1162
             unregisterReceiver(it)
1153 1163
         }
1164
+        netConnectReceiver?.let {
1165
+            unregisterReceiver(it)
1166
+        }
1154 1167
         LogShow("MainActivity被销毁")
1155 1168
     }
1156 1169
 

Loading…
Cancel
Save