android 开发过程中的相关代码(二)

/**

*   获取用户的硬件信息 

* @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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值