如何检测Android设备是低端还是高端(如moto e,moto g或nexus 5x)

时间:2016-09-16 10:37:07

标签: android performance android-memory

我想检测Android设备硬件是否足够能够顺利运行应用程序的某些功能。

到目前为止我的实施,

检查

  • 可用内存百分比。
  • CPU使用率。
  • 最大CPU频率MHz。

结果: * Nexus 5: CPU使用率=> 9.243698 记忆百分比可用=> 33.6358% 最大CPU频率MHz => 1440

  • Nexus 5x: CPU使用率=> 12.2XXX 记忆百分比可用=> 47.XXXXX%

  • Moto G2: CPU使用率=> 44.XXXX 记忆百分比可用=> 10.XXXX%

我在Nexus 5x中的流程非常棒,但在Nexus 5,moto g和moto中却没有。

//onCreate

    Log.d(LOGTAG, "Cpu Info" + Utility.getCpuInfo());

    Log.d(LOGTAG, "Memory Info" + Utility.getMemoryInfo());

    Log.d(LOGTAG, "Memory Percent Avail" + Utility.getMemoryPercentAvail(getContext()));

    Log.d(LOGTAG, "CPU Usage" + Utility.readCPUUsage());

    Log.d(LOGTAG, "Max CPU Freq MHz" + Utility.getMaxCPUFreqMHz());

// Get Memory Percent Avail

 public static double getMemoryPercentAvail(Context context) {

    ActivityManager.MemoryInfo mi = new ActivityManager.MemoryInfo();
    ActivityManager activityManager = (ActivityManager)
            context.getSystemService(Context.ACTIVITY_SERVICE);
    activityManager.getMemoryInfo(mi);
    double availableMegs = mi.availMem / 1048576L;

    //Percentage can be calculated for API 16+
    double memoryAvail = (double) mi.availMem / (double) mi.totalMem;
    return memoryAvail * 100;
}

// Read CPU Usage

public static float readCPUUsage() {

    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))) * 100;

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

    return 0;
}

// Get Max CPU Freq In MHz

public static int getMaxCPUFreqMHz() {

    int maxFreq = -1;
    try {

        RandomAccessFile reader = new RandomAccessFile( "/sys/devices/system/cpu/cpu0/cpufreq/stats/time_in_state", "r" );

        boolean done = false;
        while ( ! done ) {
            String line = reader.readLine();
            if ( null == line ) {
                done = true;
                break;
            }
            String[] splits = line.split( "\\s+" );
            assert ( splits.length == 2 );
            int timeInState = Integer.parseInt( splits[1] );
            if ( timeInState > 0 ) {
                int freq = Integer.parseInt( splits[0] ) / 1000;
                if ( freq > maxFreq ) {
                    maxFreq = freq;
                }
            }
        }

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

    return maxFreq;
}

0 个答案:

没有答案