0

I have made an android studio project, but I need to check for internet connectivity during each user interaction. I know the usage of the connectivity manager, and several other functions, but this is needed to be implemented in each activity. Can anyone provide any better solution or any better idea. So that the application shows a dialog box whenever the internet goes off.

1 Answers1

0

You may use this method

 boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getActiveNetworkInfo().getState() == NetworkInfo.State.CONNECTED){
        //we are connected to a network
        connected = true;
    }
    else
        connected = false;

Warning: If you are connected to a WiFi network that doesn't include internet access or requires browser-based authentication, connected will still be true.

You will need this permission in your manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Void
  • 898
  • 1
  • 11
  • 16