android7.0及以上TelephonyManager.getDeviceId()返回null解决方案

在android7.0及以上的系统下发现TelephonyManager.getDeviceId()在权限允许的情况下取得返回值也为null,解决方法如下:

    /**
     * 获取设备的id
     * @return
     */
    private String getDeviceId(){
        TelephonyManager  telephonyManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
        String deviceId = telephonyManager.getDeviceId();
        if (deviceId==null){
            //android.provider.Settings;
            deviceId= Settings.Secure.getString(getApplicationContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }
        return deviceId;
    }
    原文作者:majorty
    原文地址: https://www.jianshu.com/p/db7891670d39
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