android 开发过程中的相关代码(二)
来源:互联网 发布:网络前端设计师 编辑:程序博客网 时间:2024/06/05 02:59
/**
* 获取用户的硬件信息
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
publicstatic 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();
}
/**
*得到手机可用内部存储空间
*/
publicstatic long getAvailableInternalMemorySize(){
File path = Environment.getDataDirectory() ;
StatFs stat =new StatFs(path.getPath());
long blockSize = stat.getBlockSize() ;
long availableBlocks=stat.getAvailableBlocks() ;
return availableBlocks* blockSize ;
}
/**
* 得到手机内部所有的存储空间
*/
publicstatic long getTotalInternalMemorySize()
{
File file = Environment.getDataDirectory();
StatFs stat =new StatFs(file.getPath());
long blockSize = stat.getBlockSize() ;
long totalBlocks=stat.getBlockCount() ;
return totalBlocks*blockSize ;
}
/**
* 得到手机可用外部存储空间
*/
publicstatic long getAvailableExternalMemorySize()
{
File path =Environment.getExternalStorageDirectory() ;
StatFs stat=new StatFs(path.getPath());
long blockSize = stat.getAvailableBlocks() ;
long availableBlocks=stat.getBlockCount() ;
return blockSize*availableBlocks ;
}
/**
* 得到手机所有的外部存储空间
*/
publicstatic long getTotalExternalMemorySize()
{
File path = Environment.getExternalStorageDirectory();
StatFs stat=new StatFs(path.getPath());
long blockSize = stat.getBlockSize() ;
long totalBlocks = stat.getBlockCount();
return blockSize*totalBlocks ;
}
/**
* 将得到的字节数转换为MB
*/
publicstatic String formalSize(Context context, long len)
{
returnFormatter.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" ;
}elseif(strInfo.contains("ARMv6")){
strType ="armv6" ;
} elseif (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())) {
returntrue;
}
}
returnfalse;
}
/**
* 判断当前应用是否处于前台
* 不需要任何权限
*/
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;
}
}
}
returnfalse;
}
- android 开发过程中的相关代码(二)
- 软件开发过程中的事务处理(二)
- android开发过程中使用功能性代码片段(二)
- 实习过程中linux相关开发学习总结(二)
- Android 闹钟 开发过程记录(二)
- ios开发过程中的相关加密算法
- ios开发过程中的相关加密算法
- FFMPEG开发过程中的相关术语
- Android开发实用代码片段(二)
- Android笔记(二 )Eclipse开发相关/Android项目工程
- Coolite开发过程中的学习笔记(二)
- NGN-android开发中的知识点(二)
- android开发过程中的tips
- Android开发过程随记_NDK编程相关
- Android蓝牙开发(二):相关的API简介
- j2ee(SSH)项目开发中的代码小结(二)
- android蓝牙开发过程中遇到的问题(二)
- Android 手机影音 开发过程记录(二)
- 常用dos命令
- Linux和windows下sleep的使用
- 最短路打印路径SPFA(good)uva11374
- Git学习笔记
- 归并排序标准代码
- android 开发过程中的相关代码(二)
- 内核配置与裁剪浅析
- guerisson 奇迹马油霜
- 子数组的最大乘积
- 360 SDK添加之 登陆界面未弹出来解决方案
- Visual Studio 2012下直接调用boost_1_55_0库
- 数组下表越界
- UTF-8编码的详细讲解
- 2014多校第四场1006 HDU 4902 Nice boat 线段树 区间更新问题