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;
    }

你可能感兴趣的:(android7.0及以上TelephonyManager.getDeviceId()返回null解决方案)