java 打印手机的设备信息 内存 cpu gpu 信息
时间: 2024-02-20 14:02:09 浏览: 25
可以使用以下代码获取Android设备的设备信息,包括内存、CPU和GPU信息:
```java
import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ConfigurationInfo;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.opengl.GLSurfaceView;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class DeviceInfoActivity extends AppCompatActivity {
private static final String TAG = "DeviceInfoActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_device_info);
// 获取设备信息
getDeviceInfo();
}
private void getDeviceInfo() {
// 获取应用上下文
Context context = getApplicationContext();
// 获取ActivityManager
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
// 获取内存信息
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalMemory = memoryInfo.totalMem / 1024 / 1024;
long availableMemory = memoryInfo.availMem / 1024 / 1024;
Log.d(TAG, "Total Memory: " + totalMemory + "MB" + " Available Memory: " + availableMemory + "MB");
// 获取CPU信息
try {
FileReader fileReader = new FileReader("/proc/cpuinfo");
BufferedReader bufferedReader = new BufferedReader(fileReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
Log.d(TAG, line);
}
bufferedReader.close();
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
// 获取GPU信息
ConfigurationInfo configurationInfo = activityManager.getDeviceConfigurationInfo();
String glVersion = configurationInfo.getGlEsVersion();
Log.d(TAG, "OpenGL ES Version: " + glVersion);
}
}
```
需要注意的是,获取CPU信息的代码需要在应用的 `AndroidManifest.xml` 文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
```
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)