public class SysUtils {
private static final String TAG = "SysUtils";
private SysUtils() {
}
/**獲取版本號
* @param context
* @return
*/
public static String getVersionName(Context context) {
PackageManager pkgMng = context.getPackageManager();
PackageInfo pkgInfo = null;
try {
pkgInfo = pkgMng.getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e) {
e.printStackTrace();
}
return pkgInfo != null ? pkgInfo.versionName : "";
}
/**
* 獲取SD卡目錄
* @return 如果沒有SD卡就返回null
*/
public static String getSdDir() {
boolean sdCardExist = Environment.getExternalStorageState().equals(
Environment.MEDIA_MOUNTED); // 判斷sd卡是否存在
if (sdCardExist) {
return Environment.getExternalStorageDirectory().toString(); // 獲取目錄
}
return null;
}
/**
* 獲取應用快取目錄
* @param context
* @return
*/
public static String getCacheDir(Context context) {
return context.getCacheDir().toString();
}
/**獲取應用的語言
* @param context
* @return
*/
public static String getLanguage(Context context) {
Locale locale = context.getResources().getConfiguration().locale;
return locale.getLanguage();
}
/**
* 判斷程式是否在後臺執行
* @param context
* @return
*/
public static boolean isBackground(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
return true;
} else {
return false;
}
}
}
return false;
}
/**判斷app是否前臺可見
* @param context
* @return
*/
public static boolean isVisible(Context context) {
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager
.getRunningAppProcesses();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.processName.equals(context.getPackageName())) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_VISIBLE ||
appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
return true;
} else {
return false;
}
}
}
return false;
}
}