Android Question [SOLVED] Activity_PermissionResult event is never called

Discussion in 'Android Questions' started by Sandman, May 25, 2018.

  1. Sandman

    Sandman Well-Known Member Licensed User

    I have this code in an activity:

    Code:
    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?
     
  2. JordiCP

    JordiCP Well-Known Member Licensed 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

    Code:
    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: May 25, 2018
    Sandman likes this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    Cleaned up your code a bit:
    Code:
    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.
     
    Sandman likes this.
  4. Erel

    Erel Administrator Staff Member Licensed User

    A more generic version:
    Code:
    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 StringAs 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
     
    JordiCP, Pencil3 and DonManfred like this.
  5. Sandman

    Sandman Well-Known Member Licensed User

    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. ;-)
     
    JordiCP likes this.
  6. JordiCP

    JordiCP Well-Known Member Licensed User

    I only restart myself from time to time :D
     
    Sandman likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice