android.net.NetworkInfo Java Examples

The following examples show how to use android.net.NetworkInfo. 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: NetWorkTool.java    From FoodOrdering with Apache License 2.0 6 votes vote down vote up
public static boolean isNetwork(Context context) {
    boolean isnetwork = false;
    ConnectivityManager manager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkinfo = manager.getActiveNetworkInfo();
    if (networkinfo == null || !networkinfo.isAvailable()) {
        isnetwork = false;
    } else {
        int type = networkinfo.getType();
        if (type == ConnectivityManager.TYPE_MOBILE
                || type == ConnectivityManager.TYPE_WIFI) {
            isnetwork = true;

        }
    }
    return isnetwork;
}
 
Example #2
Source File: NetworkConnectivityListener.java    From Anecdote with Apache License 2.0 6 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    String action = intent.getAction();

    if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION) || mListener == null) {
        return;
    }

    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    boolean networkAvailable = networkInfo != null && networkInfo.isConnected();

    if (networkAvailable) {
        mState = State.CONNECTED;
    } else {
        mState = State.NOT_CONNECTED;
    }

    mMainHandler.post(mConnectivityRunnable);
}
 
Example #3
Source File: NetUtils.java    From Gizwits-SmartBuld_Android with MIT License 6 votes vote down vote up
/**
 * 判断当前手机是否连上Wifi.
 *
 * @param context
 *            上下文
 * @return boolean 是否连上网络
 * 
 *         *
 */
static public boolean isWifiConnected(Context context) {
	if (context != null) {
		ConnectivityManager mConnectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
		if (mWiFiNetworkInfo != null) {
			if (mWiFiNetworkInfo.isAvailable()) {
				return mWiFiNetworkInfo.isConnected();
			} else {
				return false;
			}
		}
	}
	return false;
}
 
Example #4
Source File: Util.java    From droidddle with Apache License 2.0 6 votes vote down vote up
public static boolean isConnected(Context context) {

        if (context == null) return false;

        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivityManager != null) {
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();

            if (activeNetworkInfo == null)
                return false;

            return ((activeNetworkInfo != null) && (activeNetworkInfo
                    .isConnectedOrConnecting()));
        }

        return false;
    }
 
Example #5
Source File: HttpUtils.java    From letv with Apache License 2.0 6 votes vote down vote up
public static NetworkProxy getProxy(Context context) {
    if (context == null) {
        return null;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
    if (connectivityManager == null) {
        return null;
    }
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetworkInfo == null) {
        return null;
    }
    if (activeNetworkInfo.getType() == 0) {
        Object b = b(context);
        int a = a(context);
        if (!TextUtils.isEmpty(b) && a >= 0) {
            return new NetworkProxy(b, a);
        }
    }
    return null;
}
 
Example #6
Source File: ConnectivityMonitor.java    From unity-ads-android with Apache License 2.0 6 votes vote down vote up
private static void initConnectionStatus() {
	ConnectivityManager cm = (ConnectivityManager)ClientProperties.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);

	if(cm == null) {
		return;
	}

	NetworkInfo ni  = cm.getActiveNetworkInfo();

	if(ni != null && ni.isConnected()) {
		_connected = 1;
		_wifi = ni.getType() == ConnectivityManager.TYPE_WIFI;

		if(!_wifi) {
			TelephonyManager tm = (TelephonyManager)ClientProperties.getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
			_networkType = tm.getNetworkType();
		}
	} else {
		_connected = 0;
	}
}
 
Example #7
Source File: ConnectivityTracker.java    From Easer with GNU General Public License v3.0 6 votes vote down vote up
private int convertType(NetworkInfo activeNetworkInfo) {
    if (activeNetworkInfo == null) {
        return TYPE_NOT_CONNECTED;
    }
    switch (activeNetworkInfo.getType()) {
        case ConnectivityManager.TYPE_WIFI:
            return TYPE_WIFI;
        case ConnectivityManager.TYPE_MOBILE:
            return TYPE_MOBILE;
        case ConnectivityManager.TYPE_ETHERNET:
            return TYPE_ETHERNET;
        case ConnectivityManager.TYPE_BLUETOOTH:
            return TYPE_BLUETOOTH;
        case ConnectivityManager.TYPE_VPN:
            return TYPE_VPN;
    }
    return -1;
}
 
