ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo != null && networkInfo.isConnected()) { // Device is connected to the internet } else { // Device is not connected to the internet }Description of Example: The above code example uses the getActiveNetworkInfo method to retrieve a NetworkInfo object that represents the currently active network connection. The isConnected method is then used to check whether the device is connected to the internet. If the device is connected, the code can then perform relevant network operations, such as downloading or uploading data. Package Library: The android.net.ConnectivityManager class is part of the android.net package library, which is included in the Android SDK.