'Android studio hardware and network resource tracking

I am trying to create a android app in java which will be able to track some hardware components, specifically their usage in real time like CPU usage, RAM usage, and whatever else is available to track. I would also like the app to track network usage.

Currently I'm looking everywhere to try and find a good way to get any form of data from the components, but can't find anything that I could actually use or make sense of.

If anyone knows of any open source projects or libraries which offer this kind of functionality or has done anything wimilar that you are willing to share I would be very grateful.

My attempts at monitoring the cpu:

#1 - doesn't work

try
    {
        final TextView textViewToChange = (TextView) findViewById(R.id.cpu);
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();
        while (load != null)
        {
            textViewToChange.append(load);
            //Log.d("CPU", "CPU usage: " + load);
            load = reader.readLine();
        }
    }
    catch (IOException ex)
    {
        ex.printStackTrace();
    }

#2 - gives me the "frequency" but I'm running a quad core and it's displaying only single frequency value and the frequency seems way off so i'd say not working either

try {
        double currentFreq;
        RandomAccessFile readerCurFreq;
        readerCurFreq = new RandomAccessFile("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r");
        String cur_freq = readerCurFreq.readLine();
        currentFreq = Double.parseDouble(cur_freq) / 1000;
        readerCurFreq.close();
        String final_freq = "Curent Frequency of Core 0 is: " + currentFreq;

        final TextView textViewToChange = (TextView) findViewById(R.id.cpu);
        textViewToChange.setText(final_freq);
    } catch (IOException ex) {
        String final_freq = "Core is Idle";
        final TextView textViewToChange = (TextView) findViewById(R.id.cpu);
        textViewToChange.setText(final_freq);
        ex.printStackTrace();
    }

#3 - not working

try {
        RandomAccessFile reader = new RandomAccessFile("/proc/stat", "r");
        String load = reader.readLine();

        String[] toks = load.split(" +");  // Split on one or more spaces

        long idle1 = Long.parseLong(toks[4]);
        long cpu1 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])
                + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        try {
            Thread.sleep(360);
        } catch (Exception e) {}

        reader.seek(0);
        load = reader.readLine();
        reader.close();

        toks = load.split(" +");

        long idle2 = Long.parseLong(toks[4]);
        long cpu2 = Long.parseLong(toks[2]) + Long.parseLong(toks[3]) + Long.parseLong(toks[5])
                + Long.parseLong(toks[6]) + Long.parseLong(toks[7]) + Long.parseLong(toks[8]);

        float final_freq = (float)(cpu2 - cpu1) / ((cpu2 + idle2) - (cpu1 + idle1));
        final TextView textViewToChange = (TextView) findViewById(R.id.cpu);
        textViewToChange.setText(String.valueOf(final_freq));

    } catch (IOException ex) {
        ex.printStackTrace();
    }

#4 - not working

MBeanServerConnection mbsc = ManagementFactory.getPlatformMBeanServer();

    OperatingSystemMXBean osMBean = ManagementFactory.newPlatformMXBeanProxy(
            mbsc, ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME, OperatingSystemMXBean.class);

    long nanoBefore = System.nanoTime();
    long cpuBefore = osMBean.getProcessCpuTime();

    // Call an expensive task, or sleep if you are monitoring a remote process

    long cpuAfter = osMBean.getProcessCpuTime();
    long nanoAfter = System.nanoTime();

    long percent;
    if (nanoAfter > nanoBefore)
        percent = ((cpuAfter-cpuBefore)*100L)/
                (nanoAfter-nanoBefore);
    else percent = 0;

    string result = "Cpu usage: " + percent + "%";

    final TextView textViewToChange = (TextView) findViewById(R.id.cpu);
    textViewToChange.setText(String.valueOf(result));


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source