-5

I am currently developing an application with Android studio that needs an Internet connection to work. I would like to check the internet access at the start of the application by pinging my website. I already tried the method ConnectivityManager but it just checks if the user is connected to the wifi or mobile data. How could I ping any website to check if the phone has internet access?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Delphin
  • 33
  • 5
  • Potential problem with pinging your own personal website - what if your site goes down? The phone can still access the internet so this would be a false negative. – Michael Dodd May 31 '19 at 10:50
  • @MichaelDodd it would not be a problem if I ping Google for example – Delphin May 31 '19 at 10:56
  • @delphinisoardi As unlikely as it may be, it's still not guaranteed, [as happened in March 2019](https://www.androidcentral.com/its-not-just-you-gmail-youtube-and-other-google-services-are-down-right-now) – Michael Dodd May 31 '19 at 10:58
  • @MichaelDodd This is not a problem since if google services are down the application will not work in any case since it uses Firebase – Delphin May 31 '19 at 11:11
  • @MichaelDodd Do you have any idea how I can do that ? – Delphin May 31 '19 at 11:12

2 Answers2

2
/**
 * Verifies if the device is connected to the internet.
 * @return
 */
public static boolean itsOnline(Context context) {
    try {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);

        int timeoutMs = 2000;
        Socket sock = new Socket();
        SocketAddress sockaddr = new InetSocketAddress("8.8.8.8", 53);

        sock.connect(sockaddr, timeoutMs);
        sock.close();
        Log.i("CONNECTION STATUS:", "connected");

        return true;
    } catch (IOException ioException) {
        Log.i("CONNECTION STATUS:", "disconnected");
        return false;
    }
    return false;
}
Ricardo A.
  • 685
  • 2
  • 8
  • 35
0

You can check different connection-types by using the TelephonyManager like so:

public boolean hasNetworkAccess() {
        ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
        NetworkInfo info = cm.getNetworkInfo(cm.getActiveNetwork());

        return info.isConnected();
    }

    public String connectionStrength() {
        if (!hasNetworkAccess()) {
            return "NOT_CONNECTED";
        }

        TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);

        // switch for different network types (e.g. 4G, EDGE, ...)
        switch (tm.getNetworkType()) {
            case TelephonyManager.NETWORK_TYPE_EDGE:
                // return a value that you want to identify the strength with
                return "EDGE";
            // ...
            default:
                return null;
        }
    }

Dont forget to add the <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> in the manifest above the application tag

Max pm
  • 73
  • 8