Java Code Examples for android.net.ConnectivityManager#getActiveNetworkInfo()

The following examples show how to use android.net.ConnectivityManager#getActiveNetworkInfo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Preferences.java    From fdroidclient with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Some users never use WiFi, this lets us check for that state on first run.
 */
public void setDefaultForDataOnlyConnection(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm == null) {
        return;
    }
    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
    if (activeNetwork == null || !activeNetwork.isConnectedOrConnecting()) {
        return;
    }
    if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
        NetworkInfo wifiNetwork = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (!wifiNetwork.isConnectedOrConnecting()) {
            preferences.edit().putInt(PREF_OVER_DATA, OVER_NETWORK_ALWAYS).apply();
        }
    }
}
 
Example 2
Source File: RealSystemFacade.java    From mobile-manager-tool with MIT License 6 votes vote down vote up
public Integer getActiveNetworkType() {
ConnectivityManager connectivity = (ConnectivityManager) mContext
	.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity == null) {
    Log.w(Constants.TAG, "couldn't get connectivity manager");
    return null;
}

NetworkInfo activeInfo = connectivity.getActiveNetworkInfo();
if (activeInfo == null) {
    if (Constants.LOGVV) {
	Log.v(Constants.TAG, "network is not available");
    }
    return null;
}
return activeInfo.getType();
   }
 
Example 3
Source File: VDMobileUtil.java    From NewsMe with Apache License 2.0 6 votes vote down vote up
public static int getMobileDetail(Context context) {
    ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (conn == null) {
        return NONE;
    }
    NetworkInfo networkInfo = conn.getActiveNetworkInfo();
    if (networkInfo == null) {
        // 网络不可用
        return NONE;
    }
    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return WIFI;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
        TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        return telephonyManager.getNetworkType();
    }
    return NONE;
}
 
Example 4
Source File: AsyncTaskManager.java    From sealtalk-android with MIT License 5 votes vote down vote up
/**
 * 判断网络是否可用
 * @param context
 * @param isCheckNetwork 是否检查网络,true表示检查,false表示不检查
 * @return
 */
public boolean isNetworkConnected(Context context, boolean isCheckNetwork) {
	if(isCheckNetwork && context != null){
		ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo ni = cm.getActiveNetworkInfo();
		return ni != null && ni.isConnectedOrConnecting();
	}else{
		return true;
	}
}
 
Example 5
Source File: AppUtils.java    From BmapLite with Apache License 2.0 5 votes vote down vote up
/**
 * 判断网络是否连接
 *
 * @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();
        }
    }
    return false;
}
 
Example 6
Source File: BuilderVPN.java    From InviZible with GNU General Public License v3.0 5 votes vote down vote up
BuilderVPN(ServiceVPN serviceVPN) {
    serviceVPN.super();
    ConnectivityManager cm = (ConnectivityManager) serviceVPN.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        networkInfo = cm.getActiveNetworkInfo();
    }
}
 
Example 7
Source File: NetworkUtil.java    From AndroidBasicProject with MIT License 5 votes vote down vote up
/**
 * 判断是否有网络连接
 */
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();
        }
    }
    return false;
}
 
Example 8
Source File: WifiConnectionManager.java    From medic-gateway with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean isWifiActive() {
	logEvent(ctx, "Checking if wifi is active...");
	ConnectivityManager cMan = (ConnectivityManager) ctx.getSystemService(CONNECTIVITY_SERVICE);
	NetworkInfo info = cMan.getActiveNetworkInfo();
	boolean wifiActive = info != null && info.getType() == TYPE_WIFI;

	logEvent(ctx, "Wifi active? %s [%s]", wifiActive, info);

	return wifiActive;
}
 
Example 9
Source File: MqttService.java    From Sparkplug with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * @return whether the android service can be regarded as online
 */
public boolean isOnline() {
	ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
	NetworkInfo networkInfo = cm.getActiveNetworkInfo();
     //noinspection RedundantIfStatement
     if (networkInfo != null
             && networkInfo.isAvailable()
             && networkInfo.isConnected()
             && backgroundDataEnabled) {
		return true;
	}

	return false;
}
 
Example 10
Source File: AppNetworkMgr.java    From AndroidWallet with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get network type name
 *
 * @param context context
 * @return NetworkTypeName
 */
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 11
Source File: Utils.java    From HHComicViewer with Apache License 2.0 5 votes vote down vote up
public static boolean isWifiConnected(Context context) {
    if (context.checkCallingOrSelfPermission(permission.ACCESS_WIFI_STATE)
            != PackageManager.PERMISSION_GRANTED) {
        return false;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager)
            context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = connectivityManager.getActiveNetworkInfo();
    if (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI) {
        return true;
    }
    return false;
}
 
Example 12
Source File: HueDeviceService.java    From DeviceConnect-Android with MIT License 5 votes vote down vote up
@Override
public void onReceive(final Context context, final Intent intent) {
    String action = intent.getAction();
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(action)) {
        ConnectivityManager conn = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = conn.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            reconnectAccessPoints();
        } else {
            disconnectAllBridges(true);
        }
    }
}
 
Example 13
Source File: SystemTool.java    From FriendBook with GNU General Public License v3.0 5 votes vote down vote up
/**
 * 判断网络是否连接
 */
public static boolean checkNet(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info != null;// 网络是否连接
}
 
Example 14
Source File: WaitForCloudConnectivityStep.java    From particle-android with Apache License 2.0 5 votes vote down vote up
private boolean checkIsApiHostAvailable() {
    ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(
            Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = null;
    if (cm != null) {
        activeNetworkInfo = cm.getActiveNetworkInfo();
    }
    if (activeNetworkInfo == null || !activeNetworkInfo.isConnected()) {
        return false;
    }

    return true;
}
 
Example 15
Source File: EasyNetworkMod.java    From easydeviceinfo with Apache License 2.0 5 votes vote down vote up
/**
 * Gets BSSID of Connected WiFi
 *
 * You need to declare the below permission in the manifest file to use this properly
 *
 * <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 * <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
 *
 * @return Return the basic service set identifier (BSSID) of the current access point.
 */
@RequiresPermission(allOf = {
    Manifest.permission.ACCESS_WIFI_STATE, Manifest.permission.ACCESS_NETWORK_STATE
})
public final String getWifiBSSID() {
  String result = null;
  if (PermissionUtil.hasPermission(context, Manifest.permission.ACCESS_WIFI_STATE)) {
    ConnectivityManager cm =
        (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
      NetworkInfo networkInfo = cm.getActiveNetworkInfo();
      if (networkInfo == null) {
        result = null;
      }

      if (networkInfo != null && networkInfo.isConnected()) {
        final WifiManager wifiManager =
            (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        if (wifiManager != null) {
          final WifiInfo connectionInfo = wifiManager.getConnectionInfo();
          if (connectionInfo != null && !TextUtils.isEmpty(connectionInfo.getSSID())) {
            result = connectionInfo.getBSSID();
          }
        }
      }
    }
  }
  return CheckValidityUtil.checkValidData(result);
}
 
Example 16
Source File: NetworkUtils.java    From DroidDLNA with GNU General Public License v3.0 4 votes vote down vote up
static public NetworkInfo getConnectedNetworkInfo(Context context) {

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

        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) {
            return networkInfo;
        }

        networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;

        networkInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
        if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;

        networkInfo = connectivityManager.getNetworkInfo(CONNECTIVITY_TYPE_WIMAX);
        if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;

        networkInfo = connectivityManager.getNetworkInfo(CONNECTIVITY_TYPE_ETHERNET);
        if (networkInfo != null && networkInfo.isAvailable() && networkInfo.isConnected()) return networkInfo;

        log.info("Could not find any connected network...");

        return null;
    }
 
Example 17
Source File: Utils.java    From aware with MIT License 4 votes vote down vote up
public static boolean isNetworkAvailable(Activity a) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
 
Example 18
Source File: NetworkReachabilityManager.java    From couchbase-lite-android with Apache License 2.0 4 votes vote down vote up
private boolean isOnline(Context ctx) {
    final ConnectivityManager service = ((ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE));
    if (service == null) { return false; }
    final NetworkInfo networkInfo = service.getActiveNetworkInfo();
    return (networkInfo != null) && networkInfo.isConnected();
}
 
Example 19
Source File: Util.java    From tracker-control-android with GNU General Public License v3.0 4 votes vote down vote up
public static String getNetworkGeneration(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    return (ni != null && ni.getType() == ConnectivityManager.TYPE_MOBILE ? getNetworkGeneration(ni.getSubtype()) : null);
}
 
Example 20
Source File: CacheInterceptor.java    From EmoticonGIFKeyboard with Apache License 2.0 2 votes vote down vote up
/**
 * Check if the internet is available?
 *
 * @param context instance.
 * @return True if the internet is available else false.
 */
private boolean isNetworkAvailable(@NonNull final Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    return networkInfo != null && networkInfo.isConnected();
}