Observing Network Connection State using Jetpack Compose - Android
HTML-код
- Опубликовано: 19 окт 2024
- In this video, we will create an app that observes when the network connectivity changes using Jetpack Compose and Kotlin
Repository: github.com/dan...
Let's connect 🤗:
/ daniel-atitienei
Blog: / daniel.atitienei
Do you like my content? Here you can support me with a coffee ☕:
www.buymeacoff...
Ur a genius
this is what I have:
fun hasConnection(context: Context): Boolean {
val connectivityManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val capabilities = connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)
if (capabilities != null) {
if (capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
return true
}
}
return false
}
please can you make a tutorial on bluetooth with jetpack compose
Sure
Keep going 🫶🏻
I will thanks!
good video❤, in which layer can be this code in clear architecture?
Thanks! You can put all the code in the data layer > network excluding the @Composable one. That one you can put it in the presentation layer in a package called util
@@daniel.atitienei thanks❤
You're welcome!
Наверное, стоит еще указывать и permission для этого дела.
Hi already available code regarding network connection or not , in these code any specification sir
Love it❤ bro can u make a video on that show nearby wifi network and and show how is cnnected to my wifi
Will try!
@@daniel.atitienei Thank u Sir
suspend fun getNearbyWifiNetworks(): List {
val response = context.getNearbyWifiNetworks()
return response
}
fun Context.getNearbyWifiNetworks(): List {
val wifiManager =
applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
if (ContextCompat.checkSelfPermission(
this,
Manifest.permission.ACCESS_FINE_LOCATION
) != PackageManager.PERMISSION_GRANTED
) {
return emptyList()
}
wifiManager.startScan()
return wifiManager.scanResults
}
if i work wifi but not have internet what will show?
I forgot to check for that in the video, but to do that you can have another variable in the getCurrentConnectivityState that checks if the network has NetworkCapabilities.NET_CAPABILITY_VALIDATED
@@daniel.atitienei thanks, keep going ❤️❤️
Why you are not using Broadcast Receiver?
In this case is not really needed and for directly observing this in our UI I find this way more suitable.
After sdk 24 broadcast receivers don't work for this
bro, work on your energy
I will 😅
Don't be rude