Example usage for android.net ConnectivityManager getNetworkInfo

List of usage examples for android.net ConnectivityManager getNetworkInfo

Introduction

In this page you can find the example usage for android.net ConnectivityManager getNetworkInfo.

Prototype

@Deprecated
@RequiresPermission(android.Manifest.permission.ACCESS_NETWORK_STATE)
@Nullable
public NetworkInfo getNetworkInfo(@Nullable Network network) 

Source Link

Document

Returns connection status information about a particular Network.

Usage

From source file:Main.java

public static boolean checkWifiInternetConn() {
    //Create object for ConnectivityManager class which returns network related info
    ConnectivityManager connectivity = (ConnectivityManager) m_TempContext
            .getSystemService(m_TempContext.CONNECTIVITY_SERVICE);
    //If connectivity object is not null
    if (connectivity != null) {
        //Get network info - WIFI internet access
        NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (info != null) {
            //Look for whether device is currently connected to WIFI network
            if (info.isConnected()) {
                return true;
            }/*from   w  ww  . j a v  a2  s .  c o m*/
        }
    }
    return false;
}

From source file:Main.java

public static boolean checkMobileInternetConn() {
    //Create object for ConnectivityManager class which returns network related info
    ConnectivityManager connectivity = (ConnectivityManager) m_TempContext
            .getSystemService(m_TempContext.CONNECTIVITY_SERVICE);
    //If connectivity object is not null
    if (connectivity != null) {
        //Get network info - Mobile internet access
        NetworkInfo info = connectivity.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (info != null) {
            //Look for whether device is currently connected to Mobile internet
            if (info.isConnected()) {
                return true;
            }//  ww  w . j a v a2s  .c  o  m
        }
    }
    return false;
}

From source file:Main.java

public static boolean isNetworkConnected(Context context) {
    if (context == null) {
        return false;
    }//from  ww  w  .  j  a  v  a  2  s . c om

    boolean isOK = false;

    try {
        boolean isWifiOK;
        boolean isMobileOK;

        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connManager == null) {
            return false;
        }

        NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        if (state == null) {
            return false;
        }
        if (NetworkInfo.State.CONNECTED == state) {
            isWifiOK = true;
        } else {
            isWifiOK = false;
        }

        state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
        if (state == null) {
            return false;
        }
        if (NetworkInfo.State.CONNECTED == state) {
            isMobileOK = true;
        } else {
            isMobileOK = false;
        }

        if (isMobileOK || isWifiOK) {
            isOK = true;
        }
    } catch (Exception e) {
        isOK = false;
    }

    return isOK;
}

From source file:Main.java

public static String[] getNetType(Context paramContext) {
    String[] arrayOfString = { "Unknown", "Unknown" };
    PackageManager localPackageManager = paramContext.getPackageManager();
    if (localPackageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE",
            paramContext.getPackageName()) != 0) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }/*from   w ww. jav a 2s.  c  om*/
    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext
            .getSystemService("connectivity");
    if (localConnectivityManager == null) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }
    NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
    if (localNetworkInfo1 != null && localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "2";
        return arrayOfString;
    }
    NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
    if (localNetworkInfo2 != null && localNetworkInfo2.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "1";
        arrayOfString[1] = localNetworkInfo2.getSubtypeName();
        return arrayOfString;
    }
    return arrayOfString;
}

From source file:edu.mecc.race2ged.helpers.Utils.java

/**
 * Are we connected to WiFi, Mobile Data, or nothing?
 * @return The devices current network state.
 *///from   w  w  w  .  j a  va2s .c o  m
public static int getNetworkStatus(Context context) {
    final ConnectivityManager connMgr = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final android.net.NetworkInfo wifi = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    final android.net.NetworkInfo mobile = connMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    if (wifi != null && wifi.isAvailable() && wifi.isConnected())
        return WIFI;
    else if (mobile != null && mobile.isAvailable() && mobile.isConnected())
        return MOBILE_DATA;
    return NO_CONNECTION;
}

From source file:de.eidottermihi.rpicheck.widget.OverclockingWidget.java

private static boolean isWiFiAvailable(Context context) {
    final ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo wifiInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return wifiInfo != null && wifiInfo.isConnected();
}

From source file:it.evilsocket.dsploit.net.Network.java

