package com.xjf.repository.utils; import android.app.ActivityManager; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.os.Environment; import android.text.TextUtils; import java.io.File; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * Created by hasee on 2016/9/26. */ public class CheckForAllUtils { /** * 判断字符串是否为空 */ public static boolean isNotNull(String string) { if (string != null) { return true; } else { return false; } } /** * 判断字符串是否为空和是否等于"" */ public static boolean isNotNull2(String string) { if (string != null && !string.equals("")) { return true; } else { return false; } } /* * 判断字符串是否为空和是否等于""和"null" */ public static boolean isNotNullAndOther(String string, String string2) { if (string != null && !string.equals(string2)) { //不为空且不与string2想等时返回true return true; } else { return false; } } /** * 字符串为数字 */ public static boolean isNumber(String number) { if (TextUtils.isEmpty(number)) return false; else { Pattern p = Pattern.compile("[0-9]*"); Matcher m = p.matcher(number); if (m.matches()) return true; else return false; } } /** * 带小数的数字 */ public static boolean isDecimal(String number) { if (TextUtils.isEmpty(number)) return false; else { Pattern p = Pattern.compile("^[-+]?[0-9]+(\\.[0-9]+)?$"); Matcher m = p.matcher(number); if (m.matches()) return true; else return false; } } /** * 字符串为字母 */ public static boolean isLetter(String letter) { if (TextUtils.isEmpty(letter)) return false; else return letter.matches("^[a-zA-Z]*"); } /** * 字符串是否含有汉字汉字 */ public static boolean hasChinese(String str) { if (TextUtils.isEmpty(str)) return false; else { String regEx = "[\u4e00-\u9fa5]"; Pattern p = Pattern.compile(regEx); Matcher m = p.matcher(str); if (m.find()) return true; else return false; } } /** * 判断数字是奇数还是偶数 */ public static int isEvenNumbers(String even) { if (!TextUtils.isEmpty(even) && isNumber(even)) { int i = Integer.parseInt(even); if (i % 2 == 0) { //偶数 return 2; } else { //奇数 return 1; } } else { //不是奇数也不是偶数 return 0; } } /** * 判断字符串是否字母开头 */ public static boolean isLetterBegin(String s) { if (TextUtils.isEmpty(s)) return false; else { char c = s.charAt(0); int i = (int) c; if ((i >= 65 && i <= 90) || (i >= 97 && i <= 122)) { return true; } else { return false; } } } /** * 判断字符串是否以指定内容开头 */ public static boolean startWithMytext(String mytext, String begin) { if (TextUtils.isEmpty(mytext) && TextUtils.isEmpty(begin)) return false; else { if (mytext.startsWith(begin)) return true; else return false; } } /** * 判断字符串是否以指定内容结尾 */ public static boolean endWithMytext(String mytext, String end) { if (TextUtils.isEmpty(mytext) && TextUtils.isEmpty(end)) return false; else { if (mytext.endsWith(end)) return true; else return false; } } /** * 判断字符串中是否含有指定内容 */ public static boolean hasMytext(String string, String mytext) { if (TextUtils.isEmpty(string) && TextUtils.isEmpty(mytext)) return false; else { if (string.contains(mytext)) return true; else return false; } } /** * 验证是否是手机格式 */ public static boolean isMobileNO(String mobiles) { /* 移动:134、135、136、137、138、139、150、151、157(TD)、158、159、187、188 联通:130、131、132、152、155、156、185、186 电信:133、153、180、189、(1349卫通) 总结起来就是第一位必定为1,第二位必定为3或5或8,其他位置的可以为0-9 */ String telRegex = "[1][358]\\d{9}";//"[1]"代表第1位为数字1,"[358]"代表第二位可以为3、5、8中的一个,"\\d{9}"代表后面是可以是0~9的数字,有9位。 if (TextUtils.isEmpty(mobiles)) return false; else return mobiles.matches(telRegex); } /** * 验证是否是邮箱格式格式 */ public static boolean isEmailAdd(String email) { String emailRegex = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; if (TextUtils.isEmpty(email)) return false; else return email.matches(emailRegex); } /** * 1,判断是否有网络连接 * * @param context * @return */ public static boolean isNetworkConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager .getActiveNetworkInfo(); if (mNetworkInfo != null) { return mNetworkInfo.isAvailable(); } } return false; } /** * 2.判断WIFI网络是否可用 * * @param context * @return */ public static boolean isWifiConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mWiFiNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mWiFiNetworkInfo != null) { return mWiFiNetworkInfo.isAvailable(); } } return false; } /** * 3.判断MOBILE网络是否可用 * * @param context * @return */ public static boolean isMobileConnected(Context context) { if (context != null) { ConnectivityManager mConnectivityManager = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mMobileNetworkInfo = mConnectivityManager .getNetworkInfo(ConnectivityManager.TYPE_MOBILE); if (mMobileNetworkInfo != null) { return mMobileNetworkInfo.isAvailable(); } } return false; } /** * 判断网络类型 * * @param context * @return -1:没有网络 1:WIFI网络2:wap网络3:net网络 */ public static int getNetType(Context context) { int netType = -1; ConnectivityManager connMgr = (ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr.getActiveNetworkInfo(); if (networkInfo == null) { return netType; } int nType = networkInfo.getType(); if (nType == ConnectivityManager.TYPE_MOBILE) { if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) { netType = 3; } else { netType = 2; } } else if (nType == ConnectivityManager.TYPE_WIFI) { netType = 1; } return netType; } /** * 判断当前应用程序处于前台还是后台 */ public static boolean isBackground(Context context) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.processName.equals(context.getPackageName())) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) { //后台 return true; } else { //前台 return false; } } } return false; } /* 用来判断服务是否运行. * @param context * @param className 判断的服务名字 * @return true 在运行 false 不在运行 */ public static boolean isServiceRunning(Context mContext, String className) { boolean isRunning = false; ActivityManager activityManager = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningServiceInfo> serviceList = activityManager.getRunningServices(30); if (!(serviceList.size() > 0)) { return false; } for (int i = 0; i < serviceList.size(); i++) { if (serviceList.get(i).service.getClassName().equals(className) == true) { isRunning = true; break; } } return isRunning; } /** * 判断apk是否安装 */ public static boolean checkApkExist(Context context, String packageName) { if (packageName == null || "".equals(packageName)) return false; try { ApplicationInfo info = context.getPackageManager() .getApplicationInfo(packageName, PackageManager.GET_UNINSTALLED_PACKAGES); return true; } catch (PackageManager.NameNotFoundException e) { return false; } } /** * 判断sd卡是否可以用 */ public static boolean sdCardExists() { if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { // sd card 可用 return true; } else { // 当前不可用 return false; } } /** * 判断一个文件是否存在 */ public static boolean isFileExists(String path) { try { File f = new File(path); if (!f.exists()) { return false; } } catch (Exception e) { return false; } return true; } }