java 以编程方式查找 Android 系统信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10219717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 00:05:45  来源:igfitidea点击:

Programmatically find Android system info

javaandroid

提问by Chris Ridmann

I am trying to programatically find the system info for Android devices, specifically:

我正在尝试以编程方式查找 Android 设备的系统信息,特别是:

  • RAM
  • CPU speed
  • # cores, architecture, etc.
  • 内存
  • CPU速度
  • # 内核、架构等。

Are there any Android classes that specify this information. I have been using the android.board library, but it doesn't seem to have everything that I want.

是否有任何指定此信息的 Android 类。我一直在使用 android.board 库,但它似乎没有我想要的一切。

回答by Shankar Agarwal

Let me tell you what I did, So others who visit this thread can come to know the steps:

让我告诉你我做了什么,所以访问这个线程的其他人可以知道这些步骤:

1) parse /proc/meminfo command. You can find reference code here: Get Memory Usage in Android2) use below code and get current RAM:

1) 解析 /proc/meminfo 命令。您可以在此处找到参考代码: Get Memory Usage in Android2) 使用以下代码并获取当前 RAM:

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

Note: please note that - we need to calculate total memory only once. so call point 1 only once in your code and then after, you can call code of point 2 repetitively.

注意:请注意——我们只需要计算一次总内存。所以在你的代码中只调用点 1 一次,然后你可以重复调用点 2 的代码。

回答by slayton

You can get most of the information you want from the /proc/cpuinfofile. Here is a tutorial on how to load and parse that file: http://www.roman10.net/how-to-get-cpu-information-on-android/

您可以从/proc/cpuinfo文件中获取大部分所需信息。这是有关如何加载和解析该文件的教程:http: //www.roman10.net/how-to-get-cpu-information-on-android/

Additionally the information about the RAM can be obtained from the /proc/meminfofile

此外,可以从/proc/meminfo文件中获取有关 RAM 的信息

回答by Amit Pathak

Here is the code snippet to get the current device RAM size.

这是获取当前设备 RAM 大小的代码片段。

ActivityManager actManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
MemoryInfo memInfo = new ActivityManager.MemoryInfo();
actManager.getMemoryInfo(memInfo);
long totalMemory = memInfo.totalMem;

回答by Dan

Agarwal's answer was very helpful. Had to modify it a bit since I'm not calculating free memory in an activity, but passing in an application context to a system utilities file:

阿加瓦尔的回答非常有帮助。不得不稍微修改一下,因为我不是在计算活动中的可用内存,而是将应用程序上下文传递给系统实用程序文件:

From the main activity:

从主要活动:

public class MyActivity extends Activity {
    ...
    public void onCreate(Bundle savedInstanceState) {
        ...
        MySystemUtils systemUtils = new MySystemUtils(this); // initializations
        ...
     }
}

In the SystemUtils file:

在 SystemUtils 文件中:

   MySystemUtils (Context appContext) { // called once from within onCreate
        MemoryInfo mi = new MemoryInfo();
        ActivityManager activityManager = ActivityManager)appContext.getSystemService(Activity.ACTIVITY_SERVICE);
        activityManager.getMemoryInfo(mi);
        long availableMegs = mi.availMem / 1048576L;
    }