简体   繁体   中英

Android permission dialog not showing in Xamarin.Forms

I am developing a Xamarin.Forms app using the microphone. My app uses Android 7.1 (Nougat) on a Huawei P9-Phone. My android manifest includes:

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

Still the permission dialog is not showing if i try to use the microphone. I can set the permission manually in the phone settings (the app works if i do so).

The code that accesses the mic is:

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();

What am I missing?

Can I force the app during runtime to show the dialog?

Solved

Reading this blog article , developers have to implement permission requests manually since android marshmellow.

Since Android 6.0 (Marshmallow, SDK >= 23), you have to request permissions at runtime. This has been introduced to prevent the user from having to accept all permissions during installation of the app.

This article explains the updated permission workflow: https://blog.xamarin.com/requesting-runtime-permissions-in-android-marshmallow/

(Added as community wiki since the answer was already provided in the question)

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)
            {

            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM