Java Code Examples for android.net.NetworkInfo.isConnected()

The following are Jave code examples for showing how to use isConnected() of the android.net.NetworkInfo class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: GitHub   File: Kits.java   View Source Code Vote up 8 votes
public static String getNetworkTypeName(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo;
    String type = NETWORK_TYPE_DISCONNECT;
    if (manager == null || (networkInfo = manager.getActiveNetworkInfo()) == null) {
        return type;
    }

    if (networkInfo.isConnected()) {
        String typeName = networkInfo.getTypeName();
        if ("WIFI".equalsIgnoreCase(typeName)) {
            type = NETWORK_TYPE_WIFI;
        } else if ("MOBILE".equalsIgnoreCase(typeName)) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            type = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G : NETWORK_TYPE_2G)
                    : NETWORK_TYPE_WAP;
        } else {
            type = NETWORK_TYPE_UNKNOWN;
        }
    }
    return type;
}
 
Example 2
Project: richeditor   File: Utils.java   View Source Code Vote up 7 votes
public static boolean isNetworkAvailable() {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) AppManager.app().getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}
 
Example 3
Project: MvpPlus   File: LoaderConfig.java   View Source Code Vote up 7 votes
/**
 * get the network type (wifi,wap,2g,3g)
 *
 * @param context
 * @return
 */
public static int getNetWorkType(Context context) {

    int mNetWorkType = NETWORKTYPE_INVALID;
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = manager.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected()) {
        String type = networkInfo.getTypeName();
        if (type.equalsIgnoreCase("WIFI")) {
            mNetWorkType = NETWORKTYPE_WIFI;
        } else if (type.equalsIgnoreCase("MOBILE")) {
            String proxyHost = android.net.Proxy.getDefaultHost();
            mNetWorkType = TextUtils.isEmpty(proxyHost)
                    ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G)
                    : NETWORKTYPE_WAP;
        }
    } else {
        mNetWorkType = NETWORKTYPE_INVALID;
    }
    return mNetWorkType;
}
 
Example 4
Project: FindX   File: ConnectionDetector.java   View Source Code Vote up 7 votes
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;
                  }

          NetworkInfo mWifi = connectivity.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

          if (mWifi.isConnected()) {

              return true;
          }
 
      }
      return false;
}
 
Example 5
Project: GitHub   File: CheckNetwork.java   View Source Code Vote up 7 votes
/**
 * 判断网络是否连通
 */
public static boolean isNetworkConnected(Context context) {
    try {
        if(context!=null){
            @SuppressWarnings("static-access")
            ConnectivityManager cm = (ConnectivityManager) context
                    .getSystemService(context.CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();
            return info != null && info.isConnected();
        }else{
            /**如果context为空,就返回false,表示网络未连接*/
            return false;
        }
    }catch (Exception e){
        e.printStackTrace();
        return false;
    }


}
 
Example 6
Project: MVPArms_Fragment-fragment   File: DeviceUtils.java   View Source Code Vote up 6 votes
public static boolean netIsConnected(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    //手机网络连接状态
    NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    //WIFI连接状态
    NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    if (!mobNetInfo.isConnected() && !wifiNetInfo.isConnected()) {
        //当前无可用的网络
        return false;
    }
    return true;
}
 
Example 7
Project: IelloAndroidApp   File: HelperRete.java   View Source Code Vote up 6 votes
/**
 * Determina se internet è attivo
 */
public static boolean isNetworkAvailable(Context c) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
 
Example 8
Project: AndroidKillerService   File: OtherOperatorService.java   View Source Code Vote up 6 votes
public static boolean check3Gwifi(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo infoM = manager.getNetworkInfo(0);
    if (manager.getNetworkInfo(1).isConnected() || infoM.isConnected()) {
        return true;
    }
    return false;
}
 
Example 9
Project: localcloud_fe   File: NetworkManager.java   View Source Code Vote up 6 votes
/**
 * Get the latest network connection information
 *
 * @param info the current active network info
 * @return a JSONObject that represents the network info
 */
private JSONObject getConnectionInfo(NetworkInfo info) {
    String type = TYPE_NONE;
    String extraInfo = "";
    if (info != null) {
        // If we are not connected to any network set type to none
        if (!info.isConnected()) {
            type = TYPE_NONE;
        }
        else {
            type = getType(info);
        }
        extraInfo = info.getExtraInfo();
    }

    LOG.d(LOG_TAG, "Connection Type: " + type);
    LOG.d(LOG_TAG, "Connection Extra Info: " + extraInfo);

    JSONObject connectionInfo = new JSONObject();

    try {
        connectionInfo.put("type", type);
        connectionInfo.put("extraInfo", extraInfo);
    } catch (JSONException e) {
        LOG.d(LOG_TAG, e.getLocalizedMessage());
    }

    return connectionInfo;
}
 
Example 10
Project: Crimson   File: CheckupReminders.java   View Source Code Vote up 6 votes
/**
 * Checks whether the device currently has a network connection.
 *
 * @return true if the device has a network connection, false otherwise.
 */
private boolean isDeviceOnline() {
    ConnectivityManager connMgr =
            (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    return (networkInfo != null && networkInfo.isConnected());
}
 
Example 11
Project: urdu-font-comparator-app   File: Utils.java   View Source Code Vote up 6 votes
/**
 * @return true if the internet connection currently in use is functional,
 * otherwise false
 */
public static boolean isOnline(final Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    return netInfo != null && netInfo.isConnected();
}
 
Example 12
Project: GitHub   File: CheckNetwork.java   View Source Code Vote up 6 votes
public boolean isConnected(Context context){
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Activity.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected())
        return true;
    else
        return false;
}
 
Example 13
Project: GCSApp   File: EaseCommonUtils.java   View Source Code Vote up 6 votes
/**
 * check if network avalable
 * 
 * @param context
 * @return
 */
public static boolean isNetWorkConnected(Context context) {
	if (context != null) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
		if (mNetworkInfo != null) {
			return mNetworkInfo.isAvailable() && mNetworkInfo.isConnected();
		}
	}

	return false;
}
 
Example 14
Project: IM_Here   File: Utility.java   View Source Code Vote up 6 votes
/**
 * Get the state of networking connection of the current mobile
 * @return the network is connected in mobile or not
 */
public static boolean networkIsConnected(Context context){
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo =
            connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}
 
Example 15
Project: ease-volley-wrapper   File: EaseRequest.java   View Source Code Vote up 5 votes
/**
 * Check internet connectivity.
 *
 * @return true if device is connect to <b>any</b> network, false otherwise.
 */
private boolean isDeviceOnline(Context ctx) {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info != null && info.isConnected();
}
 
Example 16
Project: emptyview   File: Utils.java   View Source Code Vote up 5 votes
public static boolean isConnected(Context context) {
    NetworkInfo info = getNetworkInfo(context);
    return info != null && info.isConnected();
}
 
Example 17
Project: SpaceRace   File: NetworkingUtility.java   View Source Code Vote up 5 votes
private static boolean connectivityCheck() {
    ConnectivityManager connectivityManager = (ConnectivityManager)
            SpaceRace.getAppContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}
 
Example 18
Project: ALC-Project   File: CheckNetworkConn.java   View Source Code Vote up 4 votes
public static boolean isConnected(Context context){

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

        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        return (info != null && info.isConnected());
    }
 
Example 19
Project: AndroidGeneralUtils   File: NetworkUtil.java   View Source Code Vote up 3 votes
/**
 * Check if there is any connectivity to a Wifi network
 *
 * @param context The context
 * @return true if connected to wifi, false otherwise
 */
public static boolean isConnectedWifi(Context context) {
    NetworkInfo info = getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
 
Example 20
Project: webtrekk-android-sdk   File: HelperFunctions.java   View Source Code Vote up 3 votes
/**
 * Checks if there is wifi or mobile connection available
 *
 * @param context The application context
 * @return true if there is network connection available
 */
public static boolean isNetworkConnection(Context context) {
    NetworkInfo networkInfo = getNetworkInfo(context);
    return networkInfo != null && networkInfo.isConnected();
}