/*
* 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();
} }

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

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

    /// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...

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

    //================二进制相关转换类============== #region 将文件转换为二进制数组 /// <summary> /// 将文件转换为二进制数组 /// ...

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

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

  4. vue项目工具文件utils.js javascript常用工具类,javascript常用工具类,util.js

    vue项目工具文件utils.js :https://blog.csdn.net/Ajaxguan/article/details/79924249 javascript常用工具类,util.js : ...

  5. commons-lang3-3.2.jar中的常用工具类的使用

    这个包中的很多工具类可以简化我们的操作,在这里简单的研究其中的几个工具类的使用. 1.StringUtils工具类 可以判断是否是空串,是否为null,默认值设置等操作: /** * StringUt ...

  6. PHP常用工具类

    <?php namespace isslib\Util; use think\Config; /** * 常用工具类 * User: xaxiong * Date: 2016/12/19 * T ...

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

    文章来源:嗨学网 敏而好学论坛www.piaodoo.com 欢迎大家相互学习 文件资源操作 Spring 定义了一个 org.springframework.core.io.Resource 接口, ...

  8. js常用工具类.

    一些js的工具类 复制代码 /** * Created by sevennight on 15-1-31. * js常用工具类 */ /** * 方法作用:[格式化时间] * 使用方法 * 示例: * ...

  9. 封装一个简单好用的打印Log的工具类And快速开发系列 10个常用工具类

    快速开发系列 10个常用工具类 http://blog.csdn.net/lmj623565791/article/details/38965311 ------------------------- ...

随机推荐

  1. Niagara AX之axvelocity的使用

    axvelocity是一个Web模板引擎模块,来源于Apache Velocity.由于我的Niagara AX的许可没有axvelocity,因此,在palette中没有找到axvelocity. ...

  2. shell中的退出状态码

    shell中的退出状态码最大只有255,如果超过这个值,就会进行取余运算,即如果执行如下命令: exit exitCode 如果exitCode大于255,那么实际的状态码为exitCode % 25 ...

  3. ZendFramework2 源码分析 index.php

    <?php /** * This makes our life easier when dealing with paths. Everything is relative * to the a ...

  4. GitHub好站点

    https://github.com/XingCloud/stream_processor

  5. Linux下ld搜索问题:ld: cannot find -l"XX"

    ld命令行工具(链接库的一个工具)的搜索路径是-L指定的,库名是-l指定的. 比如: ld -L[dir] -l[mylib] --verbose 以上我用可视化的方法显示ld的搜索路径,其结果是居然 ...

  6. Why Functional Programming Matters

    http://hi.baidu.com/lhurricane/item/35b57e12a1e3c5ddbf9042a7 http://blog.csdn.net/ddwn/article/detai ...

  7. HTTP协议及HTTP包

    HTTP协议用于在Internet上发送和接收消息.HTTP协议是一种请求-应答式的协议 ——客户端发送一个请求,服务器返回该请求的应答,所有的请求与应答都是HTTP包.HTTP协议使用可靠的TCP连 ...

  8. bzoj1664 [Usaco2006 Open]County Fair Events 参加节日庆祝

    Description Farmer John has returned to the County Fair so he can attend the special events (concert ...

  9. UESTC_菲波拉契数制 2015 UESTC Training for Dynamic Programming<Problem E>

    E - 菲波拉契数制 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submi ...

  10. UVA_埃及分数(Hard Version) UVA 12588

    Problem EEg[y]ptian Fractions (HARD version)Given a fraction a/b, write it as a sum of different Egy ...