SORU
25 HAZİRAN 2010, Cuma


Bellek Kullanımı Android

Hangi android CPU ve Bellek kullanımı alabileceğimiz herhangi bir API var mı?

Aşağıdaki gibi bir kod denedim:

package com.infostretch.mainactivity;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class CPULoad 
{
    long total = 0;
    long idle = 0;

    float usage = 0;

    public CPULoad()
    {
        readUsage();
    }

    public float getUsage()
    {
        readUsage();
        return usage;
    }

    private void readUsage()
    {
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream("/proc/stat")), 1000);
            String load = reader.readLine();
            reader.close();

            String[] toks = load.split(" ");

            long currTotal = Long.parseLong(toks[2])   Long.parseLong(toks[3])   Long.parseLong(toks[4]);
            long currIdle = Long.parseLong(toks[5]);

            this.usage = (currTotal - total) * 100.0f / (currTotal - total   currIdle - idle);
            this.total = currTotal;
            this.idle = currIdle;
        }
        catch(IOException ex)
        {
            ex.printStackTrace();
        }
    }
}

Bunu doğru şekilde yapmak mı?

CEVAP
6 NİSAN 2011, ÇARŞAMBA


Bu fonksiyon cpu kullanımı hesaplamak için kullanıyorum. Size yardımcı olabilir umuyoruz.

private float readUsage() {
    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]);

        return (float)(cpu2 - cpu1) / ((cpu2   idle2) - (cpu1   idle1));

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

    return 0;
} 

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • CodingMadeEasy

    CodingMadeEa

    25 EYLÜL 2010
  • David Wills

    David Wills

    31 Aralık 2007
  • thewinekone

    thewinekone

    17 Aralık 2005