首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

android判断是不是有网络

2012-08-21 
android判断是否有网络1public static boolean checkNet(Activity act) {ConnectivityManager manager (

android判断是否有网络

1

public static boolean checkNet(Activity act) {                 ConnectivityManager manager = (ConnectivityManager) act                .getApplicationContext().getSystemService(                       Context.CONNECTIVITY_SERVICE);                 if (manager == null) {             return false;         }                 NetworkInfo networkinfo = manager.getActiveNetworkInfo();                 if (networkinfo == null || !networkinfo.isAvailable()) {             return false;         }            return true;      }  

?2 大同小异

public static boolean checkNet_(Context context) {ConnectivityManager mConnectivity = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);NetworkInfo info = mConnectivity.getActiveNetworkInfo();if (info == null) {return false;}if (!info.isAvailable() || !mConnectivity.getBackgroundDataSetting()) {return false;} else {return true;}}

?3

private int checkNet__(Context context) {ConnectivityManager connectionManager = (ConnectivityManager) context.getSystemService("connectivity");NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();if(networkInfo != null){if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {return 1;    //返回1是 WIFI网络} else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {return 2;    //返回 2是移动互联网()} else {return 3;    //返回3是 未知网络}}else{return 0;}}
?

热点排行