繁体   English   中英

以编程方式确定 Android 设备性能

[英]Determine Android device performance programmatically

我想为具有不同性能的 android 设备运行不同的代码行。 例如这样的事情:

if (isHighPerformanceDevice()) {
    // run code for devices with high performance
} else if (isMediumPerformanceDevice()) {
    // run code for devices with medium performance
} else {
    // run code for devices with low performance
}

或者至少:

if (isHighPerformanceDevice()) {
    // run code for devices with high performance
} else {
    // run code for devices with low performance
}

不知道Android SDK里面有没有什么可以用的,或者那些方法需要手动实现? 我将不胜感激任何指导,谢谢。

具有Android 10及更高版本的设备。

我希望您使用其 RAM 获得驱动器性能。

要获取 RAM 值,只需执行以下操作:

ActivityManager actManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    ActivityManager.MemoryInfo memInfo = new ActivityManager.MemoryInfo();
    assert actManager != null;
    actManager.getMemoryInfo(memInfo);
    long totalMemory = memInfo.totalMem;
    long availMemory = memInfo.availMem;
    long usedMemory = totalMemory - availMemory;
    float precentlong = (((float) (availMemory / totalMemory)) * 100);

在这里您将获得 Total 以及 Free 和 Used RAM 大小。 这些值将是“long”,因此将其格式化为人类可读的(即以 MB/GB 为单位)。 使用以下方法执行此操作:

 private String floatForm(double d) {
    return String.format(java.util.Locale.US, "%.2f", d);
}

private String bytesToHuman(long size) {
    long Kb = 1024;
    long Mb = Kb * 1024;
    long Gb = Mb * 1024;
    long Tb = Gb * 1024;
    long Pb = Tb * 1024;
    long Eb = Pb * 1024;

    if (size < Kb) return floatForm(size) + " byte";
    if (size >= Kb && size < Mb) return floatForm((double) size / Kb) + " KB";
    if (size >= Mb && size < Gb) return floatForm((double) size / Mb) + " MB";
    if (size >= Gb && size < Tb) return floatForm((double) size / Gb) + " GB";
    if (size >= Tb && size < Pb) return floatForm((double) size / Tb) + " TB";
    if (size >= Pb && size < Eb) return floatForm((double) size / Pb) + " Pb";
    if (size >= Eb) return floatForm((double) size / Eb) + " Eb";

    return "0";
}

现在您可以根据需要自定义此代码。 我不能说 1 Pb RAM 在您的情况下是高性能还是低性能。 这取决于你的应用程序做什么。 那是你需要做的事

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM