Java Code Examples for android.net.ConnectivityManager#TYPE_WIFI

The following examples show how to use android.net.ConnectivityManager#TYPE_WIFI . 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: AboutFragment.java    From iBeebo with GNU General Public License v3.0 6 votes vote down vote up
private String buildContent() {

        String network = "";

        ConnectivityManager cm = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isConnected()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                network = "Wifi";
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

                int subType = networkInfo.getSubtype();

                if (subType == TelephonyManager.NETWORK_TYPE_GPRS) {
                    network = "GPRS";
                }
            }
        }

        return "@andforce #iBeebo#反馈 " + android.os.Build.MANUFACTURER + " " + android.os.Build.MODEL + ",Android "
                + android.os.Build.VERSION.RELEASE + ","
                + network + " version:" + buildVersionInfo();
    }
 
Example 2
Source File: TDevice.java    From Cotable with Apache License 2.0 6 votes vote down vote up
public static boolean isWifiOpen() {
    boolean isWifiConnect = false;
    ConnectivityManager cm = (ConnectivityManager) BaseApplication
            .context().getSystemService(Context.CONNECTIVITY_SERVICE);
    // check the networkInfos numbers
    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (int i = 0; i < networkInfos.length; i++) {
        if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
                isWifiConnect = false;
            }
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
                isWifiConnect = true;
            }
        }
    }
    return isWifiConnect;
}
 
Example 3
Source File: NetworkChangeReceiver.java    From netWatch with Apache License 2.0 6 votes vote down vote up
/**
 * getConnectionType()  Detects the connection type if one existed
 * @param context;
 * @return Type of Connection returned by this method can be: TYPE_WIFI=1 or :TYPE_MOBILE=2
 */
private static int getConnectionType(Context context) {

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

    NetworkInfo activeNetwork = cm.getActiveNetworkInfo();

    if (null != activeNetwork) {

        int CONNECTED_WIFI = 1;
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI)
            return CONNECTED_WIFI;

        int CONNECTED_MOBILE = 2;
        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE)
            return CONNECTED_MOBILE;
    }
    return DISCONNECTED;
}
 
Example 4
Source File: TDevice.java    From AndroidReview with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isWifiOpen() {
    boolean isWifiConnect = false;
    ConnectivityManager cm = (ConnectivityManager) BaseApplication
            .context().getSystemService(Context.CONNECTIVITY_SERVICE);
    // check the networkInfos numbers
    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (int i = 0; i < networkInfos.length; i++) {
        if (networkInfos[i].getState() == NetworkInfo.State.CONNECTED) {
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_MOBILE) {
                isWifiConnect = false;
            }
            if (networkInfos[i].getType() == ConnectivityManager.TYPE_WIFI) {
                isWifiConnect = true;
            }
        }
    }
    return isWifiConnect;
}
 
Example 5
Source File: FTPServerFragment.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
@Override
    public void onReceive(Context context, Intent intent) {
        ConnectivityManager conMan = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = conMan.getActiveNetworkInfo();
        if ((netInfo != null && (netInfo.getType() == ConnectivityManager.TYPE_WIFI || netInfo.getType() == ConnectivityManager.TYPE_ETHERNET))
|| FTPService.isEnabledWifiHotspot(getContext())) {
            // connected to wifi or eth
            ftpBtn.setEnabled(true);
        } else {
            // wifi or eth connection lost
            stopServer();
            statusText.setText(spannedStatusNoConnection);
            ftpBtn.setEnabled(true);
            ftpBtn.setEnabled(false);
            ftpBtn.setText(getResources().getString(R.string.start_ftp).toUpperCase());
        }
    }
 
Example 6
Source File: FTPService.java    From PowerFileExplorer with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isConnectedToLocalNetwork(Context context) {
    boolean connected = false;
    ConnectivityManager cm = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo ni = cm.getActiveNetworkInfo();
    connected = ni != null
            && ni.isConnected()
            && (ni.getType() & (ConnectivityManager.TYPE_WIFI | ConnectivityManager.TYPE_ETHERNET)) != 0;
    if (!connected) {
        Log.d(TAG, "isConnectedToLocalNetwork: see if it is an USB AP");
        try {
            for (NetworkInterface netInterface : Collections.list(NetworkInterface
                    .getNetworkInterfaces())) {
                if (netInterface.getDisplayName().startsWith("rndis")) {
                    connected = true;
                }
            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
    }
    return connected;
}
 
Example 7
Source File: HostMonitor.java    From android-host-monitor with Apache License 2.0 6 votes vote down vote up
static ConnectionType getCurrentConnectionType(Context context) {
    ConnectivityManager connectivityManager =
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();

    if (networkInfo == null || !networkInfo.isConnected()) {
        return ConnectionType.NONE;
    }

    int type = networkInfo.getType();

    if (type == ConnectivityManager.TYPE_MOBILE) return ConnectionType.MOBILE;
    if (type == ConnectivityManager.TYPE_WIFI) return ConnectionType.WIFI;

    Logger.error(LOG_TAG, "Unsupported connection type: " + type + ". Returning NONE");
    return ConnectionType.NONE;
}
 
Example 8
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 9
Source File: Helper.java    From iZhihu with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 判断 WIFI 是否已经打开
 *
 * @param context
 * @return
 */
public static boolean isWifiConnected(Context context) {
    if (context == null) {
        return false;
    }
    ConnectivityManager connectivityManager
            = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();
    return (activeNetInfo != null) && (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI);
}
 
Example 10
Source File: ConnectionUtils.java    From FireFiles with Apache License 2.0 5 votes vote down vote up
public static boolean isConnectedToWifi(Context context) {

        ConnectivityManager cm = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo ni = cm.getActiveNetworkInfo();
        return ni != null && ni.isConnected()
                && ni.getType() == ConnectivityManager.TYPE_WIFI;
    }
 
Example 11
Source File: NetworkUtil.java    From bither-android with Apache License 2.0 5 votes vote down vote up
private static NetworkType getNetworkType(NetworkInfo info) {
    if (info.getType() == ConnectivityManager.TYPE_WIFI || info.getType() ==
            ConnectivityManager.TYPE_ETHERNET) {
        return NetworkType.Wifi;
    } else {
        return NetworkType.Mobile;
    }
}
 
Example 12
Source File: NetWorkUtils.java    From youqu_master with Apache License 2.0 5 votes vote down vote up
/**
 * 检测wifi是否连接
 */
public static boolean isWifiConnected(Context context) {
    ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (cm != null) {
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
            return true;
        }
    }
    return false;
}
 
Example 13
Source File: Util.java    From okdownload with Apache License 2.0 5 votes vote down vote up
public static boolean isNetworkNotOnWifiType(ConnectivityManager manager) {
    if (manager == null) {
        Util.w("Util", "failed to get connectivity manager!");
        return true;
    }

    //noinspection MissingPermission, because we check permission accessable when invoked
    @SuppressLint("MissingPermission") final NetworkInfo info = manager.getActiveNetworkInfo();

    return info == null || info.getType() != ConnectivityManager.TYPE_WIFI;
}
 
Example 14
Source File: NetworkUtil.java    From ESeal with Apache License 2.0 5 votes vote down vote up
/**
 * Determine whether to use wifi
 */
public static boolean isConnectedByWifi() {
    NetworkInfo info = getNetworkInfos();
    return info != null
            && info.isConnected()
            && info.getType() == ConnectivityManager.TYPE_WIFI;
}
 
Example 15
Source File: PicassoExecutorService.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
void adjustThreadCount(NetworkInfo info) {
  if (info == null || !info.isConnectedOrConnecting()) {
    setThreadCount(DEFAULT_THREAD_COUNT);
    return;
  }
  switch (info.getType()) {
    case ConnectivityManager.TYPE_WIFI:
    case ConnectivityManager.TYPE_WIMAX:
    case ConnectivityManager.TYPE_ETHERNET:
      setThreadCount(4);
      break;
    case ConnectivityManager.TYPE_MOBILE:
      switch (info.getSubtype()) {
        case TelephonyManager.NETWORK_TYPE_LTE:  // 4G
        case TelephonyManager.NETWORK_TYPE_HSPAP:
        case TelephonyManager.NETWORK_TYPE_EHRPD:
          setThreadCount(3);
          break;
        case TelephonyManager.NETWORK_TYPE_UMTS: // 3G
        case TelephonyManager.NETWORK_TYPE_CDMA:
        case TelephonyManager.NETWORK_TYPE_EVDO_0:
        case TelephonyManager.NETWORK_TYPE_EVDO_A:
        case TelephonyManager.NETWORK_TYPE_EVDO_B:
          setThreadCount(2);
          break;
        case TelephonyManager.NETWORK_TYPE_GPRS: // 2G
        case TelephonyManager.NETWORK_TYPE_EDGE:
          setThreadCount(1);
          break;
        default:
          setThreadCount(DEFAULT_THREAD_COUNT);
      }
      break;
    default:
      setThreadCount(DEFAULT_THREAD_COUNT);
  }
}
 
Example 16
Source File: DownloaderService.java    From QtAndroidTools with MIT License 4 votes vote down vote up
/**
 * Updates the network type based upon the type and subtype returned from
 * the connectivity manager. Subtype is only used for cellular signals.
 *
 * @param type
 * @param subType
 */
private void updateNetworkType(int type, int subType) {
    switch (type) {
        case ConnectivityManager.TYPE_WIFI:
        case ConnectivityManager.TYPE_ETHERNET:
        case ConnectivityManager.TYPE_BLUETOOTH:
            mIsCellularConnection = false;
            mIsAtLeast3G = false;
            mIsAtLeast4G = false;
            break;
        case ConnectivityManager.TYPE_WIMAX:
            mIsCellularConnection = true;
            mIsAtLeast3G = true;
            mIsAtLeast4G = true;
            break;
        case ConnectivityManager.TYPE_MOBILE:
            mIsCellularConnection = true;
            switch (subType) {
                case TelephonyManager.NETWORK_TYPE_1xRTT:
                case TelephonyManager.NETWORK_TYPE_CDMA:
                case TelephonyManager.NETWORK_TYPE_EDGE:
                case TelephonyManager.NETWORK_TYPE_GPRS:
                case TelephonyManager.NETWORK_TYPE_IDEN:
                    mIsAtLeast3G = false;
                    mIsAtLeast4G = false;
                    break;
                case TelephonyManager.NETWORK_TYPE_HSDPA:
                case TelephonyManager.NETWORK_TYPE_HSUPA:
                case TelephonyManager.NETWORK_TYPE_HSPA:
                case TelephonyManager.NETWORK_TYPE_EVDO_0:
                case TelephonyManager.NETWORK_TYPE_EVDO_A:
                case TelephonyManager.NETWORK_TYPE_UMTS:
                    mIsAtLeast3G = true;
                    mIsAtLeast4G = false;
                    break;
                case TelephonyManager.NETWORK_TYPE_LTE: // 4G
                case TelephonyManager.NETWORK_TYPE_EHRPD: // 3G ++ interop
                                                          // with 4G
                case TelephonyManager.NETWORK_TYPE_HSPAP: // 3G ++ but
                                                          // marketed as
                                                          // 4G
                    mIsAtLeast3G = true;
                    mIsAtLeast4G = true;
                    break;
                default:
                    mIsCellularConnection = false;
                    mIsAtLeast3G = false;
                    mIsAtLeast4G = false;
            }
    }
}
 
Example 17
Source File: NetworkUtils.java    From Nimingban with Apache License 2.0 4 votes vote down vote up
public static boolean isConnectedWifi(Context context){
    NetworkInfo info = getActiveNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
 
Example 18
Source File: Connectivity.java    From hipda with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Check if there is any connectivity to a Wifi network
 */
public static boolean isConnectedWifi(Context context) {
    NetworkInfo info = Connectivity.getNetworkInfo(context);
    return (info != null && info.isConnected() && info.getType() == ConnectivityManager.TYPE_WIFI);
}
 
Example 19
Source File: VoIPBaseService.java    From Telegram with GNU General Public License v2.0 4 votes vote down vote up
protected int getNetworkType() {
	final NetworkInfo info = lastNetInfo = getActiveNetworkInfo();
	int type = TgVoip.NET_TYPE_UNKNOWN;
	if (info != null) {
		switch (info.getType()) {
			case ConnectivityManager.TYPE_MOBILE:
				switch (info.getSubtype()) {
					case TelephonyManager.NETWORK_TYPE_GPRS:
						type = TgVoip.NET_TYPE_GPRS;
						break;
					case TelephonyManager.NETWORK_TYPE_EDGE:
					case TelephonyManager.NETWORK_TYPE_1xRTT:
						type = TgVoip.NET_TYPE_EDGE;
						break;
					case TelephonyManager.NETWORK_TYPE_UMTS:
					case TelephonyManager.NETWORK_TYPE_EVDO_0:
						type = TgVoip.NET_TYPE_3G;
						break;
					case TelephonyManager.NETWORK_TYPE_HSDPA:
					case TelephonyManager.NETWORK_TYPE_HSPA:
					case TelephonyManager.NETWORK_TYPE_HSPAP:
					case TelephonyManager.NETWORK_TYPE_HSUPA:
					case TelephonyManager.NETWORK_TYPE_EVDO_A:
					case TelephonyManager.NETWORK_TYPE_EVDO_B:
						type = TgVoip.NET_TYPE_HSPA;
						break;
					case TelephonyManager.NETWORK_TYPE_LTE:
						type = TgVoip.NET_TYPE_LTE;
						break;
					default:
						type = TgVoip.NET_TYPE_OTHER_MOBILE;
						break;
				}
				break;
			case ConnectivityManager.TYPE_WIFI:
				type = TgVoip.NET_TYPE_WIFI;
				break;
			case ConnectivityManager.TYPE_ETHERNET:
				type = TgVoip.NET_TYPE_ETHERNET;
				break;
		}
	}
	return type;
}
 
Example 20
Source File: MainActivity.java    From UCDLive_Android with MIT License 4 votes vote down vote up
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    currentPosition = position;
    if (!Utils.isNetworkConnected(this)) {
        Toast.makeText(this, "当前网络不可用.", Toast.LENGTH_SHORT).show();
        return;
    }
    switch (Utils.getConnectedType(this)) {
        case ConnectivityManager.TYPE_MOBILE:
            Toast.makeText(this, "当前网络: mobile", Toast.LENGTH_SHORT).show();
            break;
        case ConnectivityManager.TYPE_ETHERNET:
            Toast.makeText(this, "当前网络: ehternet", Toast.LENGTH_SHORT).show();
            break;
        case ConnectivityManager.TYPE_WIFI:
            Toast.makeText(this, "当前网络: wifi", Toast.LENGTH_SHORT).show();
            break;
        default:
            break;
    }

    if (demoDirects != null && demoDirects.length > position && !TextUtils.isEmpty(demoDirects[position].trim())) {
        String streamId = streamIdEdtxt.getText().toString();
        if (TextUtils.isEmpty(streamId)) {
            Toast.makeText(this, "streamId is null", Toast.LENGTH_SHORT).show();
            return;
        }
        if (position == 0 || position == 1 || position == 2) { //音视频推流 权限检查
            if (permissionsChecker.lacksPermissions(liveNeedPermissions)) {
                PermissionsActivity.startActivityForResult(this, LIVE_REQUEST_CODE, liveNeedPermissions);
            }
            else {
                if (position == 0) {
                    MobclickAgent.onEvent(this, DemoStatistics.RTMP_CAMERA_BUTTON);
                }
                else if (position == 1) {
                    MobclickAgent.onEvent(this, DemoStatistics.RTMP_SCREEN_BUTTON);
                }
                else {
                    MobclickAgent.onEvent(this, DemoStatistics.RTMP_1V1_LINE_BUTTON);
                }
                startActivity(position);
            }
        }
        else if (position == 3) { //纯音频推流 权限检查
            if (permissionsChecker.lacksPermissions(liveAudioNeedPermissions)) {
                PermissionsActivity.startActivityForResult(this, LIVE_AUDIO_REQUEST_CODE, liveAudioNeedPermissions);
            }
            else {
                MobclickAgent.onEvent(this, DemoStatistics.RTMP_ONLY_AUDIO_BUTTON);
                startActivity(position);
            }
        }
        else { //播放 权限检查
            if (permissionsChecker.lacksPermissions(playerNeedPermissions)) {
                PermissionsActivity.startActivityForResult(this, PLAYER_REQUEST_CODE, playerNeedPermissions);
            }
            else {
                MobclickAgent.onEvent(this, DemoStatistics.RTMP_PLAY_BUTTON);
                startActivity(4);
            }
        }
    }
}