public class PackageUtils {
/**
* 获取版本名称
*
* @param context 上下文
*
* @return 版本名称
*/
public static String getVersionName(Context context) {
//获取包管理器
PackageManager pm = context.getPackageManager();
//获取包信息
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
//返回版本号
return packageInfo.versionName;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
/**
* 获取版本号
*
* @param context 上下文
*
* @return 版本号
*/
public static int getVersionCode(Context context) {
//获取包管理器
PackageManager pm = context.getPackageManager();
//获取包信息
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
//返回版本号
return packageInfo.versionCode;
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return 0;
}
/**
* 获取App的名称
*
* @param context 上下文
*
* @return 名称
*/
public static String getAppName(Context context) {
PackageManager pm = context.getPackageManager();
//获取包信息
try {
PackageInfo packageInfo = pm.getPackageInfo(context.getPackageName(), 0);
//获取应用 信息
ApplicationInfo applicationInfo = packageInfo.applicationInfo;
//获取albelRes
int labelRes = applicationInfo.labelRes;
//返回App的名称
return context.getResources().getString(labelRes);
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
}
public class StorageUtil {
/**
* 获得SD卡总大小
*
* @return
*/
public static String getSDTotalSize(Context context) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
public static long getSDTotalSizeLong() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return blockSize * totalBlocks;
}
/**
* 获得sd卡剩余容量,即可用大小
*
* @return
*/
public static String getSDAvailableSize(Context context) {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
public static long getSDAvailableSizeLong() {
File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks;
}
/**
* 获得Data总大小
*
* @return
*/
public static String getDataTotalSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
public static long getDataTotalSizeLong() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return blockSize * totalBlocks;
}
/**
* 获得Data剩余容量,即可用大小
*
* @return
*/
public static String getDataAvailableSize(Context context) {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
public static long getDataAvailableSizeLong() {
File path = Environment.getDataDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks;
}
/**
* 获得Root总大小
*
* @return
*/
public static String getRootTotalSize(Context context) {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return Formatter.formatFileSize(context, blockSize * totalBlocks);
}
public static long getRootTotalSizeLong() {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
return blockSize * totalBlocks;
}
/**
* 获得Root剩余容量,即可用大小
*
* @return
*/
public static String getRootAvailableSize(Context context) {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return Formatter.formatFileSize(context, blockSize * availableBlocks);
}
public static long getRootAvailableSizeLong() {
File path = Environment.getRootDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
return blockSize * availableBlocks;
}
/**
* method 获取可用运行内存
* description
* date: 2020-08-28 16:55
* param
* return
*/
public static String getAvailMemory(Context context){
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo=new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memInfo);
return memInfo.availMem/1024/1024/1024.0+" GB";
// Log.e("memInfo", "availMem:"+memInfo.availMem/1024/1024/1024.0+" GB");//可用
// Log.e("memInfo", "threshold:"+memInfo.threshold/1024/1024/1024.0+" GB");//low memory threshold
// Log.e("memInfo", "totalMem:"+memInfo.totalMem/1024/1024/1024.0+" GB");//总大小
// Log.e("memInfo", "lowMemory:"+memInfo.lowMemory); //if current is in low memory
}
public static long getAvailMemoryLong(Context context){
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo=new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memInfo);
return memInfo.availMem;
// Log.e("memInfo", "availMem:"+memInfo.availMem/1024/1024/1024.0+" GB");//可用
// Log.e("memInfo", "threshold:"+memInfo.threshold/1024/1024/1024.0+" GB");//low memory threshold
// Log.e("memInfo", "totalMem:"+memInfo.totalMem/1024/1024/1024.0+" GB");//总大小
// Log.e("memInfo", "lowMemory:"+memInfo.lowMemory); //if current is in low memory
}
/**
* method 获取总运行内存
* description
* date: 2020-08-28 16:55
* param
* return
*/
public static String getTotalMemory(Context context){
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo=new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memInfo);
return memInfo.totalMem/1024/1024/1024.0+" GB";
// Log.e("memInfo", "availMem:"+memInfo.availMem/1024/1024/1024.0+" GB");//可用
// Log.e("memInfo", "threshold:"+memInfo.threshold/1024/1024/1024.0+" GB");//low memory threshold
// Log.e("memInfo", "totalMem:"+memInfo.totalMem/1024/1024/1024.0+" GB");//总大小
// Log.e("memInfo", "lowMemory:"+memInfo.lowMemory); //if current is in low memory
}
public static long getTotalMemoryLong(Context context){
ActivityManager activityManager=(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memInfo=new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memInfo);
return memInfo.totalMem;
// Log.e("memInfo", "availMem:"+memInfo.availMem/1024/1024/1024.0+" GB");//可用
// Log.e("memInfo", "threshold:"+memInfo.threshold/1024/1024/1024.0+" GB");//low memory threshold
// Log.e("memInfo", "totalMem:"+memInfo.totalMem/1024/1024/1024.0+" GB");//总大小
// Log.e("memInfo", "lowMemory:"+memInfo.lowMemory); //if current is in low memory
}