How to get Android internet connection status?

How to get Android Internet connection status for HTC One app development?

I am new to android development and I am going to develop an application with internet. so i need to get interent status. Any help was appreciated.

+3
source share
5 answers

try this way and Read more

public static String getConnectivityStatusString(Context context) {
    int conn = NetworkUtil.getConnectivityStatus(context);
    String status = null;
    if (conn == NetworkUtil.TYPE_WIFI) {
        status = "Wifi enabled";
    } else if (conn == NetworkUtil.TYPE_MOBILE) {
        status = "Mobile data enabled";
    } else if (conn == NetworkUtil.TYPE_NOT_CONNECTED) {
        status = "Not connected to Internet";
    }
    return status;
}

or another easy way to complete a task

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

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
    if (ni.getTypeName().equalsIgnoreCase("WIFI"))
        if (ni.isConnected())
            haveConnectedWifi = true;
    if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
        if (ni.isConnected())
            haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;

}

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

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

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

in the Android manifest.

+3
source

You can use the following code to get the connection status.

public abstract class NetworkUtils 
{
    public static boolean isNetworkConnected(Context context) 
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        return networkInfo != null && networkInfo.isConnected();
    }
}

You need to define the following permissions in the AndroidManifest.xml file

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

Now you can call the method above using the following lines of code in any action you want,

if (!NetworkUtils.isNetworkConnected(getApplicationContext())) 
{
    // msg to display.
}
0

Use this code: This code will work in all versions of Android.

public static boolean isInternetOn(Context context) 
    {
         boolean haveConnectedWifi = false;
         boolean haveConnectedMobile = false;

         ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
         NetworkInfo[] netInfo = cm.getAllNetworkInfo();
         for (NetworkInfo ni : netInfo) 
         {
             if (ni.getTypeName().equalsIgnoreCase("WIFI"))
                 if (ni.isConnected())
                     haveConnectedWifi = true;
             if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
                 if (ni.isConnected())
                     haveConnectedMobile = true;
         }
         return haveConnectedWifi || haveConnectedMobile;
    }

It returns true if the Internet is turned on and false when the Internet is unavailable.

0
source
public static boolean isNetworkAvailable(Context context)
{
  return ((ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo() != null;
}

or

public static boolean isNetworkAvailable(Context context) {
  boolean outcome = false;

  if (context != null) {
    ConnectivityManager cm = (ConnectivityManager) context
      .getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

    for (NetworkInfo tempNetworkInfo : networkInfos) {


      /**
       * Can also check if the user is in roaming
       */
      if (tempNetworkInfo.isConnected()) {
        outcome = true;
        break;
      }
    }
  }

  return outcome;
}

Remember to add the following line to the manifest file:

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

Hi, make the ConnectionDetector class as follows

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class ConnectionDetector {

    private Context _context;

    public ConnectionDetector(Context context){
        this._context = context;
    }

    public boolean isConnectingToInternet(){
        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;
                      }

          }
          return false;
    }
}

you can use this class as follows

ConnectionDetector detector= new ConnectionDetector(getApplicationContext());

Boolean isInternetPresent = detector.isConnectingToInternet(); // true or false

with this boolean isInternetPresent you can continue further development.

if (isInternetPresent) {
                    // Internet Connection is Present


                } else {
                    // Internet connection is not present

                }

in support of all this, you need to add permission to your manifest.xml

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

All Articles