RomUtil【Android判断手机ROM,用于判断手机机型】
参考资料
正文
有时候需要判断手机系统的ROM,检测ROM是MIUI、EMUI还是Flyme,可以使用getprop命令,去系统build.prop文件查找是否有对应属性来判断是什么ROM。
build.prop的介绍可以看Android build.prop简介
代码【和参考资料一致,这里只是做个记录,详细介绍,请移步参考资料《Android判断手机ROM》】
package com.hxzk.som.base.utils; import android.os.Build; import android.text.TextUtils; import android.util.Log; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; /** * Created by HaiyuKing * Used 判断手机ROM,检测ROM是MIUI、EMUI还是Flyme * 参考资料:https://www.jianshu.com/p/ba9347a5a05a */ public class RomUtil { private static final String TAG = "Rom"; public static final String ROM_MIUI = "MIUI"; public static final String ROM_EMUI = "EMUI"; public static final String ROM_FLYME = "FLYME"; public static final String ROM_OPPO = "OPPO"; public static final String ROM_SMARTISAN = "SMARTISAN"; public static final String ROM_VIVO = "VIVO"; public static final String ROM_QIKU = "QIKU"; private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name"; private static final String KEY_VERSION_EMUI = "ro.build.version.emui"; private static final String KEY_VERSION_OPPO = "ro.build.version.opporom"; private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version"; private static final String KEY_VERSION_VIVO = "ro.vivo.os.version"; private static String sName; private static String sVersion; //华为 public static boolean isEmui() { return check(ROM_EMUI); } //小米 public static boolean isMiui() { return check(ROM_MIUI); } //vivo public static boolean isVivo() { return check(ROM_VIVO); } //oppo public static boolean isOppo() { return check(ROM_OPPO); } //魅族 public static boolean isFlyme() { return check(ROM_FLYME); } //360手机 public static boolean is360() { return check(ROM_QIKU) || check("360"); } public static boolean isSmartisan() { return check(ROM_SMARTISAN); } public static String getName() { if (sName == null) { check(""); } return sName; } public static String getVersion() { if (sVersion == null) { check(""); } return sVersion; } public static boolean check(String rom) { if (sName != null) { return sName.equals(rom); } if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) { sName = ROM_MIUI; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) { sName = ROM_EMUI; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) { sName = ROM_OPPO; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) { sName = ROM_VIVO; } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) { sName = ROM_SMARTISAN; } else { sVersion = Build.DISPLAY; if (sVersion.toUpperCase().contains(ROM_FLYME)) { sName = ROM_FLYME; } else { sVersion = Build.UNKNOWN; sName = Build.MANUFACTURER.toUpperCase(); } } return sName.equals(rom); } public static String getProp(String name) { String line = null; BufferedReader input = null; try { Process p = Runtime.getRuntime().exec("getprop " + name); input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); line = input.readLine(); input.close(); } catch (IOException ex) { Log.e(TAG, "Unable to read prop " + name, ex); return null; } finally { if (input != null) { try { input.close(); } catch (IOException e) { e.printStackTrace(); } } } return line; } }
使用
//初始化推送SDK private void initPushSdk(){ if(RomUtil.isMiui()){//如果是小米设备,则初始化小米推送 initXiaoMiPush(); }else if(RomUtil.isEmui()){ //如果是华为设备,则初始化华为推送 initHuaWeiPush(); } initJpush();//极光推送均初始化,但是如果是小米、华为设备,则需要关闭通知栏 }
小米手机只初始化极光和小米,之后再把极光通知栏推送这一块关掉。 华为手机只初始化极光和华为,之后再把极光通知栏推送这块关掉。
else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知"); int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); //如果当前是小米、华为设备,则不显示极光推送过来的通知 if(RomUtil.isMiui() || RomUtil.isEmui()){ JPushInterface.clearNotificationById(context, notifactionId);//清除指定某个通知。 } }