Android专业获取设备信息如:AndroidID、唯一设备ID、制造商、型号、版本号、版本码等

  • 在开发app中很多时候需要获取设备的基本信息等运用到项目中,就需要一些方法获取,网上资源中方法五花八门,有的还获取不到,令人头大,话不多说,鄙人整理了一套方法,话不多说真机测试如下》上图:在这里插入图片描述
  • 接下来就是代码片段了
  • 第一步接口方法类:DeviceUtils调用方法:DeviceUtils deviceUtils=new DeviceUtils(this);//实例化
  • 方法调用介绍
  • // LogTools.e(“获取设备手机制造商:”+ deviceUtils.getManufacturer());
    // LogTools.e(“获取设备AndroidID:”+deviceUtils.getAndroidID());
    // LogTools.e(“获取设备型号:”+deviceUtils.getModel());
    // LogTools.e(“获取唯一设备 ID:”+deviceUtils.getUniqueDeviceId());
    // LogTools.e(“获取设备 AppVersionCode:”+deviceUtils.getVersionCode());
    // LogTools.e(“获取唯一设备 AppVersionName:”+deviceUtils.getVerName());
    // LogTools.e(“获取设备系统版本号:”+ deviceUtils.getSDKVersionName());
    // LogTools.e(“获取设备系统版本码:”+deviceUtils.getSDKVersionCode());
    接下来就只需要傻瓜式复制粘贴就好
public class DeviceUtils {
   
   private static  Activity activity;
   public DeviceUtils(Activity activity) {
   
        this.activity=activity;
    }


    public static int getVersionCode() {
   
        int versionCode = 0;
        try {
   
            //获取软件版本号,对应AndroidManifest.xml下android:versionCode
            versionCode = activity.getPackageManager().
                    getPackageInfo(activity.getPackageName(), 0).versionCode;
        } catch (PackageManager.NameNotFoundException e) {
   
            e.printStackTrace();
        }
        return versionCode;
    }

    /**
     * 获取版本号名称
     *
     * @param
     * @return
     */
    public static String getVerName() {
   
        String verName = "";
        try {
   
            verName = activity.getPackageManager().
                    getPackageInfo(activity.getPackageName(), 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
   
            e.printStackTrace();
        }
        return verName;
    }


    /**
     * Return the version name of device's system.
     *
     * @return the version name of device's system
     */
    public static String getSDKVersionName() {
   
        return Build.VERSION.RELEASE;
    }

    /**
     * Return version code of device's system.
     *
     * @return version code of device's system
     */
    public static int getSDKVersionCode() {
   
        return Build.VERSION.SDK_INT;
    }

    /**
     * Return the android id of device.
     *
     * @return the android id of device
     */
    @SuppressLint("HardwareIds")
    public static String getAndroidID() {
   
        String id = Settings.Secure.getString(
                activity.getContentResolver(),
                Settings.Secure.ANDROID_ID
        );
        if ("9774d56d682e549c".equals(id)) return "";
        return id == null ? "" : id;
    }

    /**
     * Return the manufacturer of the product/hardware.
     * <p>e.g. Xiaomi</p>
     *
     * @return the manufacturer of the product/hardware
     */
    public static String getManufacturer() {
   
        return Build.MANUFACTURER;
    }

    /**
     * Return the model of device.
     * <p>e.g. MI2SC</p>
     *
     * @return the model of device
     */
    public static String getModel() {
   
        String model = Build.MODEL;
        if (model != null) {
   
            model = model.trim().replaceAll("\\s*", "");
        } else {
   
            model = "";
        }
        return model;
    }

    /**
     * Return an ordered list of ABIs supported by this device. The most preferred ABI is the first
     * element in the list.
     *
     * @return an ordered list of ABIs supported by this device
     */
    public static String[] getABIs() {
   
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
   
            return Build.SUPPORTED_ABIS;
        } else {
   
            if (!TextUtils.isEmpty(Build.CPU_ABI2)) {
   
                return new String[]{
   Build.CPU_ABI, Build.CPU_ABI2};
            }
            return new String[]{
   Build.CPU_ABI};
        }
    }


