check if network is metered android

To check if the network is metered in Android using Kotlin, you can use the following code:

val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val network = connectivityManager.activeNetwork
val networkCapabilities = connectivityManager.getNetworkCapabilities(network)
val isMetered = networkCapabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED) == false

Explanation: 1. Obtain the ConnectivityManager system service to access information about the network. 2. Retrieve the active network using the activeNetwork property. 3. Get the network capabilities using the getNetworkCapabilities method of the ConnectivityManager. 4. Check if the network is metered by using the hasCapability method with the NET_CAPABILITY_NOT_METERED capability.