通过反射和进程读取两种方式获取:

MIUI:ro.miui.ui.version.name

EMUI:ro.build.version.emui

反射

public static String getSystemProperty(String propName) {

Class> classType = null;

String buildVersion = null;

try {

classType = Class.forName("android.os.SystemProperties");

Method getMethod = classType.getDeclaredMethod("get", new Class>[]{String.class});

buildVersion = (String) getMethod.invoke(classType, new Object[]{propName});

} catch (Exception e) {

e.printStackTrace();

}

return buildVersion;

}

读取进程

public static String getSystemProperty(String propName){

String line;

BufferedReader input = null;

try

{

Process p = Runtime.getRuntime().exec("getprop " + propName);

input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024);

line = input.readLine();

input.close();

}

catch (IOException ex)

{

Log.e("lth", "Unable to read sysprop " + propName, ex);

return null;

}

finally

{

if(input != null)

{

try

{

input.close();

}

catch (IOException e)

{

Log.e("lth", "Exception while closing InputStream", e);

}

}

}

return line;

}

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