Java Code Examples for android.net.NetworkInfo

The following examples show how to use android.net.NetworkInfo. These examples are extracted from open source projects. 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 Project: Aria   Author: AriaLyy   File: NetUtils.java    License: 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 Project: Cotable   Author: wlemuel   File: AppContext.java    License: 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 Project: Easer   Author: renyuneyun   File: ConnectivityTracker.java    License: 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 Project: droidddle   Author: goodev   File: Util.java    License: 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 Project: Gizwits-SmartBuld_Android   Author: gizwits   File: NetUtils.java    License: 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 Project: Anecdote   Author: HugoGresse   File: NetworkConnectivityListener.java    License: 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 Project: letv   Author: JackChan1999   File: HttpUtils.java    License: 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 Project: FoodOrdering   Author: yangxch   File: NetWorkTool.java    License: 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 Project: unity-ads-android   Author: Unity-Technologies   File: ConnectivityMonitor.java    License: 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 Project: android-wifi-activity   Author: zoltanersek   File: WifiBase.java    License: 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 Project: cordova-android-chromeview   Author: thedracle   File: NetworkManager.java    License: 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 Project: Android   Author: connectim   File: SystemUtil.java    License: 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 Project: MiBandDecompiled   Author: vishnudevk   File: LocationApi.java    License: 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 Project: TVRemoteIME   Author: kingthy   File: XLUtil.java    License: 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 Project: java-n-IDE-for-Android   Author: shenghuntianlang   File: SysUtils.java    License: 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 Project: Orin   Author: aliumujib   File: Util.java    License: 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 Project: goprohero   Author: KonradIT   File: OCVideo.java    License: 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 Project: MediaSDK   Author: JeffMony   File: Util.java    License: 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 Project: browser   Author: NewCasino   File: MainApp.java    License: 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 Project: candybar-library   Author: danimahardhika   File: Preferences.java    License: 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 Project: o2oa   Author: o2oa   File: SearchContactsActivity.java    License: 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 Project: PowerSwitch_Android   Author: Power-Switch   File: NetworkHandler.java    License: 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 Project: TelePlus-Android   Author: TelePlusDev   File: Requirements.java    License: 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 Project: GSYVideoPlayer   Author: CarGuo   File: NetInfoModule.java    License: 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 Project: discreet-app-rate   Author: PomepuyN   File: Utils.java    License: 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 Project: NetStateBar   Author: finddreams   File: NetUtils.java    License: 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 Project: Focus   Author: ihewro   File: HttpUtil.java    License: 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 Project: vocefiscal-android   Author: vocefiscal   File: ImageFetcher.java    License: 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 Project: goprohero   Author: KonradIT   File: OldCamActivity.java    License: 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 Project: react-native-audio-streaming   Author: tlenclos   File: Signal.java    License: 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;
}