Xamarin Android 9.0 GetImei 错误 Java.Lang.SecurityException: getImeiForSlot android.permission.READ_PHONE_STATE

Xamarin Android 9.0 GetImei error Java.Lang.SecurityException: getImeiForSlot android.permission.READ_PHONE_STATE

我试着让我的 unique ID IMEIAndroid 9.0 API LVL 28

当我尝试使用我的代码时出现此错误:

Java.Lang.SecurityException: getImeiForSlot: Neither user 10154 nor current process has android.permission.READ_PHONE_STATE.

        public class DeviceInfo : IDeviceInfo
        {
            TelephonyManager telephonyManager;
            public DeviceInfo()
            {
                telephonyManager = 
     (TelephonyManager)Application.Context.GetSystemService(Context.TelephonyService);          
            }

            public string Id
            {
                get
                {
                    telephonyManager = (TelephonyManager)Application.Context.GetSystemService(Context.TelephonyService);
                    if (Build.VERSION.SdkInt >= BuildVersionCodes.O)
                        // TODO: Some phones has more than 1 SIM card or may not have a SIM card inserted at all
                        return telephonyManager.GetMeid(0);
                    else
    #pragma warning disable CS0618 // Type or member is obsolete
                        return telephonyManager.DeviceId;
    #pragma warning restore CS0618 // Type or member is obsolete
                }
            }

和我的 androdManifest.xml

<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" /> 
<uses-permission android:name="android.permission.READ_PRIVILEGED_PHONE_STATE"/>
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />
  <uses-permission android:name="android.permission.VIBRATE" />

你必须:

  • 在manifest中声明应用需要READ_PHONE_STATE权限。对于 Xamarin 项目:右键单击项目/Properties/Android manifest/Required permissions

  • 请求用户授予权限,请参阅应用程序权限指南中的Runtime requests了解更多详情。