//從http://www.reibang.com/p/fef3307293a5拿來的
package com.demo.czh.judgeutilslibrary;
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;
}
}
/**
* 字符串為數(shù)字
*/
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;
}
}
/**
* 帶小數(shù)的數(shù)字
*/
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;
}
}
/**
* 判斷數(shù)字是奇數(shù)還是偶數(shù)
*/
public static int isEvenNumbers(String even) {
if (!TextUtils.isEmpty(even) && isNumber(even)) {
int i = Integer.parseInt(even);
if (i % 2 == 0) {
//偶數(shù)
return 2;
} else {
//奇數(shù)
return 1;
}
} else {
//不是奇數(shù)也不是偶數(shù)
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;
}
}
}
/**
* 判斷字符串是否以指定內(nèi)容開頭
*/
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;
}
}
/**
* 判斷字符串是否以指定內(nèi)容結(jié)尾
*/
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;
}
}
/**
* 判斷字符串中是否含有指定內(nèi)容
*/
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
聯(lián)通:130毕箍、131弛房、132、152而柑、155文捶、156、185牺堰、186
電信:133拄轻、153颅围、180伟葫、189、(1349衛(wèi)通)
總結(jié)起來就是第一位必定為1院促,第二位必定為3或5或8筏养,其他位置的可以為0-9
*/
String telRegex = "[1][358]\\d{9}";//"[1]"代表第1位為數(shù)字1,"[358]"代表第二位可以為3常拓、5渐溶、8中的一個,"\\d{9}"代表后面是可以是0~9的數(shù)字弄抬,有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,判斷是否有網(wǎng)絡連接
*
* @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網(wǎng)絡是否可用
*
* @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網(wǎng)絡是否可用
*
* @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;
}
/**
* 判斷網(wǎng)絡類型
*
* @param context
* @return -1:沒有網(wǎng)絡 1:WIFI網(wǎng)絡2:wap網(wǎng)絡3:net網(wǎng)絡
*/
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;
}
}