Android Xamarin.Forms 中未显示权限对话框

Android permission dialog not showing in Xamarin.Forms

我正在使用 microphone 开发一个 Xamarin.Forms 应用程序。我的应用程序在华为 P9-Phone 上使用 Android 7.1 (Nougat)。我的 android 清单包括:

<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

如果我尝试使用 microphone,权限对话框仍然没有显示。我可以在 phone 设置中手动设置权限(如果我这样做,应用程序可以正常工作)。

接入麦克风的代码是:

this.recorder = new MediaRecorder();

this.recorder.SetAudioSource(AudioSource.Mic);
this.recorder.SetOutputFormat(OutputFormat.AmrWb);
this.recorder.SetAudioEncoder(AudioEncoder.AmrWb);
this.recorder.SetAudioSamplingRate(16000);

var directoryName = Configuration.RecordDirectory;

if (!Directory.Exists(directoryName))
{
    Directory.CreateDirectory(directoryName);
}

var fileName = Configuration.RecordName + Configuration.RecordExtension;
string path = Path.Combine(Configuration.RecordDirectory, fileName);

this.recorder.SetOutputFile(path);
this.recorder.Prepare();
this.recorder.Start();

我错过了什么?

我可以在运行时强制应用程序显示对话框吗?

已解决

阅读 this blog article,开发人员必须手动实现权限请求,因为 android marshmellow。

自 Android 6.0(Marshmallow,SDK >= 23)起,您必须在运行时请求权限。引入此功能是为了防止用户在安装应用程序期间必须接受所有权限。

本文介绍了更新后的权限工作流程:https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

(添加为社区 Wiki,因为问题中已经提供了答案)

in MainActivity.cs file, add below code in OnCreate existing method

string[] PermissionsArray = null;

protected override void OnCreate(Bundle bundle)
        {
                TabLayoutResource = Resource.Layout.Tabbar;
                ToolbarResource = Resource.Layout.Toolbar;

                base.OnCreate(bundle);

                global::Xamarin.Forms.Forms.Init(this, bundle);

                LoadApplication(new App());

                Initializer.Initialize();
                updateNonGrantedPermissions();

                try
                {
                    if (PermissionsArray != null && PermissionsArray.Length > 0)
                    {
                        if (Build.VERSION.SdkInt >= BuildVersionCodes.M)
                        {
                            ActivityCompat.RequestPermissions(this, PermissionsArray, 0);
                        }
                    }
                }
                catch(Exception oExp)
                {

                }
}

 private void updateNonGrantedPermissions()
        {
            try
            {
                List<string> PermissionList = new List<string>();
                PermissionList.Add(Manifest.Permission.MediaContentControl);
                if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.RecordAudio) != (int)Android.Content.PM.Permission.Granted)
                {
                    PermissionList.Add(Manifest.Permission.RecordAudio);
                }
                if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.WriteExternalStorage) != (int)Android.Content.PM.Permission.Granted)
                {
                    PermissionList.Add(Manifest.Permission.WriteExternalStorage);
                }
                if (ContextCompat.CheckSelfPermission(Forms.Context, Manifest.Permission.ReadPhoneState) != (int)Android.Content.PM.Permission.Granted)
                {
                    PermissionList.Add(Manifest.Permission.ReadPhoneState);
                }
                PermissionsArray = new string[PermissionList.Count];
                for (int index = 0; index < PermissionList.Count; index++)
                {
                    PermissionsArray.SetValue(PermissionList[index], index);
                }
            }
            catch(Exception oExp)
            {

            }
        }