Skip to main content

How to Detect Internet Connectivity Changes in Android

How to Detect Internet Connectivity Changes in Android

How to Detect Internet Connectivity Changes in Android

In today’s mobile-first world, apps depend heavily on stable internet connections to deliver smooth and seamless user experiences. Whether you're streaming content, syncing data, or communicating in real-time, understanding how to detect and respond to internet connectivity changes is crucial for any Android developer. In this comprehensive guide, we’ll explore the best practices and practical techniques to detect internet connectivity changes in Android using Kotlin.

Why Detect Network Connectivity Changes?

Not all network disruptions are equal. Sometimes users move between Wi-Fi and cellular data, lose signal temporarily, or switch to airplane mode. Handling these scenarios gracefully can improve your app’s reliability and user satisfaction by:

  • Preventing failed network requests
  • Displaying meaningful offline alerts or retry mechanisms
  • Managing data synchronization intelligently based on network state
  • Reducing unnecessary battery drain by avoiding network calls when offline

Detecting connectivity changes dynamically allows your app to respond in real-time, enhancing performance and user engagement.

Understanding Android’s Network APIs

Android provides several ways to monitor network connectivity changes, evolving with newer API levels. Here's a quick overview:

  • ConnectivityManager.NetworkCallback (API 21+): The recommended modern way to listen for network changes by registering callbacks.
  • BroadcastReceiver for CONNECTIVITY_ACTION (deprecated in API 24): Previously popular method but not reliable on newer versions.
  • NetworkCapabilities: Get detailed info on the type and capabilities of the current network.

For a robust and future-proof implementation, we use ConnectivityManager.NetworkCallback combined with NetworkCapabilities. This approach offers granular control over network events and status.

Setting Up Permissions

Before diving into the implementation, ensure your app has the required permissions declared in the AndroidManifest.xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

The ACCESS_NETWORK_STATE permission enables your app to query network status and listen for changes.

Detecting Connectivity Changes with NetworkCallback in Kotlin

Let's walk through a practical example that demonstrates how to listen for connectivity changes efficiently.

Step 1: Creating a Network Monitor Class

This class will register and unregister network callbacks and expose connectivity status updates using a Kotlin Flow or LiveData. For simplicity, we’ll use LiveData here, which integrates well with Android’s lifecycle components.

import android.content.Context
import android.net.ConnectivityManager
import android.net.Network
import android.net.NetworkCapabilities
import android.net.NetworkRequest
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

class NetworkMonitor(private val context: Context) {

    private val _isConnected = MutableLiveData<Boolean>()
    val isConnected: LiveData<Boolean> get() = _isConnected

    private val connectivityManager =
        context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager

    private val networkCallback = object : ConnectivityManager.NetworkCallback() {
        override fun onAvailable(network: Network) {
            // Network is available
            _isConnected.postValue(true)
        }

        override fun onLost(network: Network) {
            // Network lost
            _isConnected.postValue(false)
        }
    }

    fun startNetworkCallback() {
        val networkRequest = NetworkRequest.Builder()
            .addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
            .build()

        // Initial state check
        val activeNetwork = connectivityManager.activeNetwork
        val capabilities = connectivityManager.getNetworkCapabilities(activeNetwork)
        _isConnected.postValue(capabilities?.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET) == true)

        // Register the callback
        connectivityManager.registerNetworkCallback(networkRequest, networkCallback)
    }

    fun stopNetworkCallback() {
        connectivityManager.unregisterNetworkCallback(networkCallback)
    }
}

How It Works:

  • We create a NetworkMonitor class which takes Context.
  • Using ConnectivityManager, we register a NetworkCallback to listen for network availability and loss events.
  • LiveData<Boolean> exposes the current connectivity status that UI components or ViewModels can observe.
  • The startNetworkCallback() method performs an immediate connectivity check and registers the callback.
  • stopNetworkCallback() unregisters the callback to prevent memory leaks.

Using the Network Monitor in an Activity or ViewModel

Here's a common pattern to observe network status changes and update UI accordingly.

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.lifecycle.Observer

class MainActivity : AppCompatActivity() {

    private lateinit var networkMonitor: NetworkMonitor

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        networkMonitor = NetworkMonitor(this)

