Checking internet connection in every activity?

android

Solution

public class CheckNetwork {


private static final String TAG = CheckNetwork.class.getSimpleName();



public static boolean isInternetAvailable(Context context)
{
    NetworkInfo info = (NetworkInfo) ((ConnectivityManager)
    context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();

    if (info == null)
    {
         Log.d(TAG,"no internet connection");
         return false;
    }
    else
    {
        if(info.isConnected())
        {
            Log.d(TAG," internet connection available...");
            return true;
        }
        else
        {
            Log.d(TAG," internet connection");
            return true;
        }

    }
}
 }

To check Netowrk

In your activity

  if(CheckNetwork.isInternetAvailable(MainActivtiy.this))  //if connection available
  {

  }

Everytime you want to check internet pass the activity context returns true if available else false.

Detect whether there is an Internet connection available on Android.

How to check internet access on Android? InetAddress never times out.

Problem

I'm doing this a little late in the process. I have a quite a few screens/activites which all require a connection to the internet, doesn't matter if its wifi or network to work. I can detect the connection fine but do I need to perform this check on every single activity or is there a global way to do this for my application? Just thought I'd ask before adding a lot of code.

Original source

Related problems