Android Question How to know if a "Don't ask again" permission has been checked?

Marcob

Member
Licensed User
Hello,

I usually place a Msgbox() before RuntimePermissions.CheckAndRequest() so to let the user have more details about such permission.
If the user check the box "Don't ask again", RuntimePermissions.CheckAndRequest() will stop opening the permission dialog, in that case I would like to avoid showing my Msgbox() too.

Is there a way to check the "Don't ask again" status of the permission?
 

MarkusR

Well-Known Member
Licensed User
i believe there is also RuntimePermissions.Check() , maybe u get useful information there or open your messagebox for user.
in one project i used a extra activity form with some checkboxed just for permission check.
the user have information there and a visual feedback for desired permissions.
i used also the state for the app logic.
 
Last edited:

DonManfred

Expert
Licensed User
Is there a way to check the "Don't ask again" status of the permission?
No. You just can check the permission of you are granted or not.
In case the user used Dont ask again then it was his decision not to get asked again. He need to go into the setting from your app and manually grant the permission (systemsettings).
 

DonManfred

Expert
Licensed User
you can send the user to the systemsettings and describe what the user need to do to grant permission manually.
B4X:
    Dim in As Intent
   in.Initialize("android.settings.APPLICATION_DETAILS_SETTINGS","package:"&Application.PackageName)
   StartActivity(in)
 

Erel

Administrator
Staff member
Licensed User
Complete example:
B4X:
Sub Process_Globals
   Private rp As RuntimePermissions
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   
End Sub

Sub Activity_Click
   If ShouldShowRequestPermissionRationale(rp.PERMISSION_READ_EXTERNAL_STORAGE) Then
       MsgboxAsync("We need permission to access the external storage in order to upload your personal photos to the cloud.", "")
       Wait For Msgbox_Result (Result As Int)
   End If
   rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Success As Boolean)
   If Success Then
       For Each f As String In File.ListFiles(File.DirRootExternal)
           Log(f)
       Next
   End If
End Sub

Sub ShouldShowRequestPermissionRationale (Permission As String) As Boolean
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim ActivityCompat As JavaObject
   ActivityCompat.InitializeStatic("android.support.v4.app.ActivityCompat")
   Return ActivityCompat.RunMethod("shouldShowRequestPermissionRationale", Array(ctxt, Permission))
End Sub
https://developer.android.com/refer...tyCompat#shouldshowrequestpermissionrationale

Note that ShouldShowRequestPermissionRationale returns False when the permission is requested for the first time. Based on my tests it only returns True from the second request and until the "don't ask again" checkbox is checked.
 

Marcob

Member
Licensed User
see the post before yours...

You can send the user to the systemsetting
Thank DonManfred for your suggestion. My ideal solution is to show a msg just before the permission request appears, so the user could make the proper decision. Then, if "don't ask again" is selected, the msg should not appear anymore (as is the case with the permission request).

In this respect, Erel's reply is optimal (thank you very much for showing how to implement ShouldShowRequestPermissionRationale).
Unfortunately, due to the particular behaviour of this function, when the permission is requested for the first time, the msg won't be displayed (just when it is needed the most). Hopefully, this should fix it:

B4X:
Sub Process_Globals
   Private rp As RuntimePermissions
   Private manager as PreferenceManager
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
 
End Sub

Sub Activity_Click
   If rp.Check(rp.PERMISSION_READ_EXTERNAL_STORAGE)=False Then
      If ShouldShowRequestPermissionRationale(rp.PERMISSION_READ_EXTERNAL_STORAGE) Or manager.GetBoolean("msg_storage")=False Then
         MsgboxAsync("We need permission to access the external storage in order to upload your personal photos to the cloud.", "")
         Wait For Msgbox_Result (Result As Int)
         manager.SetBoolean("msg_storage",True)
      End If
      rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
      Wait For Activity_PermissionResult (Permission As String, result As Boolean)
      If Success Then
         For Each f As String In File.ListFiles(File.DirRootExternal)
            Log(f)
         Next
      End If
   Else
      manager.SetBoolean("msg_storage",False)
   End If
End Sub

Sub ShouldShowRequestPermissionRationale (Permission As String) As Boolean
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim ActivityCompat As JavaObject
   ActivityCompat.InitializeStatic("android.support.v4.app.ActivityCompat")
   Return ActivityCompat.RunMethod("shouldShowRequestPermissionRationale", Array(ctxt, Permission))
End Sub
 

Filippo

