走安卓开发不可避免的遇到很多机型,并且去适配。下面分享两个工具类,可以直接识别机型:
**
* Created with IntelliJ IDEA.
* **********************************
* User: skura_l
* Date: 2016年 01月 20日
*@QQ: 1234567890
* **********************************
*/
public classAndtoidRomUtil {
private static finalStringKEY_EMUI_VERSION_CODE="ro.build.version.emui";
private static finalStringKEY_MIUI_VERSION_CODE="ro.miui.ui.version.code";
private static finalStringKEY_MIUI_VERSION_NAME="ro.miui.ui.version.name";
private static finalStringKEY_MIUI_INTERNAL_STORAGE="ro.miui.internal.storage";
/**
* 华为rom
*
*@return
*/
public static booleanisEMUI() {
try{
finalBuildProperties prop = BuildProperties.newInstance();
returnprop.getProperty(KEY_EMUI_VERSION_CODE, null) !=null;
}catch(finalIOException e) {
return false;
}
}
/**
* 小米rom
*
*@return
*/
public static booleanisMIUI() {
try{
final String type = android.os.Build.MODEL;
final Build Properties prop = Build Properties.newInstance();
/*String rom = "" + prop.getProperty(KEY_MIUI_VERSION_CODE, null) + prop.getProperty(KEY_MIUI_VERSION_NAME, null)+prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null);
Log.d("Android_Rom", rom);*/
returnprop.getProperty(KEY_MIUI_VERSION_CODE, null) !=null
|| prop.getProperty(KEY_MIUI_VERSION_NAME, null) !=null
|| prop.getProperty(KEY_MIUI_INTERNAL_STORAGE, null) !=null
|| type.length() >"MI".length() && type.substring(0,"MI".length()).equals("MI");
}catch(finalIOException e) {
return false;
}
}
/**
* 魅族rom
*
*@return
*/
public static booleanisFlyme() {
try{
finalMethod method = Build.class.getMethod("hasSmartBar");
returnmethod !=null;
}catch(finalException e) {
return false;
}
}
}
/////////////////
public classBuildProperties {
private finalPropertiesproperties;
privateBuildProperties()throwsIOException {
properties=newProperties();
properties.load(newFileInputStream(newFile(Environment.getRootDirectory(),"build.prop")));
}
public booleancontainsKey(finalObject key) {
returnproperties.containsKey(key);
}
public booleancontainsValue(finalObject value) {
returnproperties.containsValue(value);
}
publicSet>entrySet() {
returnproperties.entrySet();
}
publicStringgetProperty(finalString name) {
returnproperties.getProperty(name);
}
publicStringgetProperty(finalString name, finalString defaultValue) {
returnproperties.getProperty(name,defaultValue);
}
public booleanisEmpty() {
returnproperties.isEmpty();
}
publicEnumerationkeys() {
returnproperties.keys();
}
publicSetkeySet() {
returnproperties.keySet();
}
public intsize() {
returnproperties.size();
}
publicCollectionvalues() {
returnproperties.values();
}
public staticBuildPropertiesnewInstance()throwsIOException {
return newBuildProperties();
}
}
注意: 其实只要这样就可以了String type = android.os.Build.MODEL; 这个返回的东西就可以区别手机,打印出来看就行了。 简书没用过多久,完全不会排版,而且直接copy过来空格会消失。。。
网友评论