Android Question [SOLVED] Activity_PermissionResult event is never called

Sandman

Expert
Licensed User
Longtime User
I have this code in an activity:

B4X:
Sub theButton_Click

    ' Have we permission to use camera?
    Private rp As RuntimePermissions
    Private havePermission As Boolean = True

    If Not(rp.Check("android.permission.CAMERA")) Then
        Msgbox("We need this permission because reasons so press Allow.", "Important info")
        rp.CheckAndRequest("android.permission.CAMERA")
        Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
    End If

    ' Exit early if permission wasn't granted
    If Permission = "android.permission.CAMERA" And Not(havePermission) Then
        ToastMessageShow("Sorry, can't continue, press button to try again.", True)
        Return
    End If

    ' We have the permission so go ahead
    cameraCodeGoesHere

End Sub

(I'm using rp.Check because I want to explain the permission a little better to the user before actually requesting it.)

The problem is that the Activity_PermissionResult event is never raised. (I've also tried this as its own sub, not using Wait For.) It's really strange, because in another place in the app I request permission to use the GPS, and that works just as expected.

I'm sure I've just made a blunder somewhere, but I just can't seem to find it. Can someone spot the problem?
 

JordiCP

Expert
Licensed User
Longtime User
I have just tested it for curiosity and also doesn't work for me. So I don't know the reason but found a simple & curious workaround

B4X:
Sub theButton_Click

    ' Have we permission to use camera?
    Private rp As RuntimePermissions
    Private havePermission As Boolean = True

    If Not(rp.Check("android.permission.CAMERA")) Then
        Msgbox("We need this permission because reasons so press Allow.", "Important info")
        rp.CheckAndRequest("android.permission.CAMERA")
        ' Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)   '<- don't do this
       Wait for sendMeASignalThatAllowsMeToStopWaiting<-- do that
    End If

    ' Exit early if permission wasn't granted
    If Permission = "android.permission.CAMERA" And Not(havePermission) Then
        ToastMessageShow("Sorry, can't continue, press button to try again.", True)
        Return
    End If

    ' We have the permission so go ahead
    cameraCodeGoesHere

End Sub

Public Sub Activity_PermissionResult (Permission As String, Result As Boolean)
    CallSub(Me,"sendMeASignalThatAllowsMeToStopWaiting")    'It throws the signal, and seems to be enough for the 'Wait for'
    ' ... your other code here
End Sub

--EDIT--
I correct myself and find that it works sometimes but not others, with no apparent reason :confused:
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Cleaned up your code a bit:
B4X:
Sub Activity_Click
   Private rp As RuntimePermissions
   If Not(rp.Check(rp.PERMISSION_CAMERA)) Then
       MsgboxAsync("We need this permission because reasons so press Allow.", "Important info")
       Wait For Msgbox_Result (Result As Int)
       rp.CheckAndRequest(rp.PERMISSION_CAMERA)
       Wait For Activity_PermissionResult (Permission As String, havePermission As Boolean)
       If havePermission = False Then
           ToastMessageShow("Sorry, can't continue, press button to try again.", True)
           Return
       End If
   End If
   Log("We have permission")
End Sub
Works fine here.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
A more generic version:
B4X:
Sub Activity_Click
   Dim Explanation As String = "Need permission to take pictures"
   Wait For (RequestPermissionWithExplanation(rp.PERMISSION_CAMERA, Explanation)) Complete (Success As Boolean)
   If Success = False Then
        ToastMessageShow("Sorry, can't continue, press button to try again.", True)
       Return
   End If
   Log("start camera")
End Sub

Sub RequestPermissionWithExplanation (Permission As String, Explanation As String) As ResumableSub
   If Not(rp.Check(Permission)) Then
       MsgboxAsync(Explanation, "your title")
       Wait For Msgbox_Result (Result As Int)
       rp.CheckAndRequest(Permission)
       Wait For Activity_PermissionResult (Permission2 As String, HavePermission As Boolean)
       Return HavePermission
   End If
   Return True
End Sub
 
Upvote 0

Sandman

Expert
Licensed User
Longtime User
Works fine here.

Yep, it sure did, thanks. I tried my code again to figure out what my mistake was, and to my surprise it works just fine now without any changes. I do know that I did restart the phone between posting this thread and testing now, so that's the only thing I can think of. It's kind of simple to forget that the phone is just a computer...

I'm guessing @JordiCP also needs to restart his pocket computer. ;-)
 
Upvote 0
Top