3

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.

James MV
  • 8,569
  • 17
  • 65
  • 96

4 Answers4

9

You can subclass Activity with something like InternetActivity, and in the onResume of that you can check for the connection. Then, all activities that require internet in your app should be subclasses of InternetActivity, and will automatically do the check by either calling super.onResume() or not overriding onResume at all.

Tushar
  • 8,019
  • 31
  • 38
4
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.

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

There is another interesting solution you can think of, By using a BroadcastReceiver. So right now you are planning to check so many times. Instead of doing that you will check only when Network connection changes, and notify you network module accordingly.

IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);        
registerReceiver(YOUR_RECEIVER, filter);
minhaz
  • 4,233
  • 3
  • 33
  • 49
-1

As Tushar answered above, inheriting from something like InternetActivity is fine, but you should not check it in onResume unless you are using some service which constantly requires internet connection. Instead, check it when an event like a button click occurs to go to next activity; so without starting another activity, you can display a toast to check internet connection. You should do this for any event that requires internet because whole activity rarely requires continuous internet connection.

You can add this method in that super class:

private boolean isInternetConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo() != null
            && cm.getActiveNetworkInfo().isConnectedOrConnecting();

}

Then:

if(isInternetConnected())
     //to the internet thing
else
     //display toast
Community
  • 1
  • 1
Kartik
  • 7,677
  • 4
  • 28
  • 50