Android Question How to inform the user of why a permission is needed, then ask for it

JohnC

Expert
Licensed User
Longtime User
I don't know if it's because it's late and my brain is not working at peak, but I'm having a hard time with this puzzle, and maybe someone can come up with a solution.

Basically, I don't want to overload the user asking for a bunch of permissions the first time they run the app. So, I would rather wait until they actually need a particular permission, then ask for it.

But I also want to explain to the user why my app needs that permission, so they wont be confused with a cryptic "Allow MyApp to access photos, media, and files on your device?" prompt that seems more dangerous then what I need it for.

There is also a few different parts of my app that need Read_External_Storage permission, so I wanted to create a single Sub that will do all the above and return "True" if permission was granted, or False if not.

Here is what I have so far and the problem seems to be because I have multiple wait for's:
B4X:
Sub cmdSoundPick_Click
    'allows user to pick a notification sound
  
    Wait for (CheckReadStoragePermission) Complete (Result As Boolean)
    If Result = False Then Return  'exit if no permission
  
    rm.ShowRingtonePicker("rm", Bit.Or(rm.TYPE_RINGTONE, Bit.Or(rm.TYPE_ALARM,rm.TYPE_NOTIFICATION)), False, cmdSoundPick.tag)
End Sub

Sub CheckReadStoragePermission As ResumableSub
    If Starter.rp.Check(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE) = False Then
        MsgboxAsync("In order for this app to play a built-in sound on your phone, it needs 'Read-Only' access to your 'Media'. The following screen will ask for that permission. If you deny this permission, then you will not be able to select a custom sound.","Sound Permission")
        Wait For MsgBox_Result (Result As Int)
      
        Wait for (AskReadStoragePermission) Complete (Result As Boolean)    '<=========== get "Declaration does not match previous one" error
        Return Result
      
    Else
        Return True
    End If
End Sub

Sub AskReadStoragePermission As ResumableSub
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Return Result
End Sub
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
Untested

B4X:
Sub cmdSoundPick_Click
    'allows user to pick a notification sound

    Wait for (CheckReadStoragePermission) Complete (Result As Boolean)
    If Result = False Then Return  'exit if no permission

    rm.ShowRingtonePicker("rm", Bit.Or(rm.TYPE_RINGTONE, Bit.Or(rm.TYPE_ALARM,rm.TYPE_NOTIFICATION)), False, cmdSoundPick.tag)
End Sub

Sub CheckReadStoragePermission As ResumableSub
    If Starter.rp.Check(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE) = False Then
        MsgboxAsync("In order for the app to play a built-in sound on your phone, it needs 'Read-Only' access to your 'Media'. The following screen will ask for that permission. If you deny this permission, then you will not be able to select a custom sound.","Sound Permission")
        Wait For MsgBox_Result (Result As Int)
   
        Wait for (AskReadStoragePermission) Complete (myResult As Boolean)    '<=========== Just change the valuename of the resulting event. It is NOT a must to use the Name Result
        Return myResult
   
    Else
        Return True
    End If
End Sub

Sub AskReadStoragePermission As ResumableSub
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    Return Result
End Sub

In line 13 you are waiting for he result from the msgbox. Result As Int
In Line 15 you are waiting for the storagepermissionresult which you defined as Result As Boolean.
The problem is that you are still inside a sub and you are redefining the same Variablename with a other Type.
The easy solution is probably to just use another name for Result in Line 15. See my comment.
 
Last edited:
Upvote 0

JohnC

Expert
Licensed User
Longtime User
As Mr. Burns would say "Excellent! ....(with rolling fingers)".

I'll give it a try!
 
Upvote 0

JohnC

Expert
Licensed User
Longtime User
Upvote 0
Top