判断是否有网络可用及获取联网方式

1.  ConnectivityManager cManager = (ConnectivityManager)  getSystemService(this.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = cManager.getActiveNetworkInfo();


  int type = networkInfo.getType();

   switch(type){
case ConnectivityManager.TYPE_MOBILE: // 手机接入方式
networkType = "MOBILE";
break;
case ConnectivityManager.TYPE_WIFI: // Wifi接入方式
networkType = "WIFI";
break;
}

  boolean available = false;
  NetworkInfo[] infos = connec.getAllNetworkInfo();
if(infos != null) {
    for(NetworkInfo info : infos) {
        if(info.isAvailable()){
            available = true;
            break;
        }
    }
}
return available;

你可能感兴趣的:(android,mobile)