一些常用的工具函数,包括查看网络状态和字符串的处理判断,经常忘记算是给自己提个醒吧
网络:
* 检测网络是否可用
* @return
*/
public boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();
return ni != null && ni.isConnectedOrConnecting();
}
/**
* 获取当前网络类型
* @return 0:没有网络 1:WIFI网络 2:WAP网络 3:NET网络
*/
public int getNetworkType() {
int netType = 0;
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo == null) {
return netType;
}
int nType = networkInfo.getType();
if (nType == ConnectivityManager.TYPE_MOBILE) {
String extraInfo = networkInfo.getExtraInfo();
if(!StringUtils.isEmpty(extraInfo)){//用到StringUtil工具代码了,判断当前字符是否为空
if (extraInfo.toLowerCase().equals("cmnet")) {
netType = NETTYPE_CMNET;
} else {
netType = NETTYPE_CMWAP;
}
}
} else if (nType == ConnectivityManager.TYPE_WIFI) {
netType = NETTYPE_WIFI;
}
return netType;
}
用到的类ConnectivityManager 和NetworkInfo
getDetailedState():获取详细状态。
getExtraInfo():获取附加信息。
getReason():获取连接失败的原因。
getType():获取网络类型(一般为移动或Wi-Fi)。
getTypeName():获取网络类型名称(一般取值“WIFI”或“MOBILE”)。
isAvailable():判断该网络是否可用。
isConnected():判断是否已经连接。
isConnectedOrConnecting():判断是否已经连接或正在连接。
isFailover():判断是否连接失败。
isRoaming():判断是否漫游。
关于网络类型
public static final int TYPE_NONE = -1;//无网络
public static final int TYPE_MOBILE = 0;//手机网络<pre name="code" class="java"> public static final int TYPE_WIFI = 1;//WIFi,
<span style="background-color: rgb(255, 255, 255); font-family: Arial, Helvetica, sans-serif; font-size: 18px; font-weight: bold;">字符串:</span>
/**
* 判断给定字符串是否空白串。 空白串是指由空格、制表符、回车符、换行符组成的字符串 若输入字符串为null或空字符串,返回true
*
* @param input
* @return boolean
*/
public static boolean isEmpty(String input) {
if (input == null || "".equals(input))
return true;
for (int i = 0; i < input.length(); i++) {
char c = input.charAt(i);
if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
return false;
}
}
return true;
}
/**
* 判断是不是一个合法的电子邮件地址
*
* @param email
* @return
*/
public static boolean isEmail(String email) {
if (email == null || email.trim().length() == 0)
return false;
return emailer.matcher(email).matches();
}
/**
* 字符串转整数
*
* @param str
* @param defValue
* @return
*/
public static int toInt(String str, int defValue) {
try {
return Integer.parseInt(str);
} catch (Exception e) {
}
return defValue;
}
/**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static int toInt(Object obj) {
if (obj == null)
return 0;
return toInt(obj.toString(), 0);
}
/**
* 对象转整数
*
* @param obj
* @return 转换异常返回 0
*/
public static long toLong(String obj) {
try {
return Long.parseLong(obj);
} catch (Exception e) {
}
return 0;
}
/**
* 字符串转布尔值
*
* @param b
* @return 转换异常返回 false
*/
public static boolean toBool(String b) {
try {
return Boolean.parseBoolean(b);
} catch (Exception e) {
}
return false;
}
/**
* 将一个InputStream流转换成字符串
*
* @param is
* @return
*/
public static String toConvertString(InputStream is) {
StringBuffer res = new StringBuffer();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader read = new BufferedReader(isr);
try {
String line;
line = read.readLine();
while (line != null) {
res.append(line);
line = read.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (null != isr) {
isr.close();
isr.close();
}
if (null != read) {
read.close();
read = null;
}
if (null != is) {
is.close();
is = null;
}
} catch (IOException e) {
}
}
return res.toString();
}