Android top instruction, Runtime.getRuntime () Get CPU / GPU / memory information

-- 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 use - https://blog.csdn.net/dodod2012/article/details/81100321
Android by Runtime.getRuntime () exec achieve readLine blocking problem is solved command Ping and Traceroute. - https://blog.csdn.net/dodod2012/article/details/81100162
the Process.getInputStream () blocking problem - https://blog.csdn.net/yuanzihui/article/details/51093375
    int RS = 0;
        the Thread outputThread = ProcessOutputThread new new (this.p.getInputStream ());
        the Thread errorOutputThread = new 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();
        }

    }

// resources freed up.
PP Process;
IF (PP = null!) {
    Pp.exitValue ();
    pp.destroy ();
}
. Runtime.getRuntime () Exec ( "SU")

- Android test automation - automatically get cpu and memory information - https://www.bbsmax.com/A/Gkz1bnrJR6/
 . Runtime.getRuntime () Exec () can only execute instructions within the authority.
 After the phone root, and application after application root privileges to execute all instructions.

java.io.BufferedReader Import;
Import java.io.FileWriter;
Import the java.io.InputStreamReader;
class cpuinfo {
    public static String getcpu (String the packageName) throws Exception {
        String STR = null;
 
        the try {
            the Runtime Runtime = Runtime.getRuntime () ;
            process proc = Runtime.exec ( "adb shell dumpsys cpuinfo | grep" + packageName);
 
            the try {
                // if abnormal termination is executed, the return value of the print process exits, waitFor () = 0 normal termination.
                // waitFor () method causes the current thread to wait, if necessary. process until represented by this process object has terminated. this method will return immediately if the child process has been terminated.
                // if the child process has not been terminated, the calling thread is blocked until the child process exits.
                IF (proc.waitFor ()! = 0) {
                    System.err.println ( "Exit value =" + proc.exitValue ());
                }
 
                // Create a BufferedReader objects, and the contents inside the package proc execution value returned (return value as the proc input stream)
                BufferedReader br the BufferedReader new new = (the InputStreamReader new new (proc.getInputStream ()));
 
                // Create an empty StringBuffer object, means for outputting content
                StringBuffer StringBuffer new new SR = ();
                String Line = null;
 
                // returns an input stream read line by line content and added to stringbuffer object, each line feed additions.
                the 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);
                }
            }
        } The catch (Exception StringIndexOutOfBoundsExcepiton) {
            // the TODO Auto-Generated Block the catch
            System.out.println ( "check device is connected");
        }            
 
        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;
    }
}

Guess you like

Origin blog.csdn.net/ShareUs/article/details/90525558