package com.xzy.deviceinfo.util;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Point;
import android.hardware.Sensor;
import android.hardware.SensorManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;
import androidx.annotation.RequiresPermission;
import java.io.*;
import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import static android.Manifest.permission.ACCESS_WIFI_STATE;
import static android.Manifest.permission.INTERNET;
import static android.content.Context.ACTIVITY_SERVICE;
import static android.content.Context.WIFI_SERVICE;
/**
* 系统相关的工具类。另外可以参考 RomUtils.java
*
* @author xzy
*/
@SuppressWarnings("all")
public class SystemUtils {
private static final String LINE_SEP = System.getProperty("line.separator");
private static final String SYS_EMUI = "sys_emui";
private static final String SYS_MIUI = "sys_miui";
private static final String SYS_FLYME = "sys_flyme";
private static final String KEY_MIUI_VERSION_CODE = "ro.miui.ui.version.code";
private static final String KEY_MIUI_VERSION_NAME = "ro.miui.ui.version.name";
private static final String KEY_MIUI_INTERNAL_STORAGE = "ro.miui.internal.storage";
private static final String KEY_EMUI_API_LEVEL = "ro.build.hw_emui_api_level";
private static final String KEY_EMUI_VERSION = "ro.build.version.emui";
private static final String KEY_EMUI_CONFIG_HW_SYS_VERSION = "ro.confg.hw_systemversion";
public static String getSystem() {
String system = "otherSystem";
try {
Properties prop = new Properties();
prop.load(new FileInputStream(new File(Environment.getRootDirectory(),
"build.prop")));
if (prop.getProperty(KEY_MIUI_VERSION_CODE, null) != null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) != null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) != null) {
system = SYS_MIUI;//小米
} else if (prop.getProperty(KEY_EMUI_API_LEVEL, null) != null
|| prop.getProperty(KEY_EMUI_VERSION, null) != null
|| prop.getProperty(KEY_EMUI_CONFIG_HW_SYS_VERSION, null) != null) {
system = SYS_EMUI;//华为
} else if (getMeizuFlymeOSFlag().toLowerCase().contains("flyme")) {
system = SYS_FLYME;//魅族
}
} catch (IOException e) {
e.printStackTrace();
return system;
}
return system;
}
public static String getMeizuFlymeOSFlag() {
return getSystemProperty();
}
/**
* @return 系统属性
*/
private static String getSystemProperty() {
try {
@SuppressLint("PrivateApi")
Class<?> clz = Class.forName("android.os.SystemProperties");
Method get = clz.getMethod("get", String.class, String.class);
return (String) get.invoke(clz, "ro.build.display.id", "otherSystem");
} catch (Exception e) {
Log.e("", e.getMessage());
}
return "otherSystem";
}
/**
* @return cpu 信息
*/
public static String getCpuInfo() {
String cpuCount = "cpu 核心数:" + Runtime.getRuntime().availableProcessors() + "\n";
String cpuFreq = "cpu 频率范围:" + getMinCpuFreq() + "Hz~" + getMaxCpuFreq() + "Hz" + "\n";
String cpuName = "cpu 名称:" + getCpuName() + "\n";
ProcessBuilder processBuilder;
String[] DATA = {"/system/bin/cat", "/proc/cpuinfo"};
InputStream inputStream;
Process process;
byte[] byteArry;
String cpuInfo = cpuCount + cpuFreq + cpuName + "cpu 详细信息如下:" + "\n";
;
byteArry = new byte[1024];
try {
processBuilder = new ProcessBuilder(DATA);
process = processBuilder.start();
inputStream = process.getInputStream();
while (inputStream.read(byteArry) != -1) {
cpuInfo = cpuInfo + new String(byteArry);
}
inputStream.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return cpuInfo + "\n";
}
/**
* 获取手机IMEI(需要“android.permission.READ_PHONE_STATE”权限)
*
* @return 手机IMEI
*/
@SuppressLint("MissingPermission")
public static String getIMEI(Context ctx) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
return "未知,Android 9.0 以后无法获取 IMEI";
}
TelephonyManager tm = (TelephonyManager) ctx.getSystemService(Activity.TELEPHONY_SERVICE);
try {
return tm.getDeviceId() + "\n";
} catch (Exception e) {
return "获取 IMEI 异常";
}
}
/**
* 获取内存信息。
*
* @param context 上下文
* @return 内存信息
*/
public static String getMemoInfo(Context context) {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
ActivityManager activityManager =
(ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
activityManager.getMemoryInfo(memoryInfo);
Runtime runtime = Runtime.getRuntime();
String strMemInfo =
"MemoryInfo.availMem = " +
getFileSizeDescription(memoryInfo.availMem) + "\n"
+ "MemoryInfo.totalMem = " + getFileSizeDescription(memoryInfo.totalMem) + "\n" // API 16
+ "\n"
+ "Runtime.maxMemory() = " + getFileSizeDescription(runtime.maxMemory()) + "\n"
+ "Runtime.totalMemory() = " + getFileSizeDescription(runtime.totalMemory()) + "\n"
+ "Runtime.freeMemory() = " + getFileSizeDescription(runtime.freeMemory()) + "\n";
return strMemInfo;
}
/**
* 获取系统相关的信息。
*
* @return 系统信息。
*/
public static String getSysVersionInfo(Context context) {
String sysVersionInfo = "SERIAL: " + Build.SERIAL + "\n" +
"品牌: " + Build.BRAND + "\n" +
"型号: " + Build.MODEL + "\n" +
"SDK: " + Build.VERSION.SDK + "\n" +
"屏幕分辨率: " + getScreenWidth(context) + "*" + getScreenHeight(context) + "\n" +
"APP 分辨率(去掉状态栏高度和导航栏): " + getAppScreenWidth(context) + "*" + getAppScreenHeight(context) + "\n" +
"DPI: " + getScreenDensityDpi() + "\n" +
"系统版本: Android " + Build.VERSION.RELEASE + "\n";
return sysVersionInfo;
}
/**
* 得到关于文件大小的描述信息。
*
* @param size 文件大小 long 值
* @return 文件大小的描述信息字符串。
*/
public static String getFileSizeDescription(long size) {
StringBuilder bytes = new StringBuilder();
DecimalFormat format = new DecimalFormat("###.0");
if (size >= 1024 * 1024 * 1024) {
double i = (size / (1024.0 * 1024.0 * 1024.0));
bytes.append(format.format(i)).append("GB");
} else if (size >= 1024 * 1024) {
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
获取 Android 设备的系统信息。包含屏幕信息、内存信息、IMEI、存储、传感器、CPU等信息.zip (40个子文件)
AndroidDeviceInfo-master
gradle.properties 1KB
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 232B
apk
app-release.apk 1.87MB
app
src
androidTest
java
com
xzy
deviceinfo
ExampleInstrumentedTest.kt 624B
test
java
com
xzy
deviceinfo
ExampleUnitTest.kt 343B
main
java
com
xzy
deviceinfo
MainActivity.kt 1KB
util
SystemUtils.java 30KB
res
mipmap-xxhdpi
ic_launcher.jpg 5KB
mipmap-hdpi
ic_launcher.jpg 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
mipmap-anydpi-v26
ic_launcher.xml 270B
ic_launcher_round.xml 270B
mipmap-mdpi
ic_launcher.jpg 5KB
mipmap-xxxhdpi
ic_launcher.jpg 5KB
mipmap-xhdpi
ic_launcher.jpg 5KB
values
colors.xml 208B
strings.xml 75B
styles.xml 383B
layout
activity_main.xml 4KB
drawable
ic_launcher_background.xml 5KB
AndroidManifest.xml 1KB
proguard-rules.pro 751B
build.gradle 1KB
release
output.json 234B
.gitignore 7B
screenshot
show.png 393KB
gradlew.bat 2KB
build.gradle 661B
.idea
codeStyles
codeStyleConfig.xml 142B
Project.xml 3KB
runConfigurations.xml 564B
vcs.xml 180B
misc.xml 357B
inspectionProfiles
Project_Default.xml 1KB
gradle.xml 803B
encodings.xml 135B
settings.gradle 15B
gradlew 5KB
key
xzy.jks 2KB
共 40 条
- 1
资源评论
博士僧小星
- 粉丝: 1720
- 资源: 5850
下载权益
C知道特权
VIP文章
课程特权
开通VIP
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功