Last active
August 23, 2022 18:26
-
-
Save ItsBenyaamin/b2f96ce97656a84b682f034099e06df8 to your computer and use it in GitHub Desktop.
a singleton class to check the network connection state, use both of the NetworkCallback and ConnectivityManager to support older devices
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.graymind75 | |
import android.content.Context | |
import android.net.* | |
import android.os.Build | |
import androidx.annotation.RequiresApi | |
/** | |
* singleton class to check the network connection state | |
* It's use the old ConnectivityManager.activeNetworkInfo way for pre LOLLIPOP | |
* and the new API named NetworkCallback for the devices with Android API version LOLLIPOP and above | |
* | |
* required permissions: | |
* <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> | |
* <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> | |
*/ | |
object NetworkHelper { | |
private val connectivityManager: ConnectivityManager = AppClass.instance.applicationContext | |
.getSystemService(Context.CONNECTIVITY_SERVICE) | |
as ConnectivityManager | |
private var isNetworkAvailable = false; | |
/** | |
* check the SDK_INT of device and register to the new API for connection callbacks | |
* because the new API only support the API 21 and above | |
*/ | |
init { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { | |
val networkRequest = NetworkRequest.Builder() | |
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI) | |
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR) | |
.build() | |
connectivityManager.registerNetworkCallback(networkRequest, networkCallback()) | |
} | |
} | |
/** | |
* new API callbacks to determine which status we currently in | |
* we need the below callbacks: | |
* - onAvailable: device connected to a network of course | |
* - onLost: when the connection completely lost | |
* - onUnavailable: when somehow the connection cannot be reached | |
*/ | |
private fun networkCallback() = @RequiresApi(Build.VERSION_CODES.LOLLIPOP) | |
object: ConnectivityManager.NetworkCallback() { | |
override fun onLost(network: Network) { | |
super.onLost(network) | |
isNetworkAvailable = false | |
} | |
override fun onUnavailable() { | |
super.onUnavailable() | |
isNetworkAvailable = false | |
} | |
override fun onAvailable(network: Network) { | |
super.onAvailable(network) | |
isNetworkAvailable = true | |
} | |
} | |
/** | |
* check the network state with the old way for APIs below LOLLIPOP | |
*/ | |
private fun checkIsNetworkAvailable(): Boolean { | |
val activeNetworkInfo = connectivityManager.activeNetworkInfo | |
return activeNetworkInfo != null && activeNetworkInfo.isConnected | |
} | |
/** | |
* the function for call from outside the class to check the connection status | |
* first check the SDK_INT then provide the status with the right API | |
*/ | |
fun checkNetwork(): Boolean { | |
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) | |
return isNetworkAvailable | |
return checkIsNetworkAvailable() | |
} | |
} |
Hi, I assume you are using minSdk 23
So:
class MainActivity : AppCompatActivity(), ConnectivityManager.NetworkCallback {
private lateinit var connectivityManager: ConnectivityManager
var isNetworkAvailable = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
init()
}
fun init() {
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR)
.build()
connectivityManager.registerNetworkCallback(networkRequest, this@MainActivity)
}
override fun onLost(network: Network) {
super.onLost(network)
isNetworkAvailable = false
}
override fun onUnavailable() {
super.onUnavailable()
isNetworkAvailable = false
}
override fun onAvailable(network: Network) {
super.onAvailable(network)
isNetworkAvailable = true
}
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey,
can you please give an example of getting these callbacks on activity/fragments.