Created
March 18, 2023 17:05
-
-
Save Morteza-QN/bbf8c5d720bda9d524fe9673617df68d to your computer and use it in GitHub Desktop.
check for internet connectivity when the network state changes
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
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val networkInfo = connectivityManager.activeNetworkInfo | |
return networkInfo != null && networkInfo.isConnected | |
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val network = connectivityManager.activeNetwork | |
val capabilities = connectivityManager.getNetworkCapabilities(network) | |
return capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true | |
fun isInternetAvailable(): Boolean { | |
return try { | |
val process = Runtime.getRuntime().exec("/system/bin/ping -c 1 google.com") | |
val returnVal = process.waitFor() | |
return returnVal == 0 | |
} catch (e: Exception) { | |
false | |
} | |
} | |
//---------------------------------------------- | |
val networkCallback = object : ConnectivityManager.NetworkCallback() { | |
override fun onAvailable(network: Network) { | |
super.onAvailable(network) | |
// Network is available, update state | |
networkStateFlow.value = NetworkState.Connected | |
} | |
override fun onLost(network: Network) { | |
super.onLost(network) | |
// Network is lost, update state | |
networkStateFlow.value = NetworkState.Disconnected | |
} | |
} | |
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
connectivityManager.registerDefaultNetworkCallback(networkCallback) | |
private val networkStateFlow = MutableStateFlow(getInitialNetworkState()) | |
private fun getInitialNetworkState(): NetworkState { | |
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val networkInfo = connectivityManager.activeNetworkInfo | |
return if (networkInfo != null && networkInfo.isConnected) { | |
NetworkState.Connected | |
} else { | |
NetworkState.Disconnected | |
} | |
} | |
fun observeNetworkState(): Flow<NetworkState> { | |
return networkStateFlow | |
} | |
// Observe the Flow object in your ViewModel or Activity to receive updates whenever the network state changes. | |
viewModelScope.launch { | |
observeNetworkState().collect { state -> | |
when (state) { | |
NetworkState.Connected -> { | |
// Network is connected, perform required tasks | |
} | |
NetworkState.Disconnected -> { | |
// Network is disconnected, handle accordingly | |
} | |
} | |
} | |
} | |
//------------------------------------------ | |
@AndroidEntryPoint | |
class NetworkCallbackImpl @Inject constructor( | |
private val connectivityManager: ConnectivityManager, | |
private val networkStateFlow: MutableStateFlow<NetworkState> | |
) : ConnectivityManager.NetworkCallback() { | |
override fun onAvailable(network: Network) { | |
super.onAvailable(network) | |
networkStateFlow.value = NetworkState.Connected | |
} | |
override fun onLost(network: Network) { | |
super.onLost(network) | |
networkStateFlow.value = NetworkState.Disconnected | |
} | |
} | |
@HiltAndroidApp | |
class MyApplication : Application() { | |
@Inject | |
lateinit var networkCallback: NetworkCallbackImpl | |
override fun onCreate() { | |
super.onCreate() | |
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
connectivityManager.registerDefaultNetworkCallback(networkCallback) | |
} | |
} | |
@Singleton | |
class NetworkStateRepository @Inject constructor( | |
private val context: Context | |
) { | |
private val networkStateFlow = MutableStateFlow(getInitialNetworkState()) | |
private fun getInitialNetworkState(): NetworkState { | |
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager | |
val networkInfo = connectivityManager.activeNetworkInfo | |
return if (networkInfo != null && networkInfo.isConnected) { | |
NetworkState.Connected | |
} else { | |
NetworkState.Disconnected | |
} | |
} | |
fun observeNetworkState(): Flow<NetworkState> { | |
return networkStateFlow | |
} | |
} | |
@AndroidEntryPoint | |
class MyViewModel @Inject constructor( | |
private val networkStateRepository: NetworkStateRepository | |
) : ViewModel() { | |
fun observeNetworkState(): Flow<NetworkState> { | |
return networkStateRepository.observeNetworkState() | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment