Kakey
Kakey

Reputation: 4024

Internet availability checking in android

In my application there are 20 activities and about 30 webservice calls.I need to check that internet availability before calling the webservice.I think calling Networking availability function on every time before calling each webservice is not an efficient way.whats the efficient way to check Internet?

Upvotes: 1

Views: 3910

Answers (3)

peceps
peceps

Reputation: 17557

You can check network availability periodically.

Also make sure that you catch the exceptions thrown when connecting when there is no network connection.

 /**
   * Checks if the phone has network connection.
   * 
   * @param context the context
   * @return <code>true</code> if the phone is connected
   */
  public boolean isConnected(Context context) {

     ConnectivityManager cm = (ConnectivityManager)
     context.getSystemService(Context.CONNECTIVITY_SERVICE);

     NetworkInfo wifiNetwork =
     cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
     if (wifiNetwork != null &&  wifiNetwork.isConnectedOrConnecting()) {
        return true;
     }

     NetworkInfo mobileNetwork =
     cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     if (mobileNetwork != null && mobileNetwork.isConnectedOrConnecting()) {
        return true;
     }

     NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
     if (activeNetwork != null && activeNetwork.isConnectedOrConnecting()) {
       return true;
     }

     return false;
  }

Upvotes: 1

iAndroid
iAndroid

Reputation: 951

private boolean haveNetworkConnection(Context context)
{
    boolean haveConnectedWifi = false;
    boolean haveConnectedMobile = false;

    ConnectivityManager cm = (ConnectivityManager) Your_Activity_Name.this.getSystemService(Context.CONNECTIVITY_SERVICE);
    // or if function is out side of your Activity then you need context of your Activity
    // and code will be as following
    // (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] netInfo = cm.getAllNetworkInfo();
    for (NetworkInfo ni : netInfo)
    {
        if (ni.getTypeName().equalsIgnoreCase("WIFI"))
        {
            if (ni.isConnected())
            {
                haveConnectedWifi = true;
                System.out.println("WIFI CONNECTION AVAILABLE");
            } else
            {
                System.out.println("WIFI CONNECTION NOT AVAILABLE");
            }
        }
        if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
        {
            if (ni.isConnected())
            {
                haveConnectedMobile = true;
                System.out.println("MOBILE INTERNET CONNECTION AVAILABLE");
            } else
            {
                System.out.println("MOBILE INTERNET CONNECTION NOT AVAILABLE");
            }
        }
    }
    return haveConnectedWifi || haveConnectedMobile;
}

Upvotes: 0

Jason Hanley
Jason Hanley

Reputation: 3225

You can register to listen for network connectivity with a broadcast receiver and store the result. See this Stack Overflow post for more information: How can I monitor the network connection status in Android?

Upvotes: 2

Related Questions