Android--Get CPU usage

Source: Internet
Author: User


public static float Getprocesscpurate () {Float totalCpuTime1 = gettotalcputime ();        float processCpuTime1 = Getappcputime ();        try {thread.sleep (360);        } catch (Exception e) {} float totalCpuTime2 = Gettotalcputime ();        float processCpuTime2 = Getappcputime ();        float cpurate = (processcputime2-processcputime1)/(TOTALCPUTIME2-TOTALCPUTIME1);    return cpurate;        } public static Long Gettotalcputime () {//Get total system CPU usage time string[] Cpuinfos = null; try {BufferedReader reader = new BufferedReader (new InputStreamReader (New FileInput            Stream ("/proc/stat")), 1000);            String load = Reader.readline ();            Reader.close ();        Cpuinfos = Load.split ("");        } catch (IOException ex) {ex.printstacktrace ();           } Long totalcpu = Long.parselong (cpuinfos[2])     + Long.parselong (cpuinfos[3]) + Long.parselong (cpuinfos[4]) + Long.parselong (cpuinfos[6]) + long.parse        Long (cpuinfos[5]) + Long.parselong (cpuinfos[7]) + Long.parselong (cpuinfos[8]);    return totalcpu;        } public static Long Getappcputime () {//Gets the CPU time occupied by the app string[] Cpuinfos = null;            try {int pid = Android.os.Process.myPid (); BufferedReader reader = new BufferedReader (new InputStreamReader (New FileInputStream ("/proc/" + pid +            "/stat")), 1000);            String load = Reader.readline ();            Reader.close ();        Cpuinfos = Load.split ("");        } catch (IOException ex) {ex.printstacktrace (); } Long Appcputime = Long.parselong (cpuinfos[13]) + Long.parselong (cpuinfos[14]) + Long.parselong (CPU        INFOS[15]) + Long.parselong (cpuinfos[16]);    return appcputime; }


Android--Get CPU usage

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.