Expert
Licensed User
@Marcob
Thank you! It is exactly what I was looking for.
 

Filippo

Expert
Licensed User
Complete example:
B4X:
Sub Process_Globals
   Private rp As RuntimePermissions
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
  
End Sub

Sub Activity_Click
   If ShouldShowRequestPermissionRationale(rp.PERMISSION_READ_EXTERNAL_STORAGE) Then
       MsgboxAsync("We need permission to access the external storage in order to upload your personal photos to the cloud.", "")
       Wait For Msgbox_Result (Result As Int)
   End If
   rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Success As Boolean)
   If Success Then
       For Each f As String In File.ListFiles(File.DirRootExternal)
           Log(f)
       Next
   End If
End Sub

Sub ShouldShowRequestPermissionRationale (Permission As String) As Boolean
   Dim ctxt As JavaObject
   ctxt.InitializeContext
   Dim ActivityCompat As JavaObject
   ActivityCompat.InitializeStatic("android.support.v4.app.ActivityCompat")
   Return ActivityCompat.RunMethod("shouldShowRequestPermissionRationale", Array(ctxt, Permission))
End Sub
https://developer.android.com/refer...tyCompat#shouldshowrequestpermissionrationale

Note that ShouldShowRequestPermissionRationale returns False when the permission is requested for the first time. Based on my tests it only returns True from the second request and until the "don't ask again" checkbox is checked.
Hi Erel,
Your code helped me a lot, but not completely met my requirement.
Here is my expanding code, I think it should be self explanatory.
B4X:
Sub Process_Globals
    Private rp As RuntimePermissions
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
  
End Sub

Sub Activity_Click
    GetFileList
End Sub

Sub GetFileList
    If rp.Check( rp.PERMISSION_WRITE_EXTERNAL_STORAGE) Then
        For Each f As String In File.ListFiles(File.DirRootExternal)
            Log(f)
        Next
    Else
        'Allow Permission and return to this Sub
        AllowPermissionToStorage("GetFileList")
    End If
End Sub

#Region Check Permission
Private Sub AllowPermissionToStorage(Callback As String)
    If Not(rp.Check( rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        If ShouldShowRequestPermissionRationale(rp.PERMISSION_READ_EXTERNAL_STORAGE) Then
            Msgbox2Async("In the next dialog you should allow access to photos, media and files, because without this access the app cannot save files or access its own log files.", "Memory-Access", "OK","","", Null, False)
            Wait For Msgbox_Result (Result As Int)
        End If
        Check_EXTERNAL_STORAGE(Callback)
    End If
End Sub

Private Sub Check_EXTERNAL_STORAGE(Callback As String)
    If Not(rp.Check( rp.PERMISSION_WRITE_EXTERNAL_STORAGE)) Then
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result = False Then
            If ShouldShowRequestPermissionRationale(rp.PERMISSION_READ_EXTERNAL_STORAGE) Then
                Msgbox2Async("You have denied access to photos, media and files. Without this access, the app cannot save files or access its own log files." & CRLF & _
                            "Would you like to make your selection again?", "Permission denied!", "YES", "NO", "", Null, False)
                Wait For Msgbox_Result (Ret As Int)
                If Ret = DialogResponse.POSITIVE Then
                    Check_EXTERNAL_STORAGE(Callback)
                End If
            Else
                Msgbox2Async("You have denied access to photos, media and files. Without this access, the app cannot save files or access its own log files." & CRLF & _
                            "These setting can now only be changed in the app setting. Would you like to change your app settings?", "Permission denied!", "YES", "NO", "", Null, False)
                Wait For Msgbox_Result (Ret As Int)
                If Ret = DialogResponse.POSITIVE Then
                    GetPermissionSetting
                End If
            End If
        Else
            CallSub(Me, Callback)
        End If
    End If
End Sub

Private Sub GetPermissionSetting
    Dim in As Intent
    in.Initialize("android.settings.APPLICATION_DETAILS_SETTINGS","package:" & Application.PackageName)
    StartActivity(in)
End Sub

Sub ShouldShowRequestPermissionRationale (Permission As String) As Boolean
    Dim ctxt As JavaObject
    ctxt.InitializeContext
    Dim ActivityCompat As JavaObject
    ActivityCompat.InitializeStatic("android.support.v4.app.ActivityCompat")
    Return ActivityCompat.RunMethod("shouldShowRequestPermissionRationale", Array(ctxt, Permission))
End Sub
#End Region
 
Top