I recently updated some Java code to Kotlin, and then updated the original NetReciever to kotlin, and then as gave me the following error:

WTF? What’s going on here? Read the instructions carefully…

CONNECTIVITY_ACTION is obsolete. Here the official recommended requestNetwork, registerNetworkCallbackor registerDefaultNetworkCallback to replace the original BroadcastReciever scheme, in that case, try to transform.

Firstly, different listening schemes are distinguished according to different versions

The main method

// Network change listener
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
    mConnectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
    val networkCallbackImpl = NetworkCallbackImpl()
    mConnectivityManager.requestNetwork(NetworkRequest.Builder().build(),networkCallbackImpl)
}else{
    networkReceiver =  NetWorkReciever()
    val netFilter = IntentFilter()
    netFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION)
    registerReceiver(networkReceiver,netFilter)

}
Copy the code

NetworkCallbackImpl

@RequiresApi(Build.VERSION_CODES.LOLLIPOP)
class NetworkCallbackImpl: ConnectivityManager.NetworkCallback(a){


    val TAG = "NetworkCallbackImpl"

    override fun onAvailable(network: Network) {
        super.onAvailable(network)
        Log.i(TAG,"The network is connected")}override fun onLost(network: Network) {
        super.onLost(network)
        Log.i(TAG,"Network disconnected")}override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
        super.onCapabilitiesChanged(network, networkCapabilities)
        if (networkCapabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)) {
            when {
                //WIFFI
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> {
                    Log.i(TAG,"Wifi is connected")}// Data traffic
                networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> {
                    Log.i(TAG,"Data traffic already connected")}else -> {
                    Log.i(TAG,"Other Networks")}}}}}Copy the code

Then add the original NetWorkReciever code

class NetWorkReciever:BroadcastReceiver(a){

    companion object{
        const val TAG = "NetWork"
    }

    override fun onReceive(context: Context? , intent: Intent?) {
        if(ConnectivityManager.CONNECTIVITY_ACTION==intent? .action){// Network changes
            Log.i(TAG,"NETWORK_CHANGE") val connectivityManager = context? .getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager val info = connectivityManager.activeNetworkInfo
            if (info ! =null && info.isConnected) {
                val type = info.type
                Log.i(TAG,"network isconnected ,type is $type")}else {
                Log.i(TAG,"network disconnected ,type is -1")}}}}Copy the code

Here you add two permissions to androidmanifest.xml

<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
Copy the code

Register NetWorkReciever again

<receiver android:name=".reciever.NetWorkReciever">
    <intent-filter>
        <action android:name="android.intent.action.CONNECTIVITY_CHANGE"/>
    </intent-filter>
Copy the code

Final running effect

Code address: github.com/jiaoery/MyA… (There are also some other code learning notes)