Example #8
Source File: AppContext.java    From Cotable with Apache License 2.0 6 votes vote down vote up
/**
 * Get the current active network status.
 *
 * @return 0: no network
 * 1: WIFI
 * 2: WAP
 * 3: CMNET
 */
public int getNetWorkType() {
    int netType = 0;
    ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService
            (CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo == null) return netType;

    int nType = networkInfo.getType();
    if (nType == ConnectivityManager.TYPE_MOBILE) {
        String extraInfo = networkInfo.getExtraInfo();
        if (!StringUtils.isEmpty(extraInfo)) {
            if (extraInfo.toLowerCase().equals("cmnet"))
                netType = NETTYPE_CMNET;
            else
                netType = NETTYPE_CMWAP;
        }
    } else if (nType == ConnectivityManager.TYPE_WIFI) {
        netType = NETTYPE_WIFI;
    }

    return netType;

}
 
Example #9
Source File: NetUtils.java    From Aria with Apache License 2.0 6 votes vote down vote up
/**
 * 获取网络状态,wifi,wap,2g,3g.
 *
 * @param context 上下文
 * @return int 网络状态 {@link #NETWORK_TYPE_2G},{@link #NETWORK_TYPE_3G},
 * {@link #NETWORK_TYPE_INVALID},{@link #NETWORK_TYPE_WAP},{@link #NETWORK_TYPE_WIFI}
 */
public static int getNetWorkType(Context context) {
  int netWorkType = -1;
  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")) {
      netWorkType = NETWORK_TYPE_WIFI;
    } else if (type.equalsIgnoreCase("MOBILE")) {
      String proxyHost = android.net.Proxy.getDefaultHost();
      netWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORK_TYPE_3G
          : NETWORK_TYPE_2G) : NETWORK_TYPE_WAP;
    }
  } else {
    netWorkType = NETWORK_TYPE_INVALID;
  }
  return netWorkType;
}
 
Example #10
Source File: WifiBase.java    From android-wifi-activity with Creative Commons Zero v1.0 Universal 6 votes vote down vote up
/**
 * Start to connect to a specific wifi network
 */
private void connectToSpecificNetwork() {
    final WifiManager wifi = (WifiManager) getContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    ConnectivityManager cm = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    WifiInfo wifiInfo = wifi.getConnectionInfo();
    if (networkInfo.isConnected() && wifiInfo.getSSID().replace("\"", "").equals(getWifiSSID())) {
        return;
    }
    else {
        wifi.disconnect();
    }
    progressDialog = ProgressDialog.show(getContext(), getContext().getString(R.string.connecting), String.format(getContext().getString(R.string.connecting_to_wifi), getWifiSSID()));
    taskHandler = worker.schedule(new TimeoutTask(), getSecondsTimeout(), TimeUnit.SECONDS);
    scanReceiver = new ScanReceiver();
    getContext().registerReceiver(scanReceiver
            , new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION));
    wifi.startScan();
}
 
Example #11
Source File: NetworkUtils.java    From MVVM with MIT License 5 votes vote down vote up
public static boolean isConnected(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 #12
Source File: NetworkConnectionChangeReceiver.java    From NetworkEvents with Apache License 2.0 5 votes vote down vote up
private ConnectivityStatus getConnectivityStatus(Context context) {
  ConnectivityManager connectivityManager =
      (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

  if (networkInfo != null) {
    if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
      return ConnectivityStatus.WIFI_CONNECTED;
    } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
      return ConnectivityStatus.MOBILE_CONNECTED;
    }
  }

  return ConnectivityStatus.OFFLINE;
}
 
