1、网络是否可用
/**
* 网络是否可用。
*/
public static boolean isNetworkAvaliable(Context context) {
try {
final ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
final NetworkInfo info = connectivity.getActiveNetworkInfo();
if (info != null && info.isConnected()) {
if (info.getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
} catch (Exception e) {
return false;
}
return false;
}
2、获取运营商类型
/**
* 获得运营商类型
*
* @return OperatorTypes
*/
public static int getOperatorType(Context context) {
// 获取运营商信息
final TelephonyManager telManager = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
String operator = telManager.getSimOperator();
if (operator != null) {
if (operator.equals("46000") || operator.equals("46002")
|| operator.equals("46007")) {
// 中国移动
return OperatorTypes.CMCC;
} else if (operator.equals("46001")) {
// 中国联通
return OperatorTypes.CUCC;
} else if (operator.equals("46003")) {
// 中国电信
return OperatorTypes.CTCC;
}
}
return OperatorTypes.UNKNOWN;
}
public interface OperatorTypes {
public static final int UNKNOWN = -1;
public static final int CMCC = 0;
public static final int CUCC = 1;
public static final int CTCC = 2;
}