Android device information management tool class

public class DeviceInfoUtils {

/**
 * 获取手机设备名
 *
 * @return
 */
public static String getDeviceModel() {
    return Build.MODEL;
}

/**
 * 获取系统sdk版本号
 *
 * @return
 */
public static String getSystemSdkVersion() {
    return Build.VERSION.RELEASE;
}

/**
 * 获取系统当前语言
 *
 * @return
 */
public static String getSystemLanguage() {
    return Locale.getDefault().getLanguage();
}

/**
 * 获取手机IMEI
 *
 * @param context
 * @return
 */
public static String getIMEI(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getDeviceId();
}


/**
 * 获取手机imsi
 * 国际移动用户识别码
 *
 * @param context
 * @return
 */
public static String getIMSI(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    return tm.getSubscriberId();
}

/**
 * 获取AndroidId
 *
 * @param context
 * @return
 */
public static String getAndroidId(Context context) {
    return Settings.System.getString(context.getContentResolver(), Settings.System.ANDROID_ID); // 获取AndroidId
}

/**
 * 获取wifi物理地址
 *
 * @param context
 * @return
 */
public static String getWifiMac(Context context) {
    WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo info = wifi.getConnectionInfo();
    String wifiMac = info.getMacAddress();
    return wifiMac;
}

/**
 * 获取设备的唯一标识
 *
 * @param context
 * @return 唯一标识字符串
 */
public static String getDeviceUUID(Context context) {
    String uuid = getIMEI(context);
    if (TextUtils.isEmpty(uuid)) {
        uuid = getWifiMac(context);
    }
    if (TextUtils.isEmpty(uuid)) {
        uuid = getAndroidId(context);
    }
    if (TextUtils.isEmpty(uuid)) {
        uuid = autoGenerateUUID(context);
    }
    return uuid;
}

/**
 * 获取一个uniqueid
 *
 * @param context
 * @return
 */
public static String autoGenerateUUID(Context context) {
    SharedPreferences preferences = context.getSharedPreferences("cacheMap", Context.MODE_PRIVATE);
    String uuid = preferences.getString("uuid", "");
    if (TextUtils.isEmpty(uuid)) {
        uuid = UUID.randomUUID().toString();
        preferences.edit().putString("uuid", uuid);
    }
    return uuid;
}

/**
 * 获取一个uuid
 *
 * @return
 */
public static String createUUID() {
    return UUID.randomUUID().toString();
}


/**
 * 获取系统类型
 *
 * @return
 */
public static String getOS() {
    return "android";
}


/**
 * 是否高于4.4版本
 *
 * @return boolean
 * @auther snubee
 */
public static boolean isKitKat() {
    return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
}

public static class ScreenInfo {
    public int   width;
    public int   height;
    public float density;

}

/**
 * 获取屏幕参数
 *
 * @param context
 * @return
 */
public static ScreenInfo getScreenInfo(Context context) {
    ScreenInfo info = new ScreenInfo();
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    info.width = dm.widthPixels;
    info.height = dm.heightPixels;
    info.density = dm.density;
    return info;
}

/**
 * 获取屏幕参数 如: 1280*1080
 *
 * @param context
 * @return
 */
public static String getScreenInfos(Context context) {
    DisplayMetrics dm = new DisplayMetrics();
    WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    windowManager.getDefaultDisplay().getMetrics(dm);
    return dm.heightPixels + "*" + dm.widthPixels;
}

/**
 * @desc 是否有摄像头权限
 * @create by snubee
 * @time 2016/3/2 17:00
 */
public static boolean checkCameraPermission(Context activity) {
    int res = activity.checkCallingOrSelfPermission("android.permission.CAMERA");
    return (res == PackageManager.PERMISSION_GRANTED);
}

// 检查调用者是否具有 permission权限
// 此方法仅在调用IPC(interprocess communication)方法时有用
public static boolean checkPermission(Context context, String permission) {
    //检查如果是当前应用则返回真
    if (Binder.getCallingPid() == android.os.Process.myPid()) {
        if (context.checkCallingPermission(permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
    }
    return false;
}

/**
 * @desc 获取底部虚拟导航栏高度
 * @create by lion
 * @time 2016/5/24 17:00
 */
public static int getNavigationBarHeight(Context activity) {
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

/**
 * @desc 获取顶部状态航栏高度
 * @create by lion
 * @time 2016/5/24 17:00
 */
public static int getStatusBarHeight(Context activity) {
    Resources resources = activity.getResources();
    int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    int height = resources.getDimensionPixelSize(resourceId);
    return height;
}

@SuppressLint("NewApi")
public static boolean checkDeviceHasNavigationBar(Context activity) {

    //通过判断设备是否有返回键、菜单键(不是虚拟键,是手机屏幕外的按键)来确定是否有navigation bar
    boolean hasMenuKey = ViewConfiguration.get(activity)
            .hasPermanentMenuKey();
    boolean hasBackKey = KeyCharacterMap
            .deviceHasKey(KeyEvent.KEYCODE_BACK);

    return !hasMenuKey && !hasBackKey;
}

/**
 * 获取手机品牌(厂商)
 *
 * @return
 */
public static String getDeviceOem() {
    return Build.MANUFACTURER;
}


/**
 * 获取设备系统版本号 如: 8
 *
 * @return 设备系统版本号
 */
public static String getSDKVersion() {
    return String.valueOf(android.os.Build.VERSION.SDK_INT);
}


public static String getDeviceSimOperator(Context mContext) {
    String telManagerName = "";
    TelephonyManager telManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
    String operator = telManager.getSimOperator();
    if (operator != null) {

        if (operator.equals("46000") || operator.equals("46002") || operator.equals("46007")) {

            //中国移动
            telManagerName = "CMCC";
        } else if (operator.equals("46001")) {

            //中国联通
            telManagerName = "CUCC";
        } else if (operator.equals("46003")) {

            //中国电信
            telManagerName = "CTCC";
        }

    }
    return telManagerName;
}

}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324519460&siteId=291194637