Example #13
Source File: Util.java    From scanvine-android with MIT License 5 votes vote down vote up
public static boolean AreWeOnline(Context context) {
	ConnectivityManager cm =
	        (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
	 
	NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
	return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
}
 
Example #14
Source File: Util.java    From RetroMusicPlayer with GNU General Public License v3.0 5 votes vote down vote up
public static boolean isAllowedToDownloadMetadata(final Context context) {
    switch (PreferenceUtil.getInstance(context).autoDownloadImagesPolicy()) {
        case "always":
            return true;
        case "only_wifi":
            final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
            return netInfo != null && netInfo.getType() == ConnectivityManager.TYPE_WIFI && netInfo.isConnectedOrConnecting();
        case "never":
        default:
            return false;
    }
}
 
Example #15
Source File: WifiapBroadcast.java    From WifiChat with GNU General Public License v2.0 5 votes vote down vote up
public void onReceive(Context paramContext, Intent paramIntent) {

        // wifi开关
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(paramIntent.getAction())) {
            int wifiState = paramIntent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            switch (wifiState) {
                case WifiManager.WIFI_STATE_DISABLED:
                case WifiManager.WIFI_STATE_ENABLED:
                    mListener.wifiStatusChange();
                    break;
            }
        }

        // 连接wifi
        else if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(paramIntent.getAction())) {
            mNetworkInfo = paramIntent.getParcelableExtra("networkInfo");

            /**
             * 当 DetailedState 变化为 CONNECTED 时,说明已连接成功,则通知Handler更新
             * 可避免WifiapActivity里出现重复获取IP的问题
             */
            if (mNetworkInfo.getDetailedState() == NetworkInfo.DetailedState.CONNECTED) {
                mListener.WifiConnected();
            }
        }

    }
 
Example #16
Source File: NetworkUtils.java    From LiuAGeAndroid with MIT License 5 votes vote down vote up
/**
 * 判断WiFi是否可用
 *
 * @param context 上下文
 * @return true可用
 */
public boolean isWifiConnected(Context context) {
    if (context != null) {
        ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo mWiFiNetworkInfo = mConnectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (mWiFiNetworkInfo != null) {
            return mWiFiNetworkInfo.isAvailable();
        }
    }
    return false;
}
 
Example #17
Source File: DeviceUtils.java    From MVPArms with Apache License 2.0 5 votes vote down vote up
public static boolean netIsConnected(Context context) {
    ConnectivityManager connectMgr = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    //手机网络连接状态
    NetworkInfo mobNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
    //WIFI连接状态
    NetworkInfo wifiNetInfo = connectMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    //当前无可用的网络
    return mobNetInfo.isConnected() || wifiNetInfo.isConnected();
}
 
Example #18
Source File: NETUtils.java    From GTTools with MIT License 5 votes vote down vote up
/**
 * 获取网络连接类型
 * 
 * @return -1表示没有网络
 */
public static final int getNetWorkType(Context c) {
	ConnectivityManager conn = (ConnectivityManager) c
			.getSystemService(Context.CONNECTIVITY_SERVICE);
	if (conn == null) {
		return -1;
	}
	NetworkInfo info = conn.getActiveNetworkInfo();
	if (info == null || !info.isAvailable()) {
		return -1;
	}
	int type = info.getType();
	if (type == ConnectivityManager.TYPE_WIFI) {
		return TYPE_WIFI;
	} else {
		TelephonyManager tm = (TelephonyManager) c
				.getSystemService(Context.TELEPHONY_SERVICE);
		switch (tm.getNetworkType()) {
		case TelephonyManager.NETWORK_TYPE_CDMA:
			return TYPE_GPRS;
		case TelephonyManager.NETWORK_TYPE_EDGE:
			return TYPE_GPRS;
		case TelephonyManager.NETWORK_TYPE_GPRS:
			return TYPE_GPRS;
		default:
			return TYPE_FAST;
		}
	}
}
 
Example #19
Source File: NetUtils.java    From NetStateBar with Apache License 2.0 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 #20
Source File: CommonUtil.java    From HeroVideo-master with Apache License 2.0 5 votes vote down vote up
/**
 * 检查是否是移动网络
 */
public static boolean isMobile(Context context)
{

    NetworkInfo info = getNetworkInfo(context);
    if (info != null)
    {
        if (info.getType() == ConnectivityManager.TYPE_MOBILE)
            return true;
    }
    return false;
}
 
Example #21
Source File: X5WebUtils.java    From YCWebView with Apache License 2.0 5 votes vote down vote up
/**
 * 判断网络是否连接
 * <p>需添加权限
 * {@code <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />}</p>
 *
 * @return {@code true}: 是<br>{@code false}: 否
 */
public static boolean isConnected(Context context) {
    if (context==null){
        return false;
    }
    NetworkInfo info = getActiveNetworkInfo(context);
    return info != null && info.isConnected();
}
 
Example #22
Source File: NetworkUtil.java    From Retrofit2RxjavaDemo with Apache License 2.0 5 votes vote down vote up
/**
 * isWifi
 * @param context
 * @return boolean
 */
