当前位置: 首页>>代码示例>>Java>>正文


Java NetworkInfo.State方法代码示例

本文整理汇总了Java中android.net.NetworkInfo.State方法的典型用法代码示例。如果您正苦于以下问题:Java NetworkInfo.State方法的具体用法?Java NetworkInfo.State怎么用?Java NetworkInfo.State使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在android.net.NetworkInfo的用法示例。


在下文中一共展示了NetworkInfo.State方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
        public void onReceive(Context context, Intent intent) {
            NetworkInfo.State wifiState = null;
            NetworkInfo.State mobileState = null;
            ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
            mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();
            if (wifiState != null && mobileState != null
                    && NetworkInfo.State.CONNECTED != wifiState
                    && NetworkInfo.State.CONNECTED == mobileState) {
//                Toast.makeText(context, context.getString(R.string.net_mobile), Toast.LENGTH_SHORT).show();
                // 手机网络连接成功
            } else if (wifiState != null && mobileState != null
                    && NetworkInfo.State.CONNECTED != wifiState
                    && NetworkInfo.State.CONNECTED != mobileState) {
//                Toast.makeText(context, context.getString(R.string.net_none), Toast.LENGTH_SHORT).show();

                // 手机没有任何的网络
            } else if (wifiState != null && NetworkInfo.State.CONNECTED == wifiState) {
                // 无线网络连接成功
//                Toast.makeText(context, context.getString(R.string.net_wifi), Toast.LENGTH_SHORT).show();

            }
        }
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:25,代码来源:RouteService.java

示例2: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == NetworkInfo.State.CONNECTED;
            if (isConnected) {
                MySharedPre.getInstance().putIpAddress(Utils.getIpAddress());
            } else {
                MySharedPre.getInstance().putIpAddress("0.0.0.0");
            }
        }
    }
}
 
开发者ID:Zane96,项目名称:Fairy,代码行数:17,代码来源:WifiStateReceiver.java

示例3: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    ConnectivityManager connectivityManager=
            (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    if (connectivityManager!=null) {
        if(noNetWorkNotice == null) noNetWorkNotice = NoNetWorkNotice.getInstance(activity);
        NetworkInfo[] networkInfos=connectivityManager.getAllNetworkInfo();
        for (NetworkInfo networkInfo : networkInfos) {
            NetworkInfo.State state = networkInfo.getState();
            if (NetworkInfo.State.CONNECTED == state) {
                if(null != noNetWorkNotice && noNetWorkNotice.isShowing()){
                    noNetWorkNotice.cancel();
                }
                return;
            }
        }
        if(null != noNetWorkNotice && !noNetWorkNotice.isShowing()){
            noNetWorkNotice.show();
        }
    }

}
 
开发者ID:liu-xiao-dong,项目名称:JD-Test,代码行数:23,代码来源:NetWorkChangeBroadcastReceiver.java

示例4: getNetworkState

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取网络状态
 *
 * @param context
 * @return
 */
public static int getNetworkState(Context context) {
    ConnectivityManager connManager = (ConnectivityManager) context
            .getSystemService(Context.CONNECTIVITY_SERVICE);

    // Wifi
    NetworkInfo.State state = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
            .getState();
    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
        return NETWORN_WIFI;
    }

    // 3G
    state = connManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
            .getState();
    if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
        return NETWORN_MOBILE;
    }
    return NETWORN_NONE;
}
 
开发者ID:wanliyang1990,项目名称:AppServiceRestFul,代码行数:26,代码来源:NetUtil.java

示例5: hasState

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * Filter, which returns true if at least one given state occurred
 *
 * @param states NetworkInfo.State, which can have one or more states
 * @return true if at least one given state occurred
 */
public static Predicate<Connectivity> hasState(final NetworkInfo.State... states) {
    return new Predicate<Connectivity>() {
        @Override public boolean test(@NonNull Connectivity connectivity) throws Exception {
            for (NetworkInfo.State state : states) {
                if (connectivity.getState() == state) {
                    return true;
                }
            }
            return false;
        }
    };
}
 
开发者ID:datalink747,项目名称:Rx_java2_soussidev,代码行数:19,代码来源:ConnectivityPredicate.java

示例6: getCurrentNetworkState

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 获取当前网络的状态
 *
 * @param context 上下文
 * @return 当前网络的状态。具体类型可参照NetworkInfo.State.CONNECTED、NetworkInfo.State.CONNECTED.DISCONNECTED等字段。当前没有网络连接时返回null
 */
