参考资料
正文
有时候需要判断手机系统的ROM,检测ROM是MIUI、EMUI还是Flyme,可以使用getprop命令,去系统build.prop文件查找是否有对应属性来判断是什么ROM。
build.prop的介绍可以看Android build.prop简介
代码【和参考资料一致,这里只是做个记录,详细介绍,请移步参考资料《Android判断手机ROM》】
1package com.hxzk.som.base.utils; 2 3import android.os.Build; 4import android.text.TextUtils; 5import android.util.Log; 6 7import java.io.BufferedReader; 8import java.io.IOException; 9import java.io.InputStreamReader; 10 11/** 12 * Created by HaiyuKing 13 * Used 判断手机ROM,检测ROM是MIUI、EMUI还是Flyme 14 * 参考资料:https://www.jianshu.com/p/ba9347a5a05a 15 */ 16public class RomUtil { 17 private static final String TAG = "Rom"; 18 19 public static final String ROM_MIUI = "MIUI"; 20 public static final String ROM_EMUI = "EMUI"; 21 public static final String ROM_FLYME = "FLYME"; 22 public static final String ROM_OPPO = "OPPO"; 23 public static final String ROM_SMARTISAN = "SMARTISAN"; 24 public static final String ROM_VIVO = "VIVO"; 25 public static final String ROM_QIKU = "QIKU"; 26 27 private static final String KEY_VERSION_MIUI = "ro.miui.ui.version.name"; 28 private static final String KEY_VERSION_EMUI = "ro.build.version.emui"; 29 private static final String KEY_VERSION_OPPO = "ro.build.version.opporom"; 30 private static final String KEY_VERSION_SMARTISAN = "ro.smartisan.version"; 31 private static final String KEY_VERSION_VIVO = "ro.vivo.os.version"; 32 33 private static String sName; 34 private static String sVersion; 35 36 //华为 37 public static boolean isEmui() { 38 return check(ROM_EMUI); 39 } 40 //小米 41 public static boolean isMiui() { 42 return check(ROM_MIUI); 43 } 44 //vivo 45 public static boolean isVivo() { 46 return check(ROM_VIVO); 47 } 48 //oppo 49 public static boolean isOppo() { 50 return check(ROM_OPPO); 51 } 52 //魅族 53 public static boolean isFlyme() { 54 return check(ROM_FLYME); 55 } 56 //360手机 57 public static boolean is360() { 58 return check(ROM_QIKU) || check("360"); 59 } 60 61 public static boolean isSmartisan() { 62 return check(ROM_SMARTISAN); 63 } 64 65 public static String getName() { 66 if (sName == null) { 67 check(""); 68 } 69 return sName; 70 } 71 72 public static String getVersion() { 73 if (sVersion == null) { 74 check(""); 75 } 76 return sVersion; 77 } 78 79 public static boolean check(String rom) { 80 if (sName != null) { 81 return sName.equals(rom); 82 } 83 84 if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_MIUI))) { 85 sName = ROM_MIUI; 86 } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_EMUI))) { 87 sName = ROM_EMUI; 88 } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_OPPO))) { 89 sName = ROM_OPPO; 90 } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_VIVO))) { 91 sName = ROM_VIVO; 92 } else if (!TextUtils.isEmpty(sVersion = getProp(KEY_VERSION_SMARTISAN))) { 93 sName = ROM_SMARTISAN; 94 } else { 95 sVersion = Build.DISPLAY; 96 if (sVersion.toUpperCase().contains(ROM_FLYME)) { 97 sName = ROM_FLYME; 98 } else { 99 sVersion = Build.UNKNOWN; 100 sName = Build.MANUFACTURER.toUpperCase(); 101 } 102 } 103 return sName.equals(rom); 104 } 105 106 public static String getProp(String name) { 107 String line = null; 108 BufferedReader input = null; 109 try { 110 Process p = Runtime.getRuntime().exec("getprop " + name); 111 input = new BufferedReader(new InputStreamReader(p.getInputStream()), 1024); 112 line = input.readLine(); 113 input.close(); 114 } catch (IOException ex) { 115 Log.e(TAG, "Unable to read prop " + name, ex); 116 return null; 117 } finally { 118 if (input != null) { 119 try { 120 input.close(); 121 } catch (IOException e) { 122 e.printStackTrace(); 123 } 124 } 125 } 126 return line; 127 } 128}
使用
1//初始化推送SDK 2 private void initPushSdk(){ 3 if(RomUtil.isMiui()){//如果是小米设备,则初始化小米推送 4 initXiaoMiPush(); 5 }else if(RomUtil.isEmui()){ 6 //如果是华为设备,则初始化华为推送 7 initHuaWeiPush(); 8 } 9 initJpush();//极光推送均初始化,但是如果是小米、华为设备,则需要关闭通知栏 10 }
小米手机只初始化极光和小米,之后再把极光通知栏推送这一块关掉。 华为手机只初始化极光和华为,之后再把极光通知栏推送这块关掉。
1else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) { 2 Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知"); 3 int notifactionId = bundle.getInt(JPushInterface.EXTRA_NOTIFICATION_ID); 4 Logger.d(TAG, "[MyReceiver] 接收到推送下来的通知的ID: " + notifactionId); 5 6 //如果当前是小米、华为设备,则不显示极光推送过来的通知 7 if(RomUtil.isMiui() || RomUtil.isEmui()){ 8 JPushInterface.clearNotificationById(context, notifactionId);//清除指定某个通知。 9 } 10 11 }
本文转自: