Android Question Request Multiple Permissions in a loop

iCAB

Well-Known Member
Licensed User
Hi All

Can someone please explain this behaviour
The for loop below exits before the user is responds to Starter.rp.PERMISSION_READ_PHONE_STATE prompt

B4X:
Sub Activity_Resume

    Dim blnAllowEntry = True As Boolean
  
    Dim iIndex = 0 As Int
  
    For Each permission As String In Array(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION, Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION, Starter.rp.PERMISSION_READ_PHONE_STATE)
        Starter.rp.CheckAndRequest(permission)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
      
        If Result = False Then
            ToastMessageShow("No permission!", True)
            Activity.Finish
            Return
        Else
            iIndex = iIndex + 1
            Log("iIndex = " &  iIndex)
          
            If permission = Starter.rp.PERMISSION_ACCESS_FINE_LOCATION Then
                GC.GPSClass1.LocalGPS_Start
            End If
  
            If iIndex = 3 Then
                blnPermissionsGranted = blnAllowEntry
                blnPermissionsChecked = True
            End If
        End If
    Next
  
End Sub

Here is the output of the log, while the last Permission Request is displayed

B4X:
iIndex = 1
** Activity (asplash) Resume **
iIndex = 1
iIndex = 2
iIndex = 3

I did fix my issue with a select statement checking the permission type after result, however I would like to know why the above is happening.

This code works

B4X:
Sub Activity_Resume


    Dim blnFineLoc As Boolean = False
    Dim blnCourseLoc As Boolean = False
    Dim blnPhoneState As Boolean = False

    Dim blnFineLocChecked As Boolean = False
    Dim blnCourseLocChecked As Boolean = False
    Dim blnPhoneStateChecked As Boolean = False

    For Each permission As String In Array(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION, Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION, Starter.rp.PERMISSION_READ_PHONE_STATE)
        Starter.rp.CheckAndRequest(permission)
        Wait For Activity_PermissionResult (permission As String, Result As Boolean)
       
        If Result = False Then
            ToastMessageShow("No permission!", True)
            Activity.Finish
            Return
        Else
            Select Case permission
                Case Starter.rp.PERMISSION_ACCESS_FINE_LOCATION
                    GC.GPSClass1.LocalGPS_Start
                    blnFineLoc = True
                   
                Case Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION
                    blnCourseLoc = True
                   
                Case Starter.rp.PERMISSION_READ_PHONE_STATE
                    blnPhoneState = True
            End Select
           
            If blnFineLoc = True And blnCourseLoc = True And blnPhoneState = True Then
                blnPermissionsGranted = True
                blnPermissionsChecked = True
            End If
        End If
    Next

End Sub






Thaks
iCAB
 
Last edited:
Top