1

Is there a way to check when a device, which has not been connected to the Internet , is just connected. If I connect the phone to the Internet inside my application, I should do some actions (e.g. Toast).

Note: It is not the same question with Detect if Android device has Internet connection and Android - checking if the device is connected to the internet.

Muhammadjon
  • 1,476
  • 2
  • 14
  • 35

1 Answers1

0

Try this:

private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager 
          = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}

You will also need:

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

in your android manifest.

Note that having an active network interface doesn't guarantee that a particular networked service is available.

Masoud Mokhtari
  • 2,390
  • 1
  • 17
  • 45