|
|
@ -4,20 +4,22 @@ import android.content.BroadcastReceiver |
|
|
|
import android.content.Context |
|
|
|
import android.content.Context |
|
|
|
import android.content.Intent |
|
|
|
import android.content.Intent |
|
|
|
import android.content.IntentFilter |
|
|
|
import android.content.IntentFilter |
|
|
|
|
|
|
|
import android.net.ConnectivityManager |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 监测网络变化 |
|
|
|
* 监测网络变化 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class NetworkChangedReceiver : BroadcastReceiver() { |
|
|
|
class NetworkChangedReceiver : BroadcastReceiver() { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var onReceiver: ((context: Context, intent: Intent) -> Unit)? = null |
|
|
|
|
|
|
|
|
|
|
|
val filter = IntentFilter().apply { |
|
|
|
val filter = IntentFilter().apply { |
|
|
|
addAction("android.net.conn.CONNECTIVITY_CHANGE") |
|
|
|
@Suppress("DEPRECATION") |
|
|
|
addAction("android.net.wifi.WIFI_STATE_CHANGED") |
|
|
|
addAction(ConnectivityManager.CONNECTIVITY_ACTION) |
|
|
|
addAction("android.net.wifi.STATE_CHANGE") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
override fun onReceive(context: Context?, intent: Intent?) { |
|
|
|
override fun onReceive(context: Context, intent: Intent) { |
|
|
|
|
|
|
|
onReceiver?.invoke(context, intent) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |