走安卓開發不可避免的遇到很多機型,并且去適配。下面分享兩個工具類,可以直接識別機型:
**
* 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過來空格會消失。。。