/**
* 获取用户的硬件信息
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
public static String getMobilInfo() throws Exception
{
JSONObject mbInfo =new JSONObject() ;
// 通过反射获取用户的硬件信息
Field[] fields =Build.class.getDeclaredFields();
for(Field field :fields){
// 暴力反射 ,获取私有信息
field.setAccessible(true) ;
String name = field.getName() ;
String value =field.get(null).toString() ;
mbInfo.put(name, value);
}
return mbInfo.toString();
}
/**
*得到手机可用内部存储空间
*/
public static long getAvailableInternalMemorySize(){
File path = Environment.getDataDirectory() ;
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize() ;
long availableBlocks=stat.getAvailableBlocks() ;
return availableBlocks* blockSize ;
}
/**
* 得到手机内部所有的存储空间
*/
public static long getTotalInternalMemorySize()
{
File file = Environment.getDataDirectory();
StatFs stat =new StatFs(file.getPath());
long blockSize = stat.getBlockSize() ;
long totalBlocks=stat.getBlockCount() ;
return totalBlocks*blockSize ;
}
/**
* 得到手机可用外部存储空间
*/
public static long getAvailableExternalMemorySize()
{
File path =Environment.getExternalStorageDirectory() ;
StatFs stat= new StatFs(path.getPath());
long blockSize = stat.getAvailableBlocks() ;
long availableBlocks=stat.getBlockCount() ;
return blockSize*availableBlocks ;
}
/**
* 得到手机所有的外部存储空间
*/
public static long getTotalExternalMemorySize()
{
File path = Environment.getExternalStorageDirectory();
StatFs stat= new StatFs(path.getPath());
long blockSize = stat.getBlockSize() ;
long totalBlocks = stat.getBlockCount();
return blockSize*totalBlocks ;
}
/**
* 将得到的字节数转换为MB
*/
public static String formalSize(Context context, long len)
{
return Formatter.formatFileSize(context, len);
}
/**
* 得到cpu 的有关信息
* @return
* @throws Exception
*/
public static String getCpuString() throws Exception
{
if(Build.CPU_ABI.equalsIgnoreCase("x86")){
return "Intel";
}
String strinfo;
byte[] buffer = new byte[1024] ;
RandomAccessFile reader = new RandomAccessFile("/proc/cpuinfo", "r") ;
reader.read(buffer);
String ret = new String (buffer);
int index= ret.indexOf(0);
if(index!=-1){
strinfo = ret.substring(0, index) ;
}else {
strinfo =ret ;
}
return strinfo;
}
/**
* 得到CPU 的类型
* @return
* @throws Exception
*/
public static String getCpuType() throws Exception {
String strInfo = getCpuString() ;
String strType = null;
if(strInfo.contains("ARMv5"))
{
strType = "armv5" ;
}else if(strInfo.contains("ARMv6")){
strType = "armv6" ;
} else if (strInfo.contains("ARMv7")) {
strType = "armv7";
} else if (strInfo.contains("Intel")){
strType = "x86";
}else{
strType = "unknown";
return strType;
}
if (strInfo.contains("neon")) {
strType += "_neon";
}else if (strInfo.contains("vfpv3")) {
strType += "_vfpv3";
}else if (strInfo.contains(" vfp")) {
strType += "_vfp";
}else{
strType += "_none";
}
return strType;
}
/**
* 清除所有的cookie
*/
public static void clearCookie(Context context )
{ CookieSyncManager.createInstance(context);
CookieManager.getInstance().removeAllCookie() ;
}
/**
* 检查sd 卡是否存在并且可以写入
* @param true 可以写入
*/
public static boolean isSdPresent()
{
return Environment.getExternalStorageState().equalsIgnoreCase(Environment.MEDIA_MOUNTED) ;
}
/**
* 向SD卡写入内容
*/
public static void writeSD(File file ) throws Exception{
File sdcardir = Environment.getExternalStorageDirectory() ;// 获取sd卡 的目录
File newFile =new File(sdcardir, file.getName());
FileOutputStream outputStream = new FileOutputStream(newFile);
FileInputStream inputStream = new FileInputStream(file);
byte[] buffer= new byte [1024] ;
int len = 0 ;
while((len = inputStream.read(buffer))!=-1){
outputStream.write(buffer, 0, len);
}
outputStream.close() ;
inputStream.close() ;
}
/**
* 判断当前应用是否处于前台
*/
/**
*判断当前应用程序处于前台还是后台
*需要 <uses-permission android:name="android.permission.GET_TASKS"/>权限
*/
public static boolean isApplicationBroughtToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningTaskInfo> tasks = am.getRunningTasks(1);
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get(0).topActivity;
if (!topActivity.getPackageName().equals(context.getPackageName())) {
return true;
}
}
return false;
}
/**
* 判断当前应用是否处于前台
* 不需要任何权限
*/
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) {
Log.i("后台", appProcess.processName);
return true;
}else{
Log.i("前台", appProcess.processName);
return false;
}
}
}
return false;
}