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: 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 #2
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 #3
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 #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: 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 #6
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 #7
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 #8
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 #9
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 #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: 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;
}
 
Example #12
Source File: SystemUtil.java    From Android with MIT License 5 votes vote down vote up
/**
 * Determine whether to open the Wifi
 * @return
 */
public static boolean isOpenWifi(){
    Context context = BaseApplication.getInstance().getBaseContext();
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
    return networkInfo.isConnected();
}
 
Example #13
Source File: LocationApi.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
private boolean d()
{
    ConnectivityManager connectivitymanager = (ConnectivityManager)mContext.getSystemService("connectivity");
    if (connectivitymanager != null)
    {
        NetworkInfo networkinfo = connectivitymanager.getActiveNetworkInfo();
        return networkinfo != null && networkinfo.isAvailable();
    } else
    {
        return false;
    }
}
 
Example #14
Source File: XLUtil.java    From TVRemoteIME with GNU General Public License v2.0 5 votes vote down vote up
public static String getAPNName(Context context) {
    if (context == null) {
        return null;
    }
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService("connectivity");
    if (connectivityManager == null) {
        return null;
    }
    NetworkInfo networkInfo = connectivityManager.getNetworkInfo(0);
    if (networkInfo == null) {
        return null;
    }
    return networkInfo.getExtraInfo();
}
 
Example #15
Source File: SysUtils.java    From java-n-IDE-for-Android with Apache License 2.0 5 votes vote down vote up
/**
 * require: <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
 * @param context
 * @return
 */
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivityManager
                = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnected();
    } catch (Exception e) {
        return false;
    }
}
 
Example #16
Source File: Util.java    From Orin 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 #17
Source File: OCVideo.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 #18
Source File: Util.java    From MediaSDK with Apache License 2.0 5 votes vote down vote up
private static @C.NetworkType int getMobileNetworkType(NetworkInfo networkInfo) {
  switch (networkInfo.getSubtype()) {
    case TelephonyManager.NETWORK_TYPE_EDGE:
    case TelephonyManager.NETWORK_TYPE_GPRS:
      return C.NETWORK_TYPE_2G;
    case TelephonyManager.NETWORK_TYPE_1xRTT:
    case TelephonyManager.NETWORK_TYPE_CDMA:
    case TelephonyManager.NETWORK_TYPE_EVDO_0:
    case TelephonyManager.NETWORK_TYPE_EVDO_A:
    case TelephonyManager.NETWORK_TYPE_EVDO_B:
    case TelephonyManager.NETWORK_TYPE_HSDPA:
    case TelephonyManager.NETWORK_TYPE_HSPA:
    case TelephonyManager.NETWORK_TYPE_HSUPA:
    case TelephonyManager.NETWORK_TYPE_IDEN:
    case TelephonyManager.NETWORK_TYPE_UMTS:
    case TelephonyManager.NETWORK_TYPE_EHRPD:
    case TelephonyManager.NETWORK_TYPE_HSPAP:
    case TelephonyManager.NETWORK_TYPE_TD_SCDMA:
      return C.NETWORK_TYPE_3G;
    case TelephonyManager.NETWORK_TYPE_LTE:
      return C.NETWORK_TYPE_4G;
    case TelephonyManager.NETWORK_TYPE_IWLAN:
      return C.NETWORK_TYPE_WIFI;
    case TelephonyManager.NETWORK_TYPE_GSM:
    case TelephonyManager.NETWORK_TYPE_UNKNOWN:
    default: // Future mobile network types.
      return C.NETWORK_TYPE_CELLULAR_UNKNOWN;
  }
}
 
Example #19
Source File: MainApp.java    From browser with GNU General Public License v2.0 5 votes vote down vote up
public  boolean hasNetwork() {
    if(context==null){
        return  false;
    }
    android.net.ConnectivityManager cManager = (android.net.ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    android.net.NetworkInfo info = cManager.getActiveNetworkInfo();
    if (info != null && info.isAvailable()) {
        return true;
    } else {
        return false;
    }
}
 
Example #20
Source File: Preferences.java    From candybar-library with Apache License 2.0 5 votes vote down vote up
public boolean isConnectedAsPreferred() {
    try {
        if (isWifiOnly()) {
            ConnectivityManager connectivityManager = (ConnectivityManager)
                    mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
            return activeNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI &&
                    activeNetworkInfo.isConnected();
        }
        return true;
    } catch (Exception e) {
        return false;
    }
}
 
Example #21
Source File: SearchContactsActivity.java    From o2oa with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    if (intent != null && intent.getAction().equals("android.net.conn.CONNECTIVITY_CHANGE")) {
        ConnectivityManager manager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeInfo = manager.getActiveNetworkInfo();
        if (null == activeInfo) {
            mNoConnect.setVisibility(View.VISIBLE);
            mSearchView.setVisibility(View.GONE);
        } else {
            mNoConnect.setVisibility(View.GONE);
            mSearchView.setVisibility(View.VISIBLE);
        }
    }
}
 
Example #22
Source File: NetworkHandler.java    From PowerSwitch_Android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * checks if Ethernet is connected
 *
 * @return false if Ethernet is not connected
 */
public static boolean isEthernetConnected() {
    ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    boolean isWificonnected = (networkInfo != null &&
            ConnectivityManager.TYPE_ETHERNET == networkInfo.getType() && networkInfo.isConnectedOrConnecting());
    Log.d("isEthernetConnected: " + isWificonnected);
    return isWificonnected;
}
 
Example #23
Source File: Requirements.java    From TelePlus-Android with GNU General Public License v2.0 5 votes vote down vote up
private static boolean isActiveNetworkMetered(
    ConnectivityManager connectivityManager, NetworkInfo networkInfo) {
  if (Util.SDK_INT >= 16) {
    return connectivityManager.isActiveNetworkMetered();
  }
  int type = networkInfo.getType();
  return type != ConnectivityManager.TYPE_WIFI
      && type != ConnectivityManager.TYPE_BLUETOOTH
      && type != ConnectivityManager.TYPE_ETHERNET;
}
 
Example #24
Source File: NetInfoModule.java    From GSYVideoPlayer with Apache License 2.0 5 votes vote down vote up
public String getCurrentConnectionType() {
    try {
        NetworkInfo networkInfo = mConnectivityManager.getActiveNetworkInfo();
        if (networkInfo == null || !networkInfo.isConnected()) {
            return CONNECTION_TYPE_NONE;
        } else if (ConnectivityManager.isNetworkTypeValid(networkInfo.getType())) {
            return networkInfo.getTypeName().toUpperCase();
        } else {
            return CONNECTION_TYPE_UNKNOWN;
        }
    } catch (SecurityException e) {
        mNoNetworkPermission = true;
        return CONNECTION_TYPE_UNKNOWN;
    }
}
 
Example #25
Source File: Utils.java    From discreet-app-rate with Apache License 2.0 5 votes vote down vote up
public static boolean isOnline(Context context) {
    int res = context.checkCallingOrSelfPermission(Manifest.permission.ACCESS_NETWORK_STATE);
    if (res == PackageManager.PERMISSION_GRANTED) {
        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnectedOrConnecting()) {
            return true;
        }
        return false;
    }

    return true;
}
 
Example #26
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 #27
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 #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: 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 #30
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;
}