Android Question Wait for RequestPermission=true and then continue

petr4ppc

Well-Known Member
Licensed User
Longtime User
Dear friends, please for advice...

How can check and request permission and after request permission = true will program flow?
I have read, that with function
CheckAndRequest
will program flow.
But I want not flow program while checkandrequest=true - I want wait for permission=true and after this it is possible to flow program.

How can I do this, please?

Thank you very much
p4ppc
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
But I want not flow program while checkandrequest=true

Do you mean that you want the program to pause while the <checkAndRequest> action is being dealt with? If so, I think that you could use something like this ...

B4X:
Sub Globals
    Dim rp As RuntimePermissions
    Dim permissionResult As Boolean
    Dim permissionResultReceived As Boolean
End Sub

' .... .... .... ....
' .... .... .... ....

Sub WriteFile
    permissionResultReceived = False
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Do While Not(permissionResultReceived)
            Sleep(10)     
    Loop
    If (permissionResult) Then File.WriteString(File.DirDefaultExternal, "MyFile", "Here is some data")
End Sub

Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    If (Permission = rp.PERMISSION_WRITE_EXTERNAL_STORAGE) Then permissionResult = Result
    permissionResultReceived = True
End Sub
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Dear friends, please for advice...

How can check and request permission and after request permission = true will program flow?
I have read, that with function will program flow.
But I want not flow program while checkandrequest=true - I want wait for permission=true and after this it is possible to flow program.

How can I do this, please?

Thank you very much
p4ppc

B4X:
Sub Process_Globals
    Dim RP As RuntimePermissions
End Sub

Sub Activity_Create(FirstTime As Boolean)
    RP.CheckAndRequest(RP.PERMISSION_WRITE_EXTERNAL_STORAGE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        'Flow program continues if true
        'Call a sub or put your routine here
    End If
End Sub
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Brian, Erel, Peter - thank you for your advices.

In Activity Create, I Have on first row:
B4X:
If FirstTime=True and Phone.SdkVersion>=26 Then Rules
Because I want first SET the Permissions. After this order I have for example this code
B4X:
       jhw.Initialize()
                 If jhw.Enabled=False Then jhw.Enabled=True
and other codes.

B4X:
SUB Rules
    Dim rp1 As RuntimePermissions 
    If rp1.Check(rp1.PERMISSION_RECEIVE_SMS)=False Then
        rp1.CheckAndRequest(rp1.PERMISSION_RECEIVE_SMS) 
        wait for Activity_PermissionResult(permission As String, result As Boolean)
        If result=True Then
             ToastMessageShow("OK1",True)
          Else         
             ToastMessageShow("KO1",False)
        End If     
    End If

    Dim rp2 As RuntimePermissions
    If rp2.Check(rp2.PERMISSION_READ_CONTACTS) =False Then
        rp2.CheckAndRequest(rp2.PERMISSION_READ_CONTACTS)
        wait for Activity_PermissionResult(permission As String, result As Boolean)
        If result=True Then
            'ToastMessageShow("OK2",True)
          Else
            ToastMessageShow("KO2",True)
        End If     
End Sub

'and more permissions
    End If

And now my question:
If program goes to RULES sub and in point of
B4X:
CheckandRequest
of "PERMISSION_RECEIVE_SMS"
then the flow of program is PAUSED? Or program flow to
B4X:
 jhw.Initialize()
?
It does it mean - program is paused and wait for accept permission or program is flow and not wait for user accapting of permissions?


I think that program flow and not wait for user accepting....its true, please?

Thank you
Best regards
p4ppc
 
Upvote 0

petr4ppc

Well-Known Member
Licensed User
Longtime User
Peter and Erel
thank you very much

Erel - I understand I will no use BOLD function.

I did some correction after your advices and after watching tutorial :
B4X:
    Dim rper As RuntimePermissions

    rper.CheckAndRequest(rper.PERMISSION_RECEIVE_SMS)
    wait for Activity_PermissionResult(permission As String, result As Boolean)

    rper.CheckAndRequest(rper.PERMISSION_READ_CONTACTS)
    wait for Activity_PermissionResult(permission As String, result As Boolean)

    rper.CheckAndRequest(rper.PERMISSION_READ_SMS)
    wait for Activity_PermissionResult(permission As String, result As Boolean)

    rper.CheckAndRequest("android.permission.READ_PHONE_NUMBERS")
    wait for Activity_PermissionResult(permission As String, result As Boolean)

    rper.CheckAndRequest(rper.PERMISSION_READ_PHONE_STATE)
    wait for Activity_PermissionResult(permission As String, result As Boolean) 
    If result=True Then
        ToastMessageShow("OK",True)
    Else
        ToastMessageShow("KO",True)
    End If

but I think that program flow because the app crashed after first accepting of runtime permission


EDIT:
I have solve it. I place permission to the point where I need to use this permission. Realy I think, that program flow if I use solution as described earlier. This is not worked for me.
Thank you for all advices
p4ppc
 
Last edited:
Upvote 0
Top