常用工具类,文件和内存的大小获取,shell脚本的执行

/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.cts.util;

import android.app.ActivityManager;
import android.app.ActivityManager.MemoryInfo;
import android.app.Instrumentation;
import android.content.Context;
import android.os.ParcelFileDescriptor;
import android.os.StatFs;

import java.io.FileInputStream;
import java.io.IOException;

public class SystemUtil {
    public static long getFreeDiskSize(Context context) {
        StatFs statFs = new StatFs(context.getFilesDir().getAbsolutePath());
        return (long)statFs.getAvailableBlocks() * statFs.getBlockSize();
    }

    public static long getFreeMemory(Context context) {
        MemoryInfo info = new MemoryInfo();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(info);
        return info.availMem;
    }

    public static long getTotalMemory(Context context) {
        MemoryInfo info = new MemoryInfo();
        ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(info);
        return info.totalMem; // TODO totalMem N/A in ICS.
    }

    /**
     * Executes a shell command using shell user identity, and return the standard output in string
     * <p>Note: calling this function requires API level 21 or above
     * @param instrumentation {@link Instrumentation} instance, obtained from a test running in
     * instrumentation framework
     * @param cmd the command to run
     * @return the standard output of the command
     * @throws Exception
     */
    public static String runShellCommand(Instrumentation instrumentation, String cmd)
            throws IOException {
        ParcelFileDescriptor pfd = instrumentation.getUiAutomation().executeShellCommand(cmd);
        byte[] buf = new byte[512];
        int bytesRead;
        FileInputStream fis = new ParcelFileDescriptor.AutoCloseInputStream(pfd);
        StringBuffer stdout = new StringBuffer();
        while ((bytesRead = fis.read(buf)) != -1) {
            stdout.append(new String(buf, 0, bytesRead));
        }
        fis.close();
        return stdout.toString();
    }

}
时间: 2024-08-11 13:43:54

常用工具类,文件和内存的大小获取,shell脚本的执行的相关文章

[C#] 常用工具类——文件操作类

/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在</para> /// <para> IsImgFilename:判断文件名是否为浏览器可以直接显示的图片文件名</para> /// <para> CopyFiles:复制指定目录的所有文件</para> /// <para> MoveFi

c#常用工具类:文件和二进制转换

//================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// </summary> /// <param name="FilePath">文件完整路径</param> /// <returns>二进制数组</returns> public static byte[] FileToBinar

Android 常用工具类之SPUtil,可以修改默认sp文件的路径

参考: 1. 利用Java反射机制改变SharedPreferences存储路径    Singleton1900 2. Android快速开发系列 10个常用工具类 Hongyang import android.app.Activity; import android.content.Context; import android.content.ContextWrapper; import android.content.SharedPreferences; import java.io.

第03章(常用工具类)

1 /***************** 2 ***第三章常用工具类 3 *******知识点: 4 **************1.系统相关 5 ******************1.1 System类 6 ******************1.2 Runtime类 7 ******************1.3 输入类 8 **************************1.3.1 Scanner类 9 **************************1.3.2 Buffered

Android常用工具类(收藏)

Android常用工具类 主要介绍总结的Android开发中常用的工具类,大部分同样适用于Java. 目前包括(HttpUtils.DownloadManagerPro.ShellUtils.PackageUtils.PreferencesUtils.JSONUtils.FileUtils.ResourceUtils.StringUtils.ParcelUtils.RandomUtils.ArrayUtils.ImageUtils.ListUtils.MapUtils.ObjectUtils.S

简单了解Spring中常用工具类_java - JAVA

文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口,Resource 接口是为了统一各种类型不同的资源而定义的,Spring 提供了若干 Resource 接口的实现类,这些实现类可以轻松地加载不同类型的底层资源,并提供了获取文件名.URL 地址以及资源内容的操作方法 访问文件资源 * 通过 FileSystemResource 以文件系统绝对路径的

js常用工具类.

一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * 使用方式一: * var now = new Date(); * var nowStr = now.dateFormat("yyyy-MM-dd hh:mm:ss"); * 使用方式二: * new Date().dateFormat("yyyy年MM月dd日");

java常用工具类(java技术交流群57388149)

package com.itjh.javaUtil; import java.util.ArrayList; import java.util.List; /** * * String工具类. <br> * * @author 宋立君 * @date 2014年06月24日 */ public class StringUtil { private static final int INDEX_NOT_FOUND = -1; private static final String EMPTY =

C#常用工具类——Excel操作类

/// 常用工具类——Excel操作类 /// <para> ------------------------------------------------</para> /// <para> CreateConnection:根据Excel文件路径和EXCEL驱动版本生成OleConnection对象实例</para> /// <para> ExecuteDataSet:执行一条SQL语句,返回一个DataSet对象</para>