Java代碼實現擷取安卓手機指定App的流量、CPU和記憶體佔用資訊__Java

來源:互聯網
上載者:User

原理:安卓手機的系統效能資訊都寫在系統的檔案中,通過安卓SDK源生命令來擷取這些資訊。

import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.InputStreamReader;import java.text.DecimalFormat;public class AndroidMonitor {    private static String PID(String PackageName) {        Process proc = null;        String str3 = null;        try {            Runtime runtime = Runtime.getRuntime();            proc = runtime.exec("adb shell ps |grep  " + PackageName);            if (proc.waitFor() != 0) {                System.err.println("exit value = " + proc.exitValue());            }            BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));            StringBuffer stringBuffer = new StringBuffer();            String line = null;            while ((line = in.readLine()) != null) {                stringBuffer.append(line + " ");            }            String str1 = stringBuffer.toString();            String str2 = str1.substring(str1.indexOf(" " + PackageName) - 46, str1.indexOf(" " + PackageName));            String PID = str2.substring(0, 7);            PID = PID.trim();            str3 = PID;        } catch (Exception e) {            System.err.println(e);        } finally {            try {                proc.destroy();            } catch (Exception e2) {            }        }        return str3;    }    public static double getFlow(String PackageName) {        double flow = 0;        try {            String Pid = PID(PackageName);            Runtime runtime = Runtime.getRuntime();            Process proc = runtime.exec("adb shell cat /proc/" + Pid + "/net/dev");            try {                if (proc.waitFor() != 0) {                    System.err.println("exit value = " + proc.exitValue());                }                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));                StringBuffer stringBuffer = new StringBuffer();                String line = null;                while ((line = in.readLine()) != null) {                    stringBuffer.append(line + " ");                }                String str1 = stringBuffer.toString();                String str2 = str1.substring(str1.indexOf("wlan0:"), str1.indexOf("wlan0:") + 90);                String str4 = str2.substring(7, 16);                str4 = str4.trim();                String str6 = str2.substring(67, 75);                str6 = str6.trim();                int b = Integer.parseInt(str4);                int a = Integer.parseInt(str6);                double sendFlow = a / 1024;                double revFlow = b / 1024;                flow = sendFlow + revFlow;            } catch (InterruptedException e) {                System.err.println(e);            } finally {                try {                    proc.destroy();                } catch (Exception e2) {                }            }        } catch (Exception StringIndexOutOfBoundsException) {            System.out.println("請檢查裝置是否串連");        }        return flow;    }    public static double getCPU(String PackageName) {        double Cpu = 0;        try {            Runtime runtime = Runtime.getRuntime();            Process proc = runtime.exec("adb shell top -n 1| grep " + PackageName);            try {                if (proc.waitFor() != 0) {                    System.err.println("exit value = " + proc.exitValue());                }                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));                StringBuffer stringBuffer = new StringBuffer();                String line = null;                while ((line = in.readLine()) != null) {                    stringBuffer.append(line + " ");                }                String str1 = stringBuffer.toString();                String str3 = str1.substring(str1.indexOf(PackageName) - 43, str1.indexOf(PackageName)).trim();                String cpu = str3.substring(0, 2);                cpu = cpu.trim();                Cpu = Double.parseDouble(cpu);            } catch (InterruptedException e) {                System.err.println(e);            } finally {                try {                    proc.destroy();                } catch (Exception e2) {                }            }        } catch (Exception StringIndexOutOfBoundsException) {            System.out.println("請檢查裝置是否串連");        }        return Cpu;    }    public static double getMemory(String PackageName) {        double Heap = 0;        try {            Runtime runtime = Runtime.getRuntime();            Process proc = runtime.exec("adb shell dumpsys meminfo " + PackageName);            try {                if (proc.waitFor() != 0) {                    System.err.println("exit value = " + proc.exitValue());                }                BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));                StringBuffer stringBuffer = new StringBuffer();                String line = null;                while ((line = in.readLine()) != null) {                    stringBuffer.append(line + " ");                }                String str1 = stringBuffer.toString();                String str2 = str1.substring(str1.indexOf("Objects") - 60, str1.indexOf("Objects"));                String str3 = str2.substring(0, 10);                str3 = str3.trim();                Heap = Double.parseDouble(str3) / 1024;                DecimalFormat df = new DecimalFormat("#.000");                String memory = df.format(Heap);                Heap = Double.parseDouble(memory);            } catch (InterruptedException e) {                System.err.println(e);            } finally {                try {                    proc.destroy();                } catch (Exception e2) {                }            }        }        catch (Exception StringIndexOutOfBoundsException) {            System.out.print("請檢查裝置是否串連");        }        return Heap;    }}

CPU和記憶體都可以直接調用這個方法來擷取,至於流量,可以在操作前後加入流量的方法,用兩次的值相減,即可擷取該操作所消耗的流量。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.