Java code implementation access to Android mobile phone specified app traffic, CPU and memory footprint information __java

Source: Internet
Author: User
Tags readline stringbuffer

Principle: The information about the system performance of the Android phone is written in the file, and is obtained through the Android SDK source life order.

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)-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 ();
    The 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 ("Please check whether the device is connected");
    } 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)-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 ("Please check whether the device is connected");

    } 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")-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 (); The catch (Exception E2) {}} catch (Exception Stringindexoutofboundsexcepti ON) {System.out.print ("Please check if the device is connected");
    return Heap; }
}

CPU and memory can be directly called this method to obtain, as for the flow, you can add the flow before and after the method, two times to subtract the value of the operation can be consumed by the flow.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.