/**
* 网络是否可用
*
* @param context
* @return
*/
public static boolean isNetworkAvailable(Context context) {
ConnectivityManager mgr = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] info = mgr.getAllNetworkInfo();
if (info != null) {
for (int i = 0; i < info.length; i++) {
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}
}
}
return false;
}
/**
* 电话号码验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isPhone(String str) {
// 正则表达式:验证手机号
String regexPhone = "^((13[0-9])|14[7]|(15[^4,\\D])|17[3,6,7,8]|(18[0-9]))\\d{8}$";
Pattern p = Pattern.compile(regexPhone);
Matcher m = p.matcher(str);
return m.matches();
}
/**
* 邮箱验证
*
* @param str
* @return 验证通过返回true
*/
public static boolean isMail(String str) {
// 正则表达式:验证手机号
// String regexMail =
// "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
String regexMail = "[\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?";
// [\\w!#$%&'*+/=?^_`{|}~-]+(?:\\.[\\w!#$%&'*+/=?^_`{|}~-]+)*@(?:[\\w](?:[\\w-]*[\\w])?\\.)+[\\w](?:[\\w-]*[\\w])?
Pattern p = Pattern.compile(regexMail);
Matcher m = p.matcher(str);
String[] strs = str.split("@");
String tempStr = strs[0].replace("_", "").replace(".", "");
if (!isPwdFormat(tempStr)) {
return false;
}
return m.matches();
}
Android判断网络是否可用
最新推荐文章于 2024-04-15 13:35:08 发布