瀏覽代碼

1.添加网络状态广播

20241218TB223FC(测试jar包)
wangwanlei 10 月之前
父節點
當前提交
37b4b8f920

+ 2
- 2
app/src/main/AndroidManifest.xml 查看文件

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

+ 1
- 1
app/src/main/java/com/xhly/manageapp/ManageApplication.kt 查看文件

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

+ 16
- 26
app/src/main/java/com/xhly/manageapp/broadcastreceiver/NetConnectReceiver.kt 查看文件

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
         } else {
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 查看文件

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

Loading…
取消
儲存