1

Actually i had used the following code to check the wifi is active or not (its working fine). I just want to confirm that can i use the same code to check the 3G active state:

 public boolean checkwifi()
    {
        ConnectivityManager connec = (ConnectivityManager) this.getSystemService(this.CONNECTIVITY_SERVICE);
        android.net.NetworkInfo wifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        android.net.NetworkInfo mobile = connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

        // Here if condition check for wifi and mobile network is available or not.
        // If anyone of them is available or connected then it will return true, otherwise false;

        if (wifi.isConnected()) {
            return true;
        } 
        else if (mobile.isConnected()) {
            return true;
        }
        return false;
    }
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
Vaibhav
  • 123
  • 1
  • 6
  • What happens when you run that code on your phone? If it gives the correct result then you can use it. –  Sep 22 '11 at 08:30
  • Hope this helps http://stackoverflow.com/questions/3262781/how-to-check-wifi-or-3g-network-is-available-on-android-device and this http://stackoverflow.com/questions/2919414/get-network-type – kazinix Sep 22 '11 at 08:32

2 Answers2

0

Yes - this code should work. I've got an extra bit of functionality you may want to implement:

public static boolean isOnline(Context context) {
    //ConnectivityManager is used to check available network(s)
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo() != null;
}

This does the same as your code - it will tell you if you are online via any method (3G or WiFi), but it's a bit more concise.

** EDIT **

Here's the source for that method - it will return null if no active network connection or will return a NetworkInfo object for a current connection:

/**
 * Return NetworkInfo for the active (i.e., connected) network interface.
 * It is assumed that at most one network is active at a time. If more
 * than one is active, it is indeterminate which will be returned.
 * @return the info for the active network, or {@code null} if none is active
 */
public NetworkInfo getActiveNetworkInfo() {
    enforceAccessPermission();
    for (NetworkStateTracker t : mNetTrackers) {
        NetworkInfo info = t.getNetworkInfo();
        if (info.isConnected()) {
            return info;
        }
    }
    return null;
}
Martyn
  • 16,432
  • 24
  • 71
  • 104
0

to check if your connection(WIFI or Mobile ) is On or Off , try this :

boolean connected = false;
ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED || 
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTING ||
           connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED ||
connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTING) {
        //we are connected to a network

        connected = true;
    }
    else
        connected = false;
Houcine
  • 24,001
  • 13
  • 56
  • 83