Android Question [Solved] - Run time permission syncronization

ac9ts

Active Member
Licensed User
Longtime User
I am starting to upgrade my apps for the upcoming Google SDK of 26+ requirement and am having an issue with call run time permissions. When my app starts, it checks for the existence of a few files and creates them if they are not there. The issue I am having is on new installs where the permissions haven't been granted yet. The user is still answering the permission questions while the files are trying to be created and the app crashes because it doesn't have permission yet to write to the storage.

I currently have a single activity (Main). I was thinking of moving all the code in Main to a second activity (GUI) and do the permission check in Main. Once the questions are answered, it would start GUI. The next time through, Main would read a flag set the first time and just start the GUI activity.

What is the best practice on waiting for the user to answer these prompts before moving on?
 

DonManfred

Expert
Licensed User
Longtime User
What is the best practice on waiting for the user to answer these prompts before moving on?
Start with checking if the Permission is already granted. If not then
- requestpermission
- wait for the permission result

ONLY if success (Permission granted) you should start copying the files.
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
Start with checking if the Permission is already granted. If not then
- requestpermission
- wait for the permission result

ONLY if success (Permission granted) you should start copying the files.

This is the part I'm asking about. Since the request for permission is non-blocking, the app is crashing while the request prompt is still on the screen.

No need to check for the permission unless you are doing it from a service code.

Always call CheckAndRequest and then Wait For Activity_PermissionResult. If the user already granted permission then the event will be raised immediately.

I do use CheckAndRequest. It's the waiting for Activity_PermissionResult that I'm not sure on.

What I've done is to move all my main activity code to a second activity. I'll do all the permission asking there and only start the second activity once everything has been approved. This should only be an issue on new installs.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of accessing File.DirRootExternal:
B4X:
Sub ReadFile (f As String)
   rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
   Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
   If Result = False Then
       ToastMessageShow("No permission...", True)
   Else
       Log(File.ReadString(File.DirRootExternal, f))
   End If
End Sub
 
Upvote 0

ac9ts

Active Member
Licensed User
Longtime User
This is the ticket!

B4X:
rp.CheckAndRequest(rp.PERMISSION_READ_EXTERNAL_STORAGE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
 
Upvote 0
Top