【工具】判断国产各大Rom的工具类

由于近期开发涉及到消息推送,接入了小米、华为、魅族的官方推送SDK,相应的,就需要判断用户的手机系统,根据系统进行不同的配置。

本工具均摘自于各大手机厂商开放平台,如有失效或者判断异常的,请各位尽可能的反馈一下,我做一下适配。

09月04日,更新flyme的判断逻辑,新增Smartisan;代码见末尾。

全部代码如下:

import android.os.Build;
import android.text.TextUtils;
import android.util.Log;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Method;

/**
 * Created by SherlockHolmes on 2017/8/17.
 */

public class RomUtil {
    private static final String TAG = "RomUtil";

    /**
     * 判断是否为华为系统
     */
    public static boolean isHuaweiRom() {
        if (!TextUtils.isEmpty(getEmuiVersion()) && !getEmuiVersion().equals("")) {
            Log.d(TAG, "isHuaweiRom: true");
            return true;
        }
        Log.d(TAG, "isHuaweiRom: false");
        return false;
    }

    /**
     * 判断是否为小米系统
     */
    public static boolean isMiuiRom() {
        if (!TextUtils.isEmpty(getSystemProperty("ro.miui.ui.version.name"))) {
            Log.d(TAG, "isMiuiRom: true");
            return true;
        }
        Log.d(TAG, "isMiuiRom: false");
        return false;
    }

    private 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(TAG, "Unable to read sysprop " + propName, ex);
            return null;
        } finally {
            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    Log.e(TAG, "Exception while closing InputStream", e);
                }
            }
        }
        return line;
    }

    /**
     * 判断是否为Flyme系统
     */
    public static boolean isFlymeRom() {
        try {
            // Invoke Build.hasSmartBar()
            final Method method = Build.class.getMethod("hasSmartBar");
            return method != null;
        } catch (final Exception e) {
            return false;
        }
    }

    /**
     * @return 只要返回不是"",则是EMUI版本
     */
    private static String getEmuiVersion() {
        String emuiVerion = "";
        Class<?>[] clsArray = new Class<?>[]{String.class};
        Object[] objArray = new Object[]{"ro.build.version.emui"};
        try {
            Class<?> SystemPropertiesClass = Class
                    .forName("android.os.SystemProperties");
            Method get = SystemPropertiesClass.getDeclaredMethod("get",
                    clsArray);
            String version = (String) get.invoke(SystemPropertiesClass,
                    objArray);
            Log.d(TAG, "get EMUI version is:" + version);
            if (!TextUtils.isEmpty(version)) {
                return version;
            }
        } catch (ClassNotFoundException e) {
            Log.e(TAG, " getEmuiVersion wrong, ClassNotFoundException");
        } catch (LinkageError e) {
            Log.e(TAG, " getEmuiVersion wrong, LinkageError");
        } catch (NoSuchMethodException e) {
            Log.e(TAG, " getEmuiVersion wrong, NoSuchMethodException");
        } catch (NullPointerException e) {
            Log.e(TAG, " getEmuiVersion wrong, NullPointerException");
        } catch (Exception e) {
            Log.e(TAG, " getEmuiVersion wrong");
        }
        return emuiVerion;
    }
}

查看源码:Git地址

09月04日,更新flyme的判断逻辑,新增Smartisan:

由于之前判断flyme的方式不支持flyme6,更改为如下方式,这种方式(isInstalledByPkgName()),原理上支持判断所有的定制系统(定制系统有自己特殊的app即可,比如系统更新等系统级别的app,只要是升级系统或者常规方式无法卸载的app均可做为判断依据)

更新flyme的判断逻辑:


    /**
     * 判断是否为Flyme系统
     */
    public static boolean isFlymeRom(Context context) {
        /**
         * 此方法对flyme6无效
         */
//        try {
//            // Invoke Build.hasSmartBar()
//            final Method method = Build.class.getMethod("hasSmartBar");
//            return method != null;
//        } catch (final Exception e) {
//            return false;
//        }

        return isInstalledByPkgName(context, "com.meizu.flyme.update");
    }


    /**
     * 根据包名判断这个app是否已安装
     * @param context
     * @param pkgName
     * @return
     */
    public static boolean isInstalledByPkgName(Context context, String pkgName) {
        PackageInfo packageInfo;
        try {
            packageInfo = context.getPackageManager().getPackageInfo(pkgName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            packageInfo = null;
            e.printStackTrace();
        }
        if (packageInfo == null) {
            Log.d(TAG, "isFlymeRom: true");
            return false;
        } else {
            Log.d(TAG, "isFlymeRom: false");
            return true;
        }
    }

新增Smartisan:


    /**
     * 判断是否是Smartisan系统
     * @param context
     * @return
     */
    public static boolean isSmartisanRom(Context context) {
        return isInstalledByPkgName(context, "com.smartisanos.security");
    }

查看源码:Git地址

    原文作者:工藤一号
    原文地址: https://www.jianshu.com/p/fabbe5c25162
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