diff --git a/README.md b/README.md index 09a5048..0816da0 100644 --- a/README.md +++ b/README.md @@ -24,7 +24,7 @@ Step 2. Add the dependency ``` glide dependencies { - implementation 'com.github.kongqw:NetworkMonitor:1.0.1' + implementation 'com.github.kongqw:NetworkMonitor:1.1.0' } ``` @@ -37,6 +37,17 @@ dependencies { NetworkMonitorManager.getInstance().init(this) ``` +或 + +``` kotlin +/** + * 初始化 + * @param application 上下文 + * @param jitterTime 设置抖动时间(即多久之后网络状态没有发生变化,才回调网络状态,单位:毫秒,默认1500毫秒) + */ +NetworkMonitorManager.getInstance().init(this, 1_500) +``` + ### 注册与反注册 在需要监听网络状态的页面进行注册与反注册 diff --git a/app/build.gradle b/app/build.gradle index dcca109..d4d5f2c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -12,7 +12,7 @@ android { minSdkVersion 21 targetSdkVersion 30 versionCode 1 - versionName "1.0" + versionName "1.1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" } buildTypes { diff --git a/app/src/main/java/com/kongqw/networkmonitor/MainActivity.kt b/app/src/main/java/com/kongqw/networkmonitor/MainActivity.kt index 31feab7..fa022e9 100644 --- a/app/src/main/java/com/kongqw/networkmonitor/MainActivity.kt +++ b/app/src/main/java/com/kongqw/networkmonitor/MainActivity.kt @@ -55,19 +55,19 @@ class MainActivity : AppCompatActivity() { } } } - - @NetworkMonitor(monitorFilter = [NetworkState.WIFI, NetworkState.CELLULAR]) - fun onNetWorkStateChangeWIFI(networkState: NetworkState) { - Log.i(TAG, "onNetWorkStateChangeWIFI networkState = $networkState") - } - - @NetworkMonitor(monitorFilter = [NetworkState.CELLULAR]) - fun onNetWorkStateChangeCellular(networkState: NetworkState) { - Log.i(TAG, "onNetWorkStateChangeCellular networkState = $networkState") - } - - @NetworkMonitor(monitorFilter = [NetworkState.NONE]) - fun onNetWorkStateChangeNONE(networkState: NetworkState) { - Log.i(TAG, "onNetWorkStateChangeNONE networkState = $networkState") - } +// +// @NetworkMonitor(monitorFilter = [NetworkState.WIFI, NetworkState.CELLULAR]) +// fun onNetWorkStateChangeWIFI(networkState: NetworkState) { +// Log.i(TAG, "onNetWorkStateChangeWIFI networkState = $networkState") +// } +// +// @NetworkMonitor(monitorFilter = [NetworkState.CELLULAR]) +// fun onNetWorkStateChangeCellular(networkState: NetworkState) { +// Log.i(TAG, "onNetWorkStateChangeCellular networkState = $networkState") +// } +// +// @NetworkMonitor(monitorFilter = [NetworkState.NONE]) +// fun onNetWorkStateChangeNONE(networkState: NetworkState) { +// Log.i(TAG, "onNetWorkStateChangeNONE networkState = $networkState") +// } } diff --git a/monitor/build.gradle b/monitor/build.gradle index 731fd4f..702ae18 100644 --- a/monitor/build.gradle +++ b/monitor/build.gradle @@ -12,7 +12,7 @@ android { minSdkVersion 21 targetSdkVersion 30 versionCode 1 - versionName "1.0" + versionName "1.1.0" testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner" consumerProguardFiles 'consumer-rules.pro' diff --git a/monitor/src/main/java/com/kongqw/network/monitor/NetworkMonitorManager.kt b/monitor/src/main/java/com/kongqw/network/monitor/NetworkMonitorManager.kt index 4ed0de1..e22dd1d 100644 --- a/monitor/src/main/java/com/kongqw/network/monitor/NetworkMonitorManager.kt +++ b/monitor/src/main/java/com/kongqw/network/monitor/NetworkMonitorManager.kt @@ -22,6 +22,9 @@ class NetworkMonitorManager private constructor() { private const val ANDROID_NET_CHANGE_ACTION = "android.net.conn.CONNECTIVITY_CHANGE" + // 默认网络防抖时间 + private const val DEFAULT_JITTER_TIME: Long = 1_500 + @JvmStatic private var INSTANCE: NetworkMonitorManager? = null @@ -32,13 +35,28 @@ class NetworkMonitorManager private constructor() { } private var mApplication: Application? = null + private var mJitterTime: Long = DEFAULT_JITTER_TIME private var mNetworkBroadcastReceiver = NetworkBroadcastReceiver() private var mNetworkCallback = NetworkCallback() private var netWorkStateChangedMethodMap: HashMap> = HashMap() private val mUiHandler = Handler(Looper.getMainLooper()) - fun init(application: Application) { + private var mLastNetworkState: NetworkState? = null + private var mRunnable: Runnable? = null + + /** + * 初始化 + * @param application 上下文 + * @param jitterTime 设置抖动时间 + */ + fun init(application: Application, jitterTime: Long = DEFAULT_JITTER_TIME) { mApplication = application + mJitterTime = jitterTime.let { + if (it < 0) { + return@let 0 + } + return@let it + } initMonitor(application) } @@ -90,11 +108,39 @@ class NetworkMonitorManager private constructor() { } } + private fun postNetworkState(networkState: NetworkState) { - for ((_, methods) in netWorkStateChangedMethodMap) { - methods.forEach { networkStateReceiverMethod -> - if (true == networkStateReceiverMethod.monitorFilter?.contains(networkState)) { - mUiHandler.post { networkStateReceiverMethod.method?.invoke(networkStateReceiverMethod.any, networkState) } + // Log.i("NetworkMonitorManager", "postNetworkState($networkState)") + if (mLastNetworkState == networkState) { + // Log.i("NetworkMonitorManager", "已经回调过该状态,不再多次回调") + return + } + + mRunnable?.also { mUiHandler.removeCallbacks(it) } + + + mRunnable = Runnable { + for ((_, methods) in netWorkStateChangedMethodMap) { + + methods.forEach { networkStateReceiverMethod -> + if (true == networkStateReceiverMethod.monitorFilter?.contains(networkState)) { + networkStateReceiverMethod.method?.invoke(networkStateReceiverMethod.any, networkState) + // 记录最后一次回调的网络状态 + mLastNetworkState = networkState + } + } + } + } + + when (networkState) { + NetworkState.NONE -> { + mRunnable?.also { + mUiHandler.postDelayed(it, mJitterTime) + } + } + else -> { + mRunnable?.also { + mUiHandler.postDelayed(it, mJitterTime) } } }