public static boolean isWifiConnected(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = manager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    return info != null && info.isConnected() && info.isAvailable();
}

From source file:com.harshad.linconnectclient.NotificationUtilities.java

public static boolean sendData(Context c, Notification n, String packageName) {
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);

    ConnectivityManager connManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

    // Check Wifi state, whether notifications are enabled globally, and
    // whether notifications are enabled for specific application
    if (prefs.getBoolean("pref_toggle", true) && prefs.getBoolean(packageName, true) && mWifi.isConnected()) {
        String ip = prefs.getString("pref_ip", "0.0.0.0:9090");

        // Magically extract text from notification
        ArrayList<String> notificationData = NotificationUtilities.getNotificationText(n);

        // Use PackageManager to get application name and icon
        final PackageManager pm = c.getPackageManager();
        ApplicationInfo ai;/* www.j a v  a2 s  .c  o m*/
        try {
            ai = pm.getApplicationInfo(packageName, 0);
        } catch (final NameNotFoundException e) {
            ai = null;
        }

        String notificationBody = "";
        String notificationHeader = "";
        // Create header and body of notification
        if (notificationData.size() > 0) {
            notificationHeader = notificationData.get(0);
            if (notificationData.size() > 1) {
                notificationBody = notificationData.get(1);
            }
        } else {
            return false;
        }

        for (int i = 2; i < notificationData.size(); i++) {
            notificationBody += "\n" + notificationData.get(i);
        }

        // Append application name to body
        if (pm.getApplicationLabel(ai) != null) {
            if (notificationBody.isEmpty()) {
                notificationBody = "via " + pm.getApplicationLabel(ai);
            } else {
                notificationBody += " (via " + pm.getApplicationLabel(ai) + ")";
            }
        }

        // Setup HTTP request
        MultipartEntity entity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

        // If the notification contains an icon, use it
        if (n.largeIcon != null) {
            entity.addPart("notificon",
                    new InputStreamBody(ImageUtilities.bitmapToInputStream(n.largeIcon), "drawable.png"));
        }
        // Otherwise, use the application's icon
        else {
            entity.addPart("notificon",
                    new InputStreamBody(
                            ImageUtilities.bitmapToInputStream(
                                    ImageUtilities.drawableToBitmap(pm.getApplicationIcon(ai))),
                            "drawable.png"));
        }

        HttpPost post = new HttpPost("http://" + ip + "/notif");
        post.setEntity(entity);

        try {
            post.addHeader("notifheader", Base64.encodeToString(notificationHeader.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription", Base64.encodeToString(notificationBody.getBytes("UTF-8"),
                    Base64.URL_SAFE | Base64.NO_WRAP));
        } catch (UnsupportedEncodingException e) {
            post.addHeader("notifheader",
                    Base64.encodeToString(notificationHeader.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
            post.addHeader("notifdescription",
                    Base64.encodeToString(notificationBody.getBytes(), Base64.URL_SAFE | Base64.NO_WRAP));
        }

        // Send HTTP request
        HttpClient client = new DefaultHttpClient();
        HttpResponse response;
        try {
            response = client.execute(post);
            String html = EntityUtils.toString(response.getEntity());
            if (html.contains("true")) {
                return true;
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    return false;
}

From source file:Main.java

public static int getNetWorkType(Context context) {
    // showLog("getNetWorkType");
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (null == networkInfo || !networkInfo.isAvailable()) {
        return 1;
    }//from   w ww.j a  va2  s .  co  m
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState()) {
        return 2;
    }
    if (State.CONNECTED == connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState()) {
        return 3;
    }
    return 4;
}

From source file:io.lqd.sdk.model.LQDevice.java

private static String getInternetConnectivity(Context context) {
    if (LiquidTools.checkForPermission(permission.ACCESS_NETWORK_STATE, context)) {
        ConnectivityManager connManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo mNetwork = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (mWifi != null && mWifi.isConnected()) {
            return "WiFi";
        } else if (mNetwork != null && mNetwork.isConnected()) {
            return "Cellular";
        } else {/*from   w  w  w.ja  v a2 s  . c  o m*/
            return "No Connectivity";
        }
    } else {
        return "No ACCESS_NETWORK_STATE permission";
    }
}