• Android检测网络是否可用和主机是否可达


    检测网络是否可用主要用到NetworkInfo这个类,检测主机是否可达主要用到InetAddress类

    通过NetworkInfo可以获取网络各种状态,包括但不限于网络是否可用,是否已经连接上或者正在连接,是否漫游,当前状态等;

    InetAddress可以检测某个主机在制定时间内是否可达,用于检测当前环境与目标主机之间的通讯是否可用。

    封装好一下结构,方便使用。

            public static boolean isNetworkReachable(final Context _context, final boolean _useRoaming) {
                boolean isNetworkReachable = false;
                final ConnectivityManager systemService = (ConnectivityManager) _context
                        .getSystemService(Context.CONNECTIVITY_SERVICE);

                /**
                 * Avoid NullPointerException when offline
                 
    */
                if (systemService.getActiveNetworkInfo() == null) {
                    Log.i(LOG_TAG, "Network not reachable!");
                    return false;
                }
                StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork().penaltyLog().build());
                StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());
                final NetworkInfo[] infos = systemService.getAllNetworkInfo();

                boolean userWantIOWhileRoaming = _useRoaming;

                for (final NetworkInfo info : infos) {
                    if (info != null) {
                        if (info.isConnectedOrConnecting()) {
                            final State networkState = info.getState();
                            final boolean isRoamingNow = info.isRoaming();
                            if (State.CONNECTED == networkState
                                    || State.CONNECTING == networkState) {
                                // if (Logging.isEnabled)
                                {
                                    Log.i(LOG_TAG, "Using: ");
                                    Log.i(LOG_TAG, "NetworkInfo.extraInfo : " + info.getExtraInfo());
                                    Log.i(LOG_TAG, "NetworkInfo.reason : " + info.getReason());
                                    Log.i(LOG_TAG, "NetworkInfo.subtypeName : " + info.getSubtypeName());
                                    Log.i(LOG_TAG, "NetworkInfo.state : " + info.getState());
                                    Log.i(LOG_TAG, "NetworkInfo.detailedState : " + info.getDetailedState());
                                    Log.i(LOG_TAG, "NetworkInfo.isAvailable : " + info.isAvailable());
                                    Log.i(LOG_TAG, "NetworkInfo.isConnected : " + info.isConnected());
                                    Log.i(LOG_TAG, "NetworkInfo.isConnectedOrConnecting : " + info.isConnectedOrConnecting());
                                    Log.i(LOG_TAG, "NetworkInfo.isFailover : " + info.isFailover());
                                    Log.i(LOG_TAG, "NetworkInfo.isRoaming : " + info.isRoaming());
                                    
                                    try {
                                        Log.i(LOG_TAG, "Host.isReachable : " + InetAddress.getByName( "192.168.1.27").isReachable(1000));
                                    } catch (UnknownHostException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    } catch (IOException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                                if (userWantIOWhileRoaming) {
                                    isNetworkReachable = true;
                                    break;
                                } else {
                                    isNetworkReachable = isRoamingNow ? false
                                            : true;
                                    break;
                                }
                            }
                        }
                    }
                }
                return isNetworkReachable;

            } 

  • 相关阅读:
    MyEclipse控制台输出tomcat红字
    struts标签bean:define
    字节流转换为对象的方法
    C#将jpg格式图片合成到bmp格式图片中
    [置顶]C#中Socket服务端代码分享
    [置顶] C#中Socket服务端代码分享
    [置顶] 使用Joson的格式字符串在Socket中通讯时数据格式的转换
    [置顶] AMF序列化为对象和AMF序列化为二进制字节流
    用C#获取CPU编号、硬盘编号等系统有关环境、属性
    别把紧张情绪带回家 下班后的10个最佳放松法
  • 原文地址:https://www.cnblogs.com/hopetribe/p/2580637.html
Copyright © 2020-2023  润新知