public static boolean isWifi(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null
            && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return true;
    }
    return false;
}
 
Example #23
Source File: CacheInterceptor.java    From AndroidBase with Apache License 2.0 5 votes vote down vote up
private  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 #24
Source File: NetWorkUtils.java    From GankGirl with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * 获取当前网络的具体类型
 *
 * @param context 上下文
 * @return 当前网络的具体类型。具体类型可参照TelephonyManager中的NETWORK_TYPE_1xRTT、NETWORK_TYPE_CDMA等字段。当前没有网络连接时返回NetworkUtils.NETWORK_TYPE_NO_CONNECTION
 */
public static int getCurrentNetworkSubtype(Context context) {
    NetworkInfo networkInfo
            = ((ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null
            ? networkInfo.getSubtype()
            : NETWORK_TYPE_NO_CONNECTION;
}
 
Example #25
Source File: AutoLoadImageView.java    From Rocko-Android-Demos with Apache License 2.0 5 votes vote down vote up
/**
 * Checks if the device has any active internet connection.
 *
 * @return true device with internet connection, otherwise false.
 */
private boolean isThereInternetConnection() {
  boolean isConnected;

  ConnectivityManager connectivityManager =
      (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
  NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  isConnected = (networkInfo != null && networkInfo.isConnectedOrConnecting());

  return isConnected;
}
 
Example #26
Source File: Signal.java    From react-native-audio-streaming with MIT License 5 votes vote down vote up
public boolean isConnected() {
    ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting()) {
        return true;
    }
    return false;
}
 
Example #27
Source File: OldCamActivity.java    From goprohero with MIT License 5 votes vote down vote up
public boolean isConnected(){
    ConnectivityManager connMgr = (ConnectivityManager) getSystemService(this.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
    if (networkInfo != null && networkInfo.isConnected())
        return true;
    else
        return false;
}
 
Example #28
Source File: ImageFetcher.java    From vocefiscal-android with Apache License 2.0 5 votes vote down vote up
/**
* Simple network connection check.
*
* @param context
*/
private void checkConnection(Context context) 
{
    final ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    final NetworkInfo networkInfo = cm.getActiveNetworkInfo();
    if (networkInfo == null || !networkInfo.isConnectedOrConnecting()) 
    {
        //Toast.makeText(context, R.string.no_network_connection_toast, Toast.LENGTH_LONG).show();
        //Log.e(TAG, "checkConnection - no connection found");
    }
}
 
Example #29
Source File: HttpUtil.java    From Focus with GNU General Public License v3.0 5 votes vote down vote up
/**
 * make true current connect service is wifi
 */
public static boolean isWifi() {
    ConnectivityManager connectivityManager = (ConnectivityManager) UIUtil.getContext()
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    if (activeNetInfo != null
            && activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {
        return true;
    }
    return false;
}
 
Example #30
Source File: NetworkManager.java    From cordova-android-chromeview with Apache License 2.0 5 votes vote down vote up
/**
 * Determine the type of connection
 *
 * @param info the network info so we can determine connection type.
 * @return the type of mobile network we are on
 */
private String getType(NetworkInfo info) {
    if (info != null) {
        String type = info.getTypeName();

        if (type.toLowerCase().equals(WIFI)) {
            return TYPE_WIFI;
        }
        else if (type.toLowerCase().equals(MOBILE)) {
            type = info.getSubtypeName();
            if (type.toLowerCase().equals(GSM) ||
                    type.toLowerCase().equals(GPRS) ||
                    type.toLowerCase().equals(EDGE)) {
                return TYPE_2G;
            }
            else if (type.toLowerCase().startsWith(CDMA) ||
                    type.toLowerCase().equals(UMTS) ||
                    type.toLowerCase().equals(ONEXRTT) ||
                    type.toLowerCase().equals(EHRPD) ||
                    type.toLowerCase().equals(HSUPA) ||
                    type.toLowerCase().equals(HSDPA) ||
                    type.toLowerCase().equals(HSPA)) {
                return TYPE_3G;
            }
            else if (type.toLowerCase().equals(LTE) ||
                    type.toLowerCase().equals(UMB) ||
                    type.toLowerCase().equals(HSPA_PLUS)) {
                return TYPE_4G;
            }
        }
    }
    else {
        return TYPE_NONE;
    }
    return TYPE_UNKNOWN;
}