Xamarin Asking Permission Dialog not pop up in Android API 33

  Kiến thức lập trình

I have an issue for Xamarin App. The problem is about the dialog for asking permission. With the same code, for API 33 the dialog is not showing. But for API 31 it appears.

PermissionStatus storageStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Storage);

if (storageStatus != PermissionStatus.Granted)
{
    Dictionary<Permission, PermissionStatus> results = null;
    if (Device.RuntimePlatform == Device.Android)
    {
        await _FileHelper.ShowSnackbar("AppName requires permission to access and save reports.");
        results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });
    }
    else
        results = await CrossPermissions.Current.RequestPermissionsAsync(new[] { Permission.Storage });

    storageStatus = results[Permission.Storage];
}

Below dialog example for API 31:

My app doesn’t have an issue about permission, but my concern is where did the alert go? Any explanation for that? See my code bellow.

MainActivity.cs

protected override void OnCreate(Bundle bundle)
{ 
    CurrentActivity = this;
    base.OnCreate(bundle);
    CrossCurrentActivity.Current.Activity = this;

    App.ScreenHeight = (int)(Resources.DisplayMetrics.HeightPixels / Resources.DisplayMetrics.Density);
    App.ScreenWidth = (int)(Resources.DisplayMetrics.WidthPixels / Resources.DisplayMetrics.Density);

    Plugin.CurrentActivity.CrossCurrentActivity.Current.Init(this, bundle);

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

    OxyPlot.Xamarin.Forms.Platform.Android.PlotViewRenderer.Init();
    
    AllFileSystemPermission allFileSystemPermission = new AllFileSystemPermission();
    allFileSystemPermission.RequestAsync();
    
    LoadApplication(new App());
}

public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{  
    PermissionsImplementation.Current.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    base.OnRequestPermissionsResult(requestCode, permissions, grantResults); 
}

Manifest File:

<uses-sdk android:minSdkVersion="16" android:targetSdkVersion="33" />
<application android:label="AppName" android:icon="@drawable/icon">
    <provider 
        android:name="android.support.v4.content.FileProvider" 
        android:authorities="com.qdi.appname.android.fileprovider" 
        android:exported="false" 
        android:grantUriPermissions="true">
        <meta-data 
            android:name="android.support.FILE_PROVIDER_PATHS" 
            android:resource="@xml/provider_paths" />
    </provider>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

<queries>
    <intent>
        <action android:name="android.media.action.IMAGE_CAPTURE" />
    </intent>
</queries>

I also followed this github about the same issue, but in this case I don’t get it about the dialog issue. They did not talk about the dialog. Yes the github post helps to maintain the permission but how the dialog? If I open the settings -> app -> and go to my app -> then permission -> allow media file access then everything will be fine. But what I wanted is the dialog.

I have done:

  1. Using File Provider (not necessary just FYI)
  2. Add new permission which is READ_MEDIA_IMAGES and READ_MEDIA_VIDEO
  3. Extend Permissions.BasePermission by following this link

Also by this link, there is some code that I stubble upon. Why if more than BuildVersionCodes.R there is an intent to Settings as below

var intent = new Intent(Android.Provider.Settings.ActionManageAllFilesAccessPermission);
intent.AddCategory(Android.Content.Intent.CategoryDefault);
intent.SetData(Android.Net.Uri.Parse($"package:{Platform.AppContext.PackageName}"));
Platform.CurrentActivity.StartActivity(intent);

Where is the dialog?

Any advice? Thanks in advance.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website Kho Theme wordpress Kho Theme WP Theme WP

LEAVE A COMMENT