user16779236
user16779236

Reputation:

Checking for Internet Connection in Android/Java Code

I have been using the following code (acquired from the web) to check for Internet connection in my Android (web) app.

import android.content.Context;
import android.net.ConnectivityManager;

public class DetectConnection {
    public static boolean checkInternetConnection(Context context) {

        ConnectivityManager con_manager = (ConnectivityManager)
                context.getSystemService(Context.CONNECTIVITY_SERVICE);

        return (con_manager.getActiveNetworkInfo() != null
                && con_manager.getActiveNetworkInfo().isAvailable()
                && con_manager.getActiveNetworkInfo().isConnected());
    }
}

Android studio is now giving me deprecation warnings concerning the use of getActiveNetworkInfo(). A simple search in the Internet shows that I should be using the API in [1]. I however could not figure out how to do it. Any ideas?

[1] https://developer.android.com/reference/android/net/ConnectivityManager.NetworkCallback

Upvotes: 0

Views: 1023

Answers (3)

San Nasser
San Nasser

Reputation: 33

The code I used does not give results for connecting to the Internet, but being connected to a network will return true even if the network does not provide an Internet connection.

public class InternetCkecker 
          {
               public boolean isConnected() {
                   try {
                            Process process = Runtime.getRuntime().exec("ping 
 -c 1 google.com");
                           int returnVal = process.waitFor();
                           return (returnVal == 0);
                       } catch (Exception e) {
                              return false;
                      }
              }
         }

This function will return true only if you are actually connected to the Internet

Upvotes: 0

Harshil
Harshil

Reputation: 202

Try This!

boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
            connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) {
        
        connected = true;
    }
    else
        connected = false;

You will need this permission in your manifest:

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

Upvotes: 0

Mateusz Nejman
Mateusz Nejman

Reputation: 36

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.Network;

public class NetworkCheck {
    private boolean connected = false;

    public NetworkCheck(Context context) {
        try {
            ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

            connectivityManager.registerDefaultNetworkCallback(
                new ConnectivityManager.NetworkCallback() {
                    @Override
                    public void onAvailable(Network network) {
                        connected = true;
                    }

                    @Override
                    public void onLost(Network network) {
                        connected = false;
                    }
                }

            );
        } catch (Exception e) {
            connected = false;
        }
    }

    public boolean isConnected() {
        return connected;
    }
}

You can check something like this

Upvotes: 0

Related Questions