Android 检测网络是否打开

Android 检测网络是否打开

 

1.网络是否连接(包括Wifi和移动网络)

private boolean isNetworkConnected() {  

        ConnectivityManager cm =   

                (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);  

        NetworkInfo network = cm.getActiveNetworkInfo();  

        if (network != null) {  

            return network.isAvailable();  

        }  

        return false;

}  

2.wifi是否可用

    private boolean isWifiEnable() {  

        WifiManager wifiManager = (WifiManager) mContext  

                .getSystemService(Context.WIFI_SERVICE);  

        return wifiManager.isWifiEnabled();  

    }

3.GPS是否可用

private boolean isGpsEnable() {  

        LocationManager locationManager =   

                ((LocationManager) mContext.getSystemService(Context.LOCATION_SERVICE));  

        return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);  

    }

//Android与Internet连接的程序方式(实现HTTP连接)

1、通过java.net 中的 HttpURLConnection    (自实现)

2、通过org.apach.commons.httpclient 中的 HttpClient   (封装)

3、通过Android.net.http.* 中的 AndroidHttpClient(主要使用 Apache HttpClient)

4、通过com.google.Android.net.* 中的 GoogleHttpClient(主要使用 AndroidHttpClient)

猜你喜欢

转载自technicalsearch.iteye.com/blog/1791936