Java Code Examples for android.net.NetworkInfo#getState()

The following examples show how to use android.net.NetworkInfo#getState() . 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: ConnectionsManager.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static boolean isConnectedOrConnectingToWiFi()
{
    try
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null)
            return false;

        NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        NetworkInfo.State state = netInfo.getState();
        if (state == NetworkInfo.State.CONNECTED ||
                state == NetworkInfo.State.CONNECTING ||
                state == NetworkInfo.State.SUSPENDED)
            return true;
    }
    catch (Exception e)
    {
        FileLog.e(e);
    }

    return false;
}
 
Example 2
Source File: VenvyDeviceUtil.java    From VideoOS-Android-SDK with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                // 当前网络是连接的
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    // 当前所连接的网络可用
                    return true;
                }
            }
        }
        return false;
    } catch (Exception e) {
        VenvyLog.e(VenvyDeviceUtil.TAG, e);
        return true;
    }
}
 
Example 3
Source File: AppUtil.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
/**
 * 描述:判断网络是否有效.
 *
 * @return true, if is network available
 */
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}
 
Example 4
Source File: NetUtils.java    From coderfun with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 判断网络是否连接
 *
 * @param context
 * @return
 */
public static boolean isConnected(Context context) {

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

    if (null != connectivity) {

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
Example 5
Source File: AppPresenter.java    From UPMiss with GNU General Public License v3.0 6 votes vote down vote up
public static boolean isHaveNetwork() {
    final Context context = Model.getApplication();
    if (context == null)
        return false;
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager == null) {
        return false;
    } else {
        NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
        if (networkInfo != null && networkInfo.length > 0) {
            for (NetworkInfo aNetworkInfo : networkInfo) {
                if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 6
Source File: NetworkUtils.java    From Elephant with Apache License 2.0 6 votes vote down vote up
public static boolean isNetworkAvailable(Context c) {
        Context context = c.getApplicationContext();
        // 获取手机所有连接管理对象(包括对wi-fi,net等连接的管理)
        ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null) {
            return false;
        } else {
            // 获取NetworkInfo对象
            NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();

            if (networkInfo != null && networkInfo.length > 0) {
                for (NetworkInfo aNetworkInfo : networkInfo) {
//                    System.out.println(i + "===状态===" + networkInfo[i].getState());
//                    System.out.println(i + "===类型===" + networkInfo[i].getTypeName());
                    // 判断当前网络状态是否为连接状态
                    if (aNetworkInfo.getState() == NetworkInfo.State.CONNECTED) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
 
Example 7
Source File: NetworkUtils.java    From ReadMark with Apache License 2.0 6 votes vote down vote up
/**
 * 判断网络是否连接
 *
 * @param context
 * @return
 */
public static boolean isConnected(Context context) {

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

    if (null != connectivity) {

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {
                return true;
            }
        }
    }
    return false;
}
 
Example 8
Source File: DeviceUtils.java    From MVPArms with Apache License 2.0 6 votes vote down vote up
/**
 * wifi是否开启
 *
 * @param context
 * @return
 */
public static boolean isWifiOpen(Context context) {
    boolean isWifiConnect = false;
    ConnectivityManager cm = (ConnectivityManager) context.getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
    // check the networkInfos numbers
    NetworkInfo[] networkInfos = cm.getAllNetworkInfo();
    for (NetworkInfo networkInfo : networkInfos) {
        if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                isWifiConnect = false;
            }
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                isWifiConnect = true;
            }
        }
    }
    return isWifiConnect;
}
 
Example 9
Source File: NetUtil.java    From MeiZiNews with MIT License 6 votes vote down vote up
/**
 * 判断网络是否连接
 *
 * @param context
 * @return
 */
public static boolean isConnected(Context context) {

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

    if (null != connectivity) {

        NetworkInfo info = connectivity.getActiveNetworkInfo();
        if (null != info && info.isConnected()) {
            if (info.getState() == NetworkInfo.State.CONNECTED) {

                return true;
            }
        }
    }
    return false;
}
 
Example 10
Source File: DeviceUtils.java    From SmartZPN with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean checkNet(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        return false;
    }
    return false;
}
 
Example 11
Source File: ConnectionsManager.java    From TelePlus-Android with GNU General Public License v2.0 6 votes vote down vote up
public static boolean isConnectedToWiFi()
{
    try
    {
        ConnectivityManager connectivityManager = (ConnectivityManager) ApplicationLoader.applicationContext.getSystemService(
                Context.CONNECTIVITY_SERVICE);
        if (connectivityManager == null)
            return false;

        NetworkInfo netInfo = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
        if (netInfo != null && netInfo.getState() == NetworkInfo.State.CONNECTED)
            return true;
    }
    catch (Exception e)
    {
        FileLog.e(e);
    }

    return false;
}
 
Example 12
Source File: AppUtil.java    From Rx-Retrofit with MIT License 6 votes vote down vote up
/**
 * 描述:判断网络是否有效.
 *
 * @return true, if is network available
 */
public static boolean isNetworkAvailable(Context context) {
    try {
        ConnectivityManager connectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        if (connectivity != null) {
            NetworkInfo info = connectivity.getActiveNetworkInfo();
            if (info != null && info.isConnected()) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
        return false;
    }
    return false;
}
 
Example 13
Source File: WifiStateChangeService.java    From fdroidclient with GNU General Public License v3.0 5 votes vote down vote up
@Override
protected void onHandleIntent(Intent intent) {
    android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_LOWEST);
    if (intent == null) {
        Utils.debugLog(TAG, "received null Intent, ignoring");
        return;
    }
    Utils.debugLog(TAG, "WiFi change service started.");
    NetworkInfo ni = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
    wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
    wifiState = wifiManager.getWifiState();
    Utils.debugLog(TAG, "ni == " + ni + "  wifiState == " + printWifiState(wifiState));
    if (ni == null
            || ni.getState() == NetworkInfo.State.CONNECTED || ni.getState() == NetworkInfo.State.DISCONNECTED) {
        if (previousWifiState != wifiState &&
                (wifiState == WifiManager.WIFI_STATE_ENABLED
                        || wifiState == WifiManager.WIFI_STATE_DISABLING  // might be switching to hotspot
                        || wifiState == WifiManager.WIFI_STATE_DISABLED   // might be hotspot
                        || wifiState == WifiManager.WIFI_STATE_UNKNOWN)) { // might be hotspot
            if (wifiInfoThread != null) {
                wifiInfoThread.interrupt();
            }
            wifiInfoThread = new WifiInfoThread();
            wifiInfoThread.start();
        }

        if (Build.VERSION.SDK_INT < 21 && wifiState == WifiManager.WIFI_STATE_ENABLED) {
            UpdateService.scheduleIfStillOnWifi(this);
        }
    }
}
 
Example 14
Source File: NetworkUtil.java    From ZZShow with Apache License 2.0 5 votes vote down vote up
/**
 *  检查当前网络是否可用
 * @return 是否连接到网络
 */
public static boolean isNetworkAvailable(){
    ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.getInstance()
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    if(connectivityManager != null){
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if(info != null && info.isConnected()){
            if(info.getState() == NetworkInfo.State.CONNECTED){
                return true;
            }
        }
    }
    return false;
}
 
Example 15
Source File: NetWorkUtils.java    From ZhihuDaily with Apache License 2.0 5 votes vote down vote up
public static final boolean isNetWorkAvailable(Context context) {
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager != null) {
        NetworkInfo[] networkInfos=connectivityManager.getAllNetworkInfo();
        if (networkInfos != null) {
            for (NetworkInfo info : networkInfos) {
                if (info.getState() == NetworkInfo.State.CONNECTED) {
                    return true;
                }
            }
        }
    }
    return false;
}
 
Example 16
Source File: LinphoneManager.java    From Linphone4Android with GNU General Public License v3.0 5 votes vote down vote up
public void updateNetworkReachability() {
	ConnectivityManager cm = (ConnectivityManager) mServiceContext.getSystemService(Context.CONNECTIVITY_SERVICE);
	NetworkInfo eventInfo = cm.getActiveNetworkInfo();

	if (eventInfo == null || eventInfo.getState() == NetworkInfo.State.DISCONNECTED) {
		Log.i("No connectivity: setting network unreachable");
		mLc.setNetworkReachable(false);
	} else if (eventInfo.getState() == NetworkInfo.State.CONNECTED){
		manageTunnelServer(eventInfo);

		boolean wifiOnly = LinphonePreferences.instance().isWifiOnlyEnabled();
		if (wifiOnly){
			if (eventInfo.getType()==ConnectivityManager.TYPE_WIFI)
				mLc.setNetworkReachable(true);
			else {
				Log.i("Wifi-only mode, setting network not reachable");
				mLc.setNetworkReachable(false);
			}
		}else{
			int curtype=eventInfo.getType();

			if (curtype!=mLastNetworkType){
				//if kind of network has changed, we need to notify network_reachable(false) to make sure all current connections are destroyed.
				//they will be re-created during setNetworkReachable(true).
				Log.i("Connectivity has changed.");
				mLc.setNetworkReachable(false);
			}
			mLc.setNetworkReachable(true);
			mLastNetworkType=curtype;
		}
	}

	if (mLc.isNetworkReachable()) {
		// When network isn't available, push informations might not be set. This should fix the issue.
		LinphonePreferences prefs = LinphonePreferences.instance();
		prefs.setPushNotificationEnabled(prefs.isPushNotificationEnabled());
	}
}
 
Example 17
Source File: NetworkManager.java    From exodus-android-app with GNU General Public License v3.0 5 votes vote down vote up
private boolean isConnectedToInternet(Context context) {
    //verify the connectivity
    ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if(connectivityManager == null)
        return false;
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
    if (networkInfo != null) {
        NetworkInfo.State networkState = networkInfo.getState();
        if (networkState.equals(NetworkInfo.State.CONNECTED)) {
            return true;
        }
    }
    return false;
}
 
Example 18
Source File: NetworkUtils.java    From LRecyclerView with Apache License 2.0 5 votes vote down vote up
/**
 * 获取当前网络状态 返回2代表wifi,1代表2G/3G
 *
 * @param paramContext
 * @return
 */
public static String[] getNetType(Context paramContext) {
    String[] arrayOfString = {"Unknown", "Unknown"};
    PackageManager localPackageManager = paramContext.getPackageManager();
    if (localPackageManager.checkPermission("android.permission.ACCESS_NETWORK_STATE", paramContext.getPackageName()) != 0) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }

    ConnectivityManager localConnectivityManager = (ConnectivityManager) paramContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (localConnectivityManager == null) {
        arrayOfString[0] = "Unknown";
        return arrayOfString;
    }

    NetworkInfo localNetworkInfo1 = localConnectivityManager.getNetworkInfo(1);
    if (localNetworkInfo1 != null && localNetworkInfo1.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "2";
        return arrayOfString;
    }

    NetworkInfo localNetworkInfo2 = localConnectivityManager.getNetworkInfo(0);
    if (localNetworkInfo2 != null && localNetworkInfo2.getState() == NetworkInfo.State.CONNECTED) {
        arrayOfString[0] = "1";
        arrayOfString[1] = localNetworkInfo2.getSubtypeName();
        return arrayOfString;
    }

    return arrayOfString;
}
 
Example 19
Source File: NetworkConnectChangedReceiver.java    From apollo-DuerOS with Apache License 2.0 4 votes vote down vote up
@Override
public void onReceive(Context context, Intent intent) {
    // Monitor wifi status, have nothing to do with wifi connection
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        LogUtil.d(TAG, "wifiState:" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            default:
                break;
        }
    }
    // Monitor the connection status of WiFi, that is whether or not there is an effective wireless routing
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent
                .getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            // Get the network status of the NetWorkInfo object
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            // The State object is the state of connection success or not
            NetworkInfo.State state = networkInfo.getState();
            // Determine whether or not the network has been connected
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            LogUtil.d(TAG, "isConnected:" + isConnected);
        }
    }
    // To monitor the network connection, including wifi and mobile data opening and closing,
    // as well as connection available on the connection had been listening
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        // Get the NetworkInfo object of the network state
        NetworkInfo info = intent
                .getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
        if (info != null) {
            // If the current network connection is successful and the network connection is available
            if (NetworkInfo.State.CONNECTED == info.getState() && info.isAvailable()) {
                if (info.getType() == ConnectivityManager.TYPE_WIFI
                        || info.getType() == ConnectivityManager.TYPE_MOBILE) {
                    LogUtil.i(TAG, getConnectionType(info.getType()) + " connected");
                    // transform to the following module
                    if (mOnNetworkChangeListener != null) {
                        LogUtil.i(TAG, "mOnLocationListener is set");
                        mOnNetworkChangeListener.onNetworkChange();
                    }
                }
            } else {
                LogUtil.i(TAG, getConnectionType(info.getType()) + " disconnected");
                if (mOnNetworkChangeListener != null) {
                    LogUtil.i(TAG, "onShowCompass is set");
                    mOnNetworkChangeListener.onShowCompass();
                }
            }
        }

    }
}
 
Example 20
Source File: NetworkUtils.java    From gank with GNU General Public License v3.0 4 votes vote down vote up
public static NetworkInfo.State getCurrentNetworkState(Context context) {
    NetworkInfo networkInfo = ((ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null?networkInfo.getState():null;
}