Android开发获取设备硬件相关信息

本文汇总了在Android开发中获取设备硬件和网络信息的方法,包括设备型号、CPU信息、内存、摄像头、分辨率、运营商信息等。详细介绍了如何获取手机型号、品牌、制造商、安卓版本、CPU型号、架构、核心数、频率、温度、RAM、ROM、电池容量、摄像头像素、屏幕分辨率、本机号码、运营商名称和IMEI码,为开发者提供便利。
部署运行你感兴趣的模型镜像

在安卓开发过程中,有时需要获取设备硬件信息以及网络信息,包括设备型号、CPU信息、RAM以及ROM信息、摄像头信息、分辨率、运营商信息等。网上逐条搜索相关方法比较繁琐,因此本文总结了相关信息的具体获取方法。本文所有代码能获取的设备信息如下图所示:

首先在manifest.xml添加以下权限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />

下面开始逐一获取设备相关信息分条目添加至某一FragmentPage中的LinearLayout pwTable中。

通过以下代码获取设备型号:

TextView p_model = new TextView(getActivity());
p_model.setTextColor(Color.BLACK);
p_model.setText("手机型号:" + android.os.Build.MODEL);
pwTable.addView(p_model);

通过以下代码获取手机品牌:

TextView p_brand = new TextView(getActivity());
p_brand.setTextColor(Color.BLACK);
p_brand.setText("手机品牌:" + android.os.Build.BRAND);
pwTable.addView(p_brand);

通过以下代码获取手机制造商,此条目信息通常情况与手机品牌一致:

TextView p_company = new TextView(getActivity());
p_company.setTextColor(Color.BLACK);
p_company.setText("制造商:" + android.os.Build.MANUFACTURER);
pwTable.addView(p_company);

通过以下代码获取安卓版本信息:

TextView p_and = new TextView(getActivity());
p_and.setTextColor(Color.BLACK);
p_and.setText("Android版本号:" + android.os.Build.VERSION.RELEASE);
pwTable.addView(p_and);

通过以下代码获取CPU型号信息:

TextView p_cpuname = new TextView(getActivity());
p_cpuname.setTextColor(Color.BLACK);
p_cpuname.setText("CPU型号:" + android.os.Build.HARDWARE);
pwTable.addView(p_cpuname);

通过以下代码获取CPU架构信息:

TextView p_cputype = new TextView(getActivity());
p_cputype.setTextColor(Color.BLACK);
p_cputype.setText("CPU架构:" + android.os.Build.CPU_ABI);
pwTable.addView(p_cputype);

通过以下代码获取CPU核心数信息:

int cores;
try{
	cores = new File("/sys/devices/system/cpu/").listFiles(CPU_FILTER).length;
} catch(SecurityException e){
	cores = 0;
} catch(NullPointerException e){
	cores = 0;
}
TextView p_cpucore = new TextView(getActivity());
p_cpucore.setTextColor(Color.BLACK);
p_cpucore.setText("CPU核心数:" + cores);
pwTable.addView(p_cpucore);

通过以下代码获取CPU最高频率信息:

int maxRate1 = 0;
double temp;
String maxRate = "0";
try {
	FileReader fr1 = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq");
	BufferedReader br1 = new BufferedReader(fr1);
	String text1 = br1.readLine();
	maxRate1 = Integer.parseInt(text1);
	temp = maxRate1 / 1000.0;
	maxRate = String.format("%.1f", temp);
	br1.close();
} catch (IOException e) {
	// TODO 自动生成的 catch 块
}
TextView p_cpurate = new TextView(getActivity());
p_cpurate.setTextColor(Color.BLACK);
p_cpurate.setText("CPU最高频率:" + maxRate + "MHz");
pwTable.addView(p_cpurate);

通过以下代码获取CPU最低频率信息:

int minRate1 = 0;
String minRate = "0";
try {
	FileReader fr2 = new FileReader("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_min_freq");
	BufferedReader br2 = new BufferedReader(fr2);
	String text2 = br2.readLine();
	minRate1 = Integer.parseInt(text2);
	temp = minRate1 / 1000.0;
	minRate = String.format("%.1f", temp);
	br2.close();
} catch (IOException e) {
	// TODO 自动生成的 catch 块
}
TextView p_cpurate2 = new TextView(getActivity());
p_cpurate2.setTextColor(Color.BLACK);
p_cpurate2.setText("CPU最低频率:" + minRate + "MHz");
pwTable.addView(p_cpurate2);

通过以下代码获取CPU温度:

