'Determine Android device performance programmatically
I want to run different lines of code for android devices with different performance. For example something like this:
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
}
Or at least:
if (isHighPerformanceDevice()) {
// run code for devices with high performance
} else {
// run code for devices with low performance
}
I wonder if there is something in Android SDK
I can use or those methods should be implemented manually? I would appreciate any guidance on this, thank you.
Devices with Android 10 and higher.
Solution 1:[1]
I'd prefer you to get the drive performance using its RAM.
To get RAM values simply do:
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);
Here you will get Total as well as Free and Used RAM size. The value of these will be in "long", so format it to human-readable (i.e in MB/GB). Use the following method to do so:
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";
}
Now you can customise this code to your needs. I can't say that 1 Pb RAM is hight performance or low performance in your case. It depends on what your app does. That is the thing u need to do
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sambhav. K |