Skip to content

Commit

Permalink
添加防抖参数
Browse files Browse the repository at this point in the history
  • Loading branch information
kongqw committed Jan 12, 2021
1 parent 08a5cb1 commit d4fda13
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 23 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand All @@ -37,6 +37,17 @@ dependencies {
NetworkMonitorManager.getInstance().init(this)
```


``` kotlin
/**
* 初始化
* @param application 上下文
* @param jitterTime 设置抖动时间(即多久之后网络状态没有发生变化,才回调网络状态,单位:毫秒,默认1500毫秒)
*/
NetworkMonitorManager.getInstance().init(this, 1_500)
```

### 注册与反注册

在需要监听网络状态的页面进行注册与反注册
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionName "1.1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
30 changes: 15 additions & 15 deletions app/src/main/java/com/kongqw/networkmonitor/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
// }
}
2 changes: 1 addition & 1 deletion monitor/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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<Any, ArrayList<NetworkStateReceiverMethod>> = 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)
}

Expand Down Expand Up @@ -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)
}
}
}
Expand Down

0 comments on commit d4fda13

Please sign in to comment.