String text4 = "0";
try {
	FileReader fr4 = new FileReader("/sys/class/thermal/thermal_zone9/subsystem/thermal_zone9/temp");
	BufferedReader br4 = new BufferedReader(fr4);
	text4 = br4.readLine();
	br4.close();
} catch (IOException e) {
	// TODO 自动生成的 catch 块
}
TextView p_temp = new TextView(getActivity());
p_temp.setTextColor(Color.BLACK);
p_temp.setText("CPU温度:" + text4 + "℃");
pwTable.addView(p_temp);

通过以下代码获取RAM大小信息:

int memorySize = 0;
String ramSize = "0";
try {
	FileReader fr3 = new FileReader("/proc/meminfo");
	BufferedReader br3 = new BufferedReader(fr3);
	String memoryLine = br3.readLine();
	String subMemoryLine = memoryLine.substring(memoryLine.indexOf("MemTotal:"));
	memorySize = Integer.parseInt(subMemoryLine.replaceAll("\\D+", ""));
	temp = memorySize / 1000000.0;
	ramSize = String.format("%.1f", temp);
	br3.close();
} catch (IOException e) {
	// TODO 自动生成的 catch 块
}
TextView p_ram = new TextView(getActivity());
p_ram.setTextColor(Color.BLACK);
p_ram.setText("RAM大小:" + ramSize + "GB");
pwTable.addView(p_ram);

通过以下代码获取ROM大小信息:

StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long romSize1 = blockSize * totalBlocks;
temp = romSize1 / 1000000000.0;
String romSize = String.format("%.1f", temp);
TextView p_rom = new TextView(getActivity());
p_rom.setTextColor(Color.BLACK);
p_rom.setText("ROM大小:" + romSize + "GB");
pwTable.addView(p_rom);

通过以下代码获取电池容量信息:

PowerProfile pp = new PowerProfile(getActivity());
int batteryRate = (int) pp.getBatteryCapacity();
TextView p_batteryrate = new TextView(getActivity());
p_batteryrate.setTextColor(Color.BLACK);
p_batteryrate.setText("电池容量:" + batteryRate + "mAh");
pwTable.addView(p_batteryrate);

通过以下代码获取前置摄像头像素信息:

TextView p_front = new TextView(getActivity());
p_front.setTextColor(Color.BLACK);
p_front.setText("前置摄像头像素:" + CameraUtils.getCameraPixels(CameraUtils.HasFrontCamera()));
pwTable.addView(p_front);

通过以下代码获取后置摄像头像素信息: 

TextView p_back = new TextView(getActivity());
p_back.setTextColor(Color.BLACK);
p_back.setText("后置摄像头像素:" + CameraUtils.getCameraPixels(CameraUtils.HasBackCamera()));
pwTable.addView(p_back);

通过以下代码获取屏幕分辨率信息(有虚拟按键时会有误差):

WindowManager windowManager = getActivity().getWindowManager();    
Display display = windowManager.getDefaultDisplay();    
int screenWidth = screenWidth = display.getWidth();    
int screenHeight = screenHeight = display.getHeight();
TextView p_screen = new TextView(getActivity());
p_screen.setTextColor(Color.BLACK);
p_screen.setText("屏幕分辨率:" + screenHeight + "×" + screenWidth);
pwTable.addView(p_screen);

通过以下代码获取本机号码信息(双卡手机此方法只能获取卡1号码):

TextView p_num = new TextView(getActivity());
p_num.setTextColor(Color.BLACK);
p_num.setText("本机号码:" + num);
pwTable.addView(p_num);

通过以下代码获取运营商名称(同样此方法只能获取卡1运营商):

TextView p_nm = new TextView(getActivity());
p_nm.setTextColor(Color.BLACK);
p_nm.setText("运营商名称:" + nm);
pwTable.addView(p_nm);

通过以下代码获取IMEI码(同样获取的是卡1的IMEI码):

TextView p_imei = new TextView(getActivity());
p_imei.setTextColor(Color.BLACK);
p_imei.setText("IMEI码:" + imei);
pwTable.addView(p_imei);
通过以下代码获取手机MAC地址:

WifiManager wifiMng = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfor = wifiMng.getConnectionInfo();
TextView p_mac = new TextView(getActivity());
p_mac.setTextColor(Color.BLACK);
p_mac.setText("MAC地址:" + wifiInfor.getMacAddress());
pwTable.addView(p_mac);
具体实际应用请见我的GitHub: https://github.com/HirojiSawatari/Sysmonitor/blob/master/src/hg/crx/sysmonitor/FragmentPage2.java





您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值