Android Question [SOLVED] Wait for result with Activity_PermissionResult

hbruno

Member
Licensed User
Longtime User
Hello,
I have a problem with "Activity_PermissionResult" :
if i write this in Activity_Create :
B4X:
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        StoragePermission=Result 'Global variable
when i reach "Wait For", the program jump instantly to "Activity_Resume" and so, my variable StoragePermission was never set.
How can i stop the program for waiting the user response ?
I have the same question with :
B4X:
        MES.GetPermission
        Wait For MES_StorageAvailable
        StoragePermission=MES.HasPermission
it's annoying because right after getting permission to write in external storage, I have to create a directory and copy a file into it, what is done in Activity_Create.
Thanks for the person who save my journey !
 

agraham

Expert
Licensed User
Longtime User
This is expected behaviour. Your StoragePermission variable will be set when Activity_Create is resumed and returns the permission result. If you must check and create the folder at startup then do it in Activity_Create after the Wait For.

This is how my BasicIDE development environment does it:
B4X:
    ' Target SDK 28and earlier
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE) ' Implicit read capability if granted
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)   
    Log($"PERMISSION_WRITE_EXTERNAL_STORAGE = ${Result}"$)   
    
    ' Target SDK30 and later
    ' manually add AddPermission(android.permission.MANAGE_EXTERNAL_STORAGE) to manifest
    'Use the ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION intent action to direct users to a system settings page where they can enable the following option for your app: Allow access to manage all files.
    'To determine if granted call Environment.isExternalStorageManager()
    'Dim in As Intent
    'in.Initialize("android.settings.MANAGE_APP_ALL_FILES_ACCESS_PERMISSION"", "package:my.package.app")
    'StartActivity(in)    
    
    If Not(File.IsDirectory(File.DirRootExternal, IdeDir)) Then
        File.MakeDir(File.DirRootExternal, IdeDir)
    End If
    If Not(File.IsDirectory(IdePath, IdeHelpDir)) Then
        File.MakeDir(IdePath, IdeHelpDir)
    End If
    If Not(File.IsDirectory(IdePath, IdeLayoutDir)) Then
        File.MakeDir(IdePath, IdeLayoutDir)
    End If
    If Not(File.IsDirectory(IdePath, IdeLauncherDir)) Then
        File.MakeDir(IdePath, IdeLauncherDir)
    End If
    
    rp.CheckAndRequest(rp.PERMISSION_ACCESS_FINE_LOCATION)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Script.GPSAvailable = True
    Else
        Script.GPSAvailable = False
    End If
 
Upvote 0

hbruno

Member
Licensed User
Longtime User
Thanks Andrew,
I saw my error: all actions (request permission, make folder, file copy) can be set in sequence in Activity_Create, and in Activity_Resume, as long as I don't have the permissions, I have to set the access to the files in a try-catch.
 
Upvote 0
Top