Skip to content Skip to sidebar Skip to footer

How To Find Network Enable Status In Android?

Is there possible to find internet enable using intent in broadcast receiver in android pls help me? I want exactly background process when app is not open i want to detect network

Solution 1:

try this below code,

public class ConnectionAvailable extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(context.CONNECTIVITY_SERVICE);
    boolean is3g = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .isConnectedOrConnecting();
    boolean isWifi = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .isConnectedOrConnecting();

    if (!is3g && !isWifi) {

        Toast.makeText(context, "Internet Connection Lost",
                Toast.LENGTH_LONG).show();

    } else {
        if ((intent.getAction() != null)
                && (intent.getAction()
                        .equals("android.intent.action.AIRPLANE_MODE"))) {
            Toast.makeText(context, "Internet Connection Lost",
                    Toast.LENGTH_LONG).show();               
        }
    }
}
}

This might help you out..


Solution 2:

try the following code:

private void setMobileDataEnabled(Context context, boolean enabled) {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setMobileDataEnabledMethod = iConnectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
setMobileDataEnabledMethod.setAccessible(true);

setMobileDataEnabledMethod.invoke(iConnectivityManager, enabled);
 }

Solution 3:

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;

Solution 4:

  public static boolean isNetworkAvailable(Context context) {
             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;
          }

Solution 5:

  public boolean isOnline() {
    ConnectivityManager cm =
        (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;}
   and add this permission     

call this method and add this permission in your manifest file <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />


Post a Comment for "How To Find Network Enable Status In Android?"