Android top指令,Runtime.getRuntime()获取CPU/GPU/内存等信息

-- top指令
String topCmdOld = "top -d 1 -n 60 -m 10 -s rss";
String topCmdNew = "top -d 1 -n 60 -s 6";

-- Android Runtime.getRuntime().exec 使用方法- https://blog.csdn.net/dodod2012/article/details/81100321
Android通过Runtime.getRuntime().exec实现Ping和Traceroute命令时readLine阻塞问题解决- https://blog.csdn.net/dodod2012/article/details/81100162
Process.getInputStream()阻塞问题- https://blog.csdn.net/yuanzihui/article/details/51093375
    int rs = 0;
        Thread outputThread = new ProcessOutputThread(this.p.getInputStream());
        Thread errorOutputThread = new ProcessOutputThread(this.p.getErrorStream());
        outputThread.start();
        errorOutputThread.start();
        rs = Process.waitFor();
        outputThread.join();
        errorOutputThread.join();
        this.outputList = outputThread.getOutputList();
        this.errorOutputList = errorOutputThread.getOutputList();
        return rs;

public void doCmdWithResultProcessing(String cmd) {
        LogUtil.d("lmx", "cmd=" +cmd);
        Process proc = null;
        int exitVal = 1;

        InputStreamReader in = null;
        BufferedReader br = null;
        String line = null;
        try {
            proc = Runtime.getRuntime().exec(cmd);

            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(), "ERROR");
            errorGobbler.start();
//            StreamGobbler stdout = new StreamGobbler(proc.getInputStream(), "STDOUT");
//            stdout.start();

            exitVal = proc.waitFor();
            errorGobbler.join();

            LogUtil.d("lmx", "111 CMD success...");
            in = new InputStreamReader(proc.getInputStream());
            br = new BufferedReader(in);

//            exitVal = proc.waitFor();
            LogUtil.d("lmx", "222 CMD success...");
        } catch (InterruptedException e) {
            LogUtil.d("lmx", "CMD fail...");
            e.printStackTrace();
        } catch (IOException e) {
            LogUtil.d("lmx", "CMD fail...");
            e.printStackTrace();
        }

        try {
            LogUtil.d("lmx", "line, try");
            LogUtil.d("lmx", "br="+br);
            while ((line = br.readLine()) != null) {
                if (line.contains("remote")) {
                    continue;
                } else if (line.contains("carlife")|| line.contains("com.baidu.carli")) {
                    Log.e("lmx","line="+line);
                    TopDataProcessing.getInstance().topDataProc(line);
                } else {
                    continue;
                }

            }
        } catch (IOException e) {
            LogUtil.d("lmx", "line, IOException");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

// 资源释放掉了。
Process pp;
if (pp != null) {
    pp.exitValue();
    pp.destroy();
}
Runtime.getRuntime().exec("su")

-- Android自动化测试-自动获取cpu和内存信息- https://www.bbsmax.com/A/Gkz1bnrJR6/
 Runtime.getRuntime().exec()只能执行权限以内的指令。
 手机root后,且应用申请root权限后才能执行所有指令。

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
class CpuInfo{
    public static String getcpu(String packageName) throws Exception{
        String str = null;
 
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("adb shell dumpsys cpuinfo | grep " + packageName);
 
            try{
                //如果执行时非正常终止,则打印进程退出的返回值,waitFor()=0 为正常终止.
                //waitFor()方法将导致当前的线程等待.如果必要的话.直到由该Process对象表示的进程已经终止.此方法将立即返回,如果子进程已经终止.
                //如果子进程尚未终止,则调用线程将被阻塞,直到子进程退出.
                if(proc.waitFor() != 0){
                    System.err.println("exit value = " + proc.exitValue());
                }
 
                //创建一个BufferedReader对象,且里边装的内容为执行proc返回的值(将proc的返回值作为输入流)
                BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
 
                //创建一个空StringBuffer对象,用来装输出内容
                StringBuffer sr = new StringBuffer();
                String line = null;
 
                //逐行读取返回输入流内容并添加到stringbuffer对象中,每次添加都进行换行.
                while((line = br.readLine()) != null){
                    sr.append(line + "\n");
                }
 
                String str1 = sr.toString();
                System.out.println(str1);
                /*
                String str2 = str1.substring(str1.indexOf(packageName),str1.indexOf(packageName) + 28);
                str = str2.substring(18,23);*/
 
                FileWriter fw = new FileWriter("d:\\cpuinfo.txt",true);
                fw.flush();
                fw.write(str1);
                //fw.write("==========================" + "\n");
                fw.close();
 
            }catch(InterruptedException e){
                System.out.println(e);
            }finally{
                try{
                    proc.destroy();
                }catch(Exception e2){
                    //System.out.println(e2);
                }
            }
        } catch (Exception StringIndexOutOfBoundsExcepiton) {
            // TODO Auto-generated catch block
            System.out.println("请检查设备是否连接");
        }            
 
        return str;
    }
}

import java.io.*;
import java.lang.StringBuffer ;
 
class MemInfo{
    public static String getMemory(String packageName) throws IOException, InterruptedException{
        String str = null;
 
        try {
            Runtime runtime = Runtime.getRuntime();
            Process proc = runtime.exec("adb shell dumpsys meminfo | grep " + packageName);
 
            try{
                if(proc.waitFor() != 0){
                    System.err.println("exit value = " + proc.exitValue());
                }
                BufferedReader br = new BufferedReader(new InputStreamReader(proc.getInputStream()));
 
                StringBuffer sf = new StringBuffer();
 
                String line = null;
                while((line = br.readLine()) != null){
                    sf.append(line + "\n");
                }
 
                String str1 = sf.toString();
                System.out.println(str1);
                /*
                String str2 = str1.substring(str1.indexOf("Objects")-60,str1.indexOf("Objects"));
                str = str2.substring(0,10);
                str.trim();*/
 
                FileWriter fw = new FileWriter("d:\\meminfo.txt",true);
                fw.flush();
                fw.write(str1);
                //fw.write("==========================" + "\n");
                fw.close();
 
            }catch(InterruptedException e){
                System.out.println(e);
            }finally{
                try{
                    proc.destroy();
                }catch(Exception e2){
                    System.out.println(e2);
                }
            }
        } catch (Exception StringIndexOutOfBoundsExcepiton) {
            // TODO Auto-generated catch block
            System.out.println("请检查设备是否连接");
        }
 
        return str;
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值