@@ -16,7 +16,7 @@ import com.kongqw.network.monitor.util.AnnotationUtils
1616import com.kongqw.network.monitor.util.NetworkStateUtils
1717import java.util.*
1818
19- class NetworkMonitorManager private constructor() : ConnectivityManager.NetworkCallback() {
19+ class NetworkMonitorManager private constructor() {
2020
2121 companion object {
2222
@@ -33,6 +33,7 @@ class NetworkMonitorManager private constructor() : ConnectivityManager.NetworkC
3333
3434 private var mApplication: Application ? = null
3535 private var mNetworkBroadcastReceiver = NetworkBroadcastReceiver ()
36+ private var mNetworkCallback = NetworkCallback ()
3637 private var netWorkStateChangedMethodMap: HashMap <Any , ArrayList <NetworkStateReceiverMethod >> = HashMap ()
3738 private val mUiHandler = Handler (Looper .getMainLooper())
3839
@@ -46,10 +47,10 @@ class NetworkMonitorManager private constructor() : ConnectivityManager.NetworkC
4647
4748 when {
4849 Build .VERSION .SDK_INT >= Build .VERSION_CODES .N -> {
49- connectivityManager?.registerDefaultNetworkCallback(this )
50+ connectivityManager?.registerDefaultNetworkCallback(mNetworkCallback )
5051 }
5152 Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP -> {
52- connectivityManager?.registerNetworkCallback(NetworkRequest .Builder ().build(), this )
53+ connectivityManager?.registerNetworkCallback(NetworkRequest .Builder ().build(), mNetworkCallback )
5354 }
5455 else -> {
5556 val intentFilter = IntentFilter ().apply { addAction(ANDROID_NET_CHANGE_ACTION ) }
@@ -66,19 +67,20 @@ class NetworkMonitorManager private constructor() : ConnectivityManager.NetworkC
6667 any?.apply { netWorkStateChangedMethodMap.remove(this ) }
6768 }
6869
69- override fun onAvailable (network : Network ) {
70- super .onAvailable(network)
71- postNetworkState(NetworkStateUtils .getNetworkState(mApplication?.applicationContext))
72- }
70+ internal inner class NetworkCallback : ConnectivityManager .NetworkCallback () {
7371
74- override fun onLost (network : Network ) {
75- super .onLost(network)
76- postNetworkState(NetworkState .NONE )
77- // postNetworkState(NetworkStateUtils.getNetworkState(mApplication?.applicationContext))
78- }
72+ override fun onAvailable (network : Network ) {
73+ super .onAvailable(network)
74+ postNetworkState(NetworkStateUtils .getNetworkState(mApplication?.applicationContext))
75+ }
7976
77+ override fun onLost (network : Network ) {
78+ super .onLost(network)
79+ postNetworkState(NetworkState .NONE )
80+ }
81+ }
8082
81- inner class NetworkBroadcastReceiver : BroadcastReceiver () {
83+ internal inner class NetworkBroadcastReceiver : BroadcastReceiver () {
8284 override fun onReceive (context : Context ? , intent : Intent ? ) {
8385 when (intent?.action) {
8486 ANDROID_NET_CHANGE_ACTION -> {
0 commit comments