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;

        } 

posted @ 2012-07-07 15:03  hopetribe  阅读(1239)  评论(0编辑  收藏  举报