16

I'm new to Android development and working on an Android application that requires the phone to be connected to the internet, through either Wifi, EDGE or 3G.

This is the code that I'm using to check whether an internet connection is available

public static boolean isConnected()
{
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    return cm.getActiveNetworkInfo().isConnectedOrConnecting();
}

I've also set these permissions in the manifest file

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

This works fine in the emulator running version 1.5 of Android when 3G is enabled, but it crashes when I disable the 3G connection. My application throws a null pointer exception when I call isConnectedOrConnecting(). The same thing also happens on my HTC Desire running Android 2.1.

Hope that anyone knows the solution to this.

Thanks in advance!

Swati Garg
  • 995
  • 1
  • 10
  • 21
Charles
  • 2,615
  • 3
  • 29
  • 35

5 Answers5

24

If the crash is directly on your line:

return cm.getActiveNetworkInfo().isConnectedOrConnecting();

then that means getActiveNetworkInfo() returned null, because there is no active network -- in that case, your isConnected() method should return false.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Doh, this is something that I should've seen. Thanks. – Charles May 02 '10 at 13:28
  • 6
    Yeah, well, it'd help if the documentation for `getActiveNetworkInfo()` actually existed. :-) – CommonsWare May 02 '10 at 14:12
  • please help\n i have pasted the whole function but their is red line coming under getsystemservice... and eclipse is not providing any suggestions.. please suggest some solution on how to check internet connectivity – Sourabh Mar 27 '11 at 18:30
  • Use IsConnected() instead of isConnectedOrConnecting(). "isConnectedOrConnecting() Indicates whether network connectivity exists or is in the process of being established. This is good for applications that need to do anything related to the network other than read or write data. For the latter, call isConnected() instead, which guarantees that the network is fully usable." – Yousha Aleayoub Apr 13 '15 at 20:40
8

I wrote this method to handle this:

public boolean isOnline() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    if (ni!=null && ni.isAvailable() && ni.isConnected()) {
        return true;
    } else {
        return false; 
    }
}

One way to do it I guess...

mutable2112
  • 439
  • 4
  • 15
4

To check internet is there or not can be checked only on device......On emulator it may not work.... I have got the following code & its working 100% on android device..... :)

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    tv = (TextView)findViewById(R.id.txt);
    b = checkInternetConnection();


    if(b!=true)
    {
        tv.setText("net is not dr.......");
    }
    else
    {
        tv.setText("net is dr.......");
    }

}
//Check weather Internet connection is available or not
public boolean checkInternetConnection() {
           final ConnectivityManager conMgr = (ConnectivityManager) getSystemService (Context.CONNECTIVITY_SERVICE);
           if (conMgr.getActiveNetworkInfo() != null && conMgr.getActiveNetworkInfo().isAvailable() &&    conMgr.getActiveNetworkInfo().isConnected()) {
                 return true;
           } else {
                 System.out.println("Internet Connection Not Present");
               return false;
           }
        }

}

Dhrupal
  • 1,863
  • 1
  • 23
  • 38
1

You have used this snippet.

ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
      if (connectivity != null)
      {
          NetworkInfo[] info = connectivity.getAllNetworkInfo();
          if (info != null)
              for (int i = 0; i < info.length; i++)
                  if (info[i].getState() == NetworkInfo.State.CONNECTED)
                  {
                      return true;
                  }

      }
Harshid
  • 5,701
  • 4
  • 37
  • 50
-2

use this to detemine if connceted to wifi/3g:

is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).isConnected();
    isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
    network = is3g||isWifi;

and this to enable wifi yourself:

WifiManager wifiManager = (WifiManager) MainWindowYuval.this.getSystemService(Context.WIFI_SERVICE);
                    wifiManager.setWifiEnabled(true);      
yogi
  • 1,327
  • 2
  • 12
  • 33