    private static final    String KEY_UDID = "KEY_UDID";
    private volatile static String udid;

    /**
     * Return the unique device id.
     * <pre>{1}{UUID(macAddress)}</pre>
     * <pre>{2}{UUID(androidId )}</pre>
     * <pre>{9}{UUID(random    )}</pre>
     *
     * @return the unique device id
     */
    public static String getUniqueDeviceId() {
   
        return getUniqueDeviceId("", true);
    }

    /**
     * Return the unique device id.
     * <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
     * <pre>{prefix}{2}{UUID(androidId )}</pre>
     * <pre>{prefix}{9}{UUID(random    )}</pre>
     *
     * @param prefix The prefix of the unique device id.
     * @return the unique device id
     */
    public static String getUniqueDeviceId(String prefix) {
   
        return getUniqueDeviceId(prefix, true);
    }

    /**
     * Return the unique device id.
     * <pre>{1}{UUID(macAddress)}</pre>
     * <pre>{2}{UUID(androidId )}</pre>
     * <pre>{9}{UUID(random    )}</pre>
     *
     * @param useCache True to use cache, false otherwise.
     * @return the unique device id
     */
    public static String getUniqueDeviceId(boolean useCache) {
   
        return getUniqueDeviceId("", useCache);
    }

    /**
     * Return the unique device id.
     * <pre>android 10 deprecated {prefix}{1}{UUID(macAddress)}</pre>
     * <pre>{prefix}{2}{UUID(androidId )}</pre>
     * <pre>{prefix}{9}{UUID(random    )}</pre>
     *
     * @param prefix   The prefix of the unique device id.
     * @param useCache True to use cache, false otherwise.
     * @return the unique device id
     */
    public static String getUniqueDeviceId(String prefix, boolean useCache) {
   
        if (!useCache) {
   
            return getUniqueDeviceIdReal(prefix);
        }
        if (udid == null) {
   
            synchronized (DeviceUtils.class) {
   
                if (udid == null) {
   
                    UtilsBridge utilsBridge=new UtilsBridge(activity);
                    final String id = utilsBridge.getSpUtils4Utils().getString(KEY_UDID, null);
                    if (id != null) {
   
                        udid = id;
                        return udid;
                    }
                    return getUniqueDeviceIdReal(prefix);
                }
            }
        }
        return udid;
    }

    private static String getUniqueDeviceIdReal(String prefix) {
   
        try {
   
            final String androidId = getAndroidID();
            if (!TextUtils.isEmpty(androidId)) {
   
                return saveUdid(prefix + 2, androidId);
            }

        } catch (Exception ignore) {
   /**/}
        return saveUdid(prefix + 9, "");
    }



    private static String saveUdid(String prefix, String id) {
   
        udid = getUdid(prefix, id);
        UtilsBridge utilsBridge=new UtilsBridge(activity);
        utilsBridge.getSpUtils4Utils().put(KEY_UDID, udid);
        return udid;
    }

    private static String getUdid(String prefix, String id) {
   
        if (id.equals("")) {
   
            return prefix + UUID.randomUUID().toString().replace("-", "");
        }
        return prefix + UUID.nameUUIDFromBytes(id.getBytes()).toString().replace("-", "");
    }
}
public  class AdaptScreenUtils {
   

    private static List<Field> sMetricsFields;

    private AdaptScreenUtils() {
   
        throw new UnsupportedOperationException("u can't instantiate me...");
    }


    private static void applyDisplayMetrics(@NonNull final Resources resources, final float newXdpi) {
   
        resources.getDisplayMetrics().xdpi = newXdpi;
        Utils.getApp().getResources().getDisplayMetrics().xdpi = newXdpi;
        applyOtherDisplayMetrics(resources, newXdpi);
    }

    static Runnable getPreLoadRunnable() {
   
        return new Runnable() {
   
            @Override
            public void run() {
   
                preLoad();
            }
        };
    }

    private static void preLoad() {
   
        applyDisplayMetrics
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木易明~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值