android.net.ConnectivityManager#getActiveNetworkInfo ( )源码实例Demo

下面列出了android.net.ConnectivityManager#getActiveNetworkInfo ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: NewsMe   文件: VDMobileUtil.java
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;
}
 
源代码2 项目: mobile-manager-tool   文件: RealSystemFacade.java
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();
   }
 
源代码3 项目: fdroidclient   文件: Preferences.java
/**
 * 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();
        }
    }
}
 
源代码4 项目: easydeviceinfo   文件: EasyNetworkMod.java
/**
 * 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);
}
 
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;
}
 
源代码6 项目: FriendBook   文件: SystemTool.java
/**
 * 判断网络是否连接
 */
public static boolean checkNet(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo info = cm.getActiveNetworkInfo();
    return info != null;// 网络是否连接
}
 
源代码7 项目: DeviceConnect-Android   文件: HueDeviceService.java
@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);
        }
    }
}
 
源代码8 项目: HHComicViewer   文件: Utils.java
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;
}
 
源代码9 项目: sealtalk-android   文件: AsyncTaskManager.java
/**
 * 判断网络是否可用
 * @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;
	}
}
 
源代码10 项目: AndroidWallet   文件: AppNetworkMgr.java
/**
 * 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;
}
 
源代码11 项目: Sparkplug   文件: MqttService.java
/**
 * @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;
}
 
源代码12 项目: medic-gateway   文件: WifiConnectionManager.java
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;
}
 
源代码13 项目: AndroidBasicProject   文件: NetworkUtil.java
/**
 * 判断是否有网络连接
 */
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;
}
 
源代码14 项目: InviZible   文件: BuilderVPN.java
BuilderVPN(ServiceVPN serviceVPN) {
    serviceVPN.super();
    ConnectivityManager cm = (ConnectivityManager) serviceVPN.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        networkInfo = cm.getActiveNetworkInfo();
    }
}
 
源代码15 项目: BmapLite   文件: AppUtils.java
/**
 * 判断网络是否连接
 *
 * @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;
}
 
源代码16 项目: tracker-control-android   文件: Util.java
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);
}
 
源代码17 项目: aware   文件: Utils.java
public static boolean isNetworkAvailable(Activity a) {
    ConnectivityManager connectivityManager
            = (ConnectivityManager) a.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null && activeNetworkInfo.isConnected();
}
 
源代码18 项目: DroidDLNA   文件: NetworkUtils.java
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;
    }
 
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();
}
 
源代码20 项目: EmoticonGIFKeyboard   文件: CacheInterceptor.java
/**
 * 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();
}