25

How would i know whether my device is connected the web or not? How can i detect connectivity? Any sample code?

Muhammad Maqsoodur Rehman
  • 33,681
  • 34
  • 84
  • 124
  • Possible duplicate of [Android check internet connection](http://stackoverflow.com/questions/9570237/android-check-internet-connection) – Sobhan Moradi Feb 09 '17 at 12:10

4 Answers4

54

First, you need permission to know whether the device is connected to the web or not. This needs to be in your manifest, in the <manifest> element:

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

Next, you need to get a reference to the ConnectivityManager:

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

From there you need to obtain a NetworkInfo object. For most, this will mean using ConnectivityManager. getActiveNetworkInfo():

NetworkInfo ni = cm.getActiveNetworkInfo();
if (ni == null) {
    // There are no active networks.
    return false;
}

From there, you just need to use one of NetworkInfo's methods to determine if the device is connected to the internet:

boolean isConnected = ni.isConnected();
wojciii
  • 4,253
  • 1
  • 30
  • 39
Dan Lew
  • 85,990
  • 32
  • 182
  • 176
6

First, you need permission to know whether the device is connected to the web or not. This needs to be in your manifest, in the element:

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

then

ConnectivityManager connec = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);

if (connec != null && (
    (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.CONNECTED) || 
    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.CONNECTED))) { 

        //You are connected, do something online.

} else if (connec != null && (
    (connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState() == NetworkInfo.State.DISCONNECTED) ||
    (connec.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState() == NetworkInfo.State.DISCONNECTED ))) {            

        //Not connected.    
        Toast.makeText(getApplicationContext(), "You must be connected to the internet", Toast.LENGTH_LONG).show();

} 
zasadnyy
  • 2,107
  • 2
  • 19
  • 26
Tim
  • 261
  • 1
  • 5
  • 10
4

Add this permission in your AppManifest.xml file:

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

The method to check if network is available or not:

boolean isNetworkAvailable() {
  ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

  NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
  boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();
  return isConnected;
}

Source

Himanshu Aggarwal
  • 1,803
  • 2
  • 24
  • 36
0

http://developer.android.com/reference/android/net/ConnectivityManager.html - CONNECTIVITY_ACTION

Alex Volovoy
  • 67,778
  • 13
  • 73
  • 54