        networkMonitor.isConnected.observe(this, Observer { isConnected ->
            if (isConnected) {
                // Network is available
                showMessage("You are connected to the Internet!")
            } else {
                // Network lost
                showMessage("No internet connection!")
            }
        })
    }

    override fun onStart() {
        super.onStart()
        networkMonitor.startNetworkCallback()
    }

    override fun onStop() {
        super.onStop()
        networkMonitor.stopNetworkCallback()
    }

    private fun showMessage(message: String) {
        // Implement your UI code here, e.g. Toast or Snackbar
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
    }
}

Handling Edge Cases and API Compatibility

API Level Compatibility: The above solution works on Android Lollipop (API 21) and above since NetworkCallback was introduced in API 21. For older devices, a fallback mechanism using a BroadcastReceiver for CONNECTIVITY_ACTION might be necessary, but those legacy devices are becoming rare.

Detecting Actual Internet Access: Note that a network being "available" doesn’t always mean the internet is accessible. Sometimes networks are captive portals or have no upstream internet connectivity. To handle this, you can implement additional network validation by pinging a reliable server, but this requires asynchronous network calls and may introduce latency.

Battery Considerations: Continuously monitoring connectivity changes is light on resources when using NetworkCallback. However, always unregister callbacks when the monitoring is not needed to keep battery usage optimal.

Summary

Detecting internet connectivity changes in Android apps is vital for creating responsive and user-friendly applications. Using ConnectivityManager.NetworkCallback along with Kotlin’s modern language features offers a clean, efficient, and highly responsive approach to monitoring your app’s network state.

We walked through:

  • Why network connectivity detection matters
  • Relevant Android APIs and permissions
  • A detailed Kotlin implementation with NetworkCallback and LiveData
  • A practical example integrating the network monitor into an activity
  • Best practices and edge cases to consider

By integrating these techniques, your app can gracefully adapt to network changes, providing a seamless experience even when connectivity is inconsistent. Keep exploring Android’s evolving APIs to stay ahead in building robust and user-centric applications.

Further Reading and References

Happy coding! 🚀

Comments

Popular posts from this blog

How to Download Apk file from Url and Install Programmatically

In this post we learn about download apk file from server or website and then install it Programmatically in Phone. Sometimes we have to download external apk file from server and then install if downloading successfully finished.For this we use AsyncTask class  for background process. So here is Code Snippet for this task.Lets Start :- Before this we have to add these Permissions in Manifest.xml file : <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> DownloadNewVersion.java class DownloadNewVersion extends AsyncTask<String,Integer,Boolean> { @Override protected void onPreExecute() { super.onPreExecute(); bar = new ProgressDialog(getActivity()); bar.setCancelable(false); bar.setMessage("Downl...

Working with Android 11 (Changes and Security Features)

 Hello everyone , I am here with new article which is hot topic nowadays "Android 11" .The stable version of Android.  Android 11 is the eleventh major release and 18th version of Android, the mobile operating system developed by the Open Handset Alliance led by Google. It was released on September 8, 2020.It is comes with many security features and other features as well . And it is now compulsory in play store  to upload new apps with API lavel 30 which is compatible with Android 11 and from November onwards old apps also have to update with API 30 .Some other guidelines you can check out from here . Play Store Guidelines So its clear that we have to update our apps with API level 30 .But Android 11 comes with some changes as well which we have to do in our projects. For example from Android developer site "Android 11 (API level 30) further enhances the platform, giving better protection to app and user data on external storage. ". Scoped storage enforcement: Apps...

How to sort a list in ascending order and Add header by the first letter in the RecyclerView in Android

Hello Forks ! Hope you are doing well. In this tutorial we will understand how to sort a list of data, it could be any data like bank names , places names etc. in ascending order with a header which grouped the same type of data under it. For example we have a list of banks data and we want to group all banks starting with 'A' character in single unit and so on. So without taking too much time  let's move to the coding part and understand how we can achieve this. Step 1 : Create a Model class Named Bank.java public class Bank { private String name; public Bank (String name) { this .name = name; } public String getName () { return name; } } Step 2 : Sort the List of Banks with this Method import java.util.Collections; import java.util.Comparator; import java.util.List; public void sortBankList (List<Bank> bankList) { Collections.sort(bankList, new Comparator <Bank>() { @Override pub...