public static NetworkInfo.State getCurrentNetworkState(Context context) {
    NetworkInfo networkInfo
            = ((ConnectivityManager) context.getSystemService(
            Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
    return networkInfo != null ? networkInfo.getState() : null;
}
 
开发者ID:liying2008,项目名称:Simpler,代码行数:13,代码来源:NetWorkUtils.java

示例7: isGpsNet

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * 检查网络是否是GPRS连接
 */
public static boolean isGpsNet(Context context) {
    ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo.State gprs = manager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

    if (gprs == NetworkInfo.State.CONNECTED || gprs == NetworkInfo.State.CONNECTING) {
        return true;
    }
    return false;

}
 
开发者ID:lennyup,项目名称:react-native-udesk,代码行数:14,代码来源:UdeskUtil.java

示例8: hasState

import android.net.NetworkInfo; //导入方法依赖的package包/类
/**
 * Determines if any of provided {@link android.net.NetworkInfo.State states} occurred.
 *
 * @param networkStates one or many {@link android.net.NetworkInfo.State}
 *
 * @return {@code true} if any of the given network states occurred, {@code false} otherwise
 */
public static Predicate<RxNetworkInfo> hasState(final NetworkInfo.State... networkStates) {
  return new Predicate<RxNetworkInfo>() {
    @Override
    public boolean test(RxNetworkInfo networkInfo) throws Exception {
      return Arrays.asList(networkStates).contains(networkInfo.getState());
    }
  };
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:16,代码来源:RxNetworkInfoPredicate.java

示例9: getState

import android.net.NetworkInfo; //导入方法依赖的package包/类
public NetworkInfo.State getState() {
    return state;
}
 
开发者ID:datalink747,项目名称:Rx_java2_soussidev,代码行数:4,代码来源:Connectivity.java

示例10: onReceive

import android.net.NetworkInfo; //导入方法依赖的package包/类
@Override
public void onReceive(Context context, Intent intent) {
    // 这个监听wifi的打开与关闭,与wifi的连接无关
    if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
        Log.e(TAG, "wifiState" + wifiState);
        switch (wifiState) {
            case WifiManager.WIFI_STATE_DISABLED:
                Log.d(TAG, "onReceive: WIFI_STATE_DISABLED");
                break;
            case WifiManager.WIFI_STATE_DISABLING:
                break;
            case WifiManager.WIFI_STATE_ENABLING:
                break;
            case WifiManager.WIFI_STATE_ENABLED:
                Log.d(TAG, "onReceive: WIFI_STATE_ENABLED");
                break;
            case WifiManager.WIFI_STATE_UNKNOWN:
                break;
            default:
                break;
        }
    }

    // 这个监听wifi的连接状态即是否连上了一个有效无线路由,当上边广播的状态是WifiManager
    // .WIFI_STATE_DISABLING,和WIFI_STATE_DISABLED的时候,根本不会接到这个广播。
    // 在上边广播接到广播是WifiManager.WIFI_STATE_ENABLED状态的同时也会接到这个广播,
    // 当然刚打开wifi肯定还没有连接到有效的无线
    if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
        Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
        if (null != parcelableExtra) {
            NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
            NetworkInfo.State state = networkInfo.getState();
            boolean isConnected = state == State.CONNECTED;// 当然,这边可以更精确的确定状态
            Log.e(TAG, "isConnected" + isConnected);
            if (isConnected) {
            } else {
            }
        }
    }

    // 这个监听网络连接的设置,包括wifi和移动数据的打开和关闭。.
    // 最好用的还是这个监听。wifi如果打开,关闭,以及连接上可用的连接都会接到监听。见log
    // 这个广播的最大弊端是比上边两个广播的反应要慢,如果只是要监听wifi,我觉得还是用上边两个配合比较合适
    if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
        ConnectivityManager manager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        Log.i(TAG, "CONNECTIVITY_ACTION");

        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (activeNetwork != null) { // connected to the internet
            if (activeNetwork.isConnected()) {
                if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
                    // connected to wifi
                    Log.e(TAG, "当前WiFi连接可用 ");
                } else if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE) {
                    // connected to the mobile provider's data plan
                    Log.e(TAG, "当前移动网络连接可用 ");
                }
            } else {
                Log.e(TAG, "当前没有网络连接,请确保你已经打开网络 ");
            }


            Log.e(TAG, "info.getTypeName()" + activeNetwork.getTypeName());
            Log.e(TAG, "getSubtypeName()" + activeNetwork.getSubtypeName());
            Log.e(TAG, "getState()" + activeNetwork.getState());
            Log.e(TAG, "getDetailedState()"+ activeNetwork.getDetailedState().name());
            Log.e(TAG, "getDetailedState()" + activeNetwork.getExtraInfo());
            Log.e(TAG, "getType()" + activeNetwork.getType());
        } else {   // not connected to the internet
            Log.e(TAG, "当前没有网络连接,请确保你已经打开网络 ");
        }
    }
}
 
开发者ID:fendoudebb,项目名称:PlayAndroid,代码行数:76,代码来源:NetworkChangeReceiver.java

示例11: getState

import android.net.NetworkInfo; //导入方法依赖的package包/类
/** @see NetworkInfo#getState() */
public NetworkInfo.State getState() {
  return state;
}
 
开发者ID:greyfoxit,项目名称:RxNetwork,代码行数:5,代码来源:RxNetworkInfo.java


注:本文中的android.net.NetworkInfo.State方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。