Android Question Handle multiple permission request

Discussion in 'Android Questions' started by Shay, Jun 29, 2018.

  1. Shay

    Shay Well-Known Member Licensed User

    I have in my app multiple permission requests, such as:
    rp.CheckAndRequest(rp.PERMISSION_READ_CONTACTS)
    rp.CheckAndRequest(rp.PERMISSION_READ_CALL_LOG)
    rp.CheckAndRequest(rp.PERMISSION_CALL_PHONE)

    I wish that app will start only if the above will approve (since no point opening the app without it)
    what is the best way to handle such
     
    superkan likes this.
  2. BillMeyer

    BillMeyer Well-Known Member Licensed User

    You could possibly use this:

    Code:
    Sub Activity_PermissionResult (Permission As String, Result As Boolean)
       
    If Permission = rp.PERMISSION_ACCESS_FINE_LOCATION Then
         gmap.MyLocationEnabled = Result
       
    ELSE
            
    ' Credit for the following code to DonManfred
             rs= Msgbox2("You have not given permission - I can not proceed"    ,"Permission Failure","Yes","Cancel","No",Null)
            
    If rs=DialogResponse.POSITIVE Then
                 
    StopService(Starter)
                
    Activity.Finish
                
    ExitApplication
                
    Return True ' RETURN TRUE to consume the Event---
       End If
    End Sub
    Example discussion here https://www.b4x.com/android/forum/threads/app-not-closing.92401/#post-584361

    I have not tested - but I have faith that it will work.

    Enjoy !!
     
  3. Shay

    Shay Well-Known Member Licensed User

    Yes, but this handle 1 permission, how do I handle multiple?
     
    superkan likes this.
  4. BillMeyer

    BillMeyer Well-Known Member Licensed User

    Multiple If Statements.

    Process --> Check and Request --> Permission Result --> If statement --> Return --> Check next permission --> Repeat Process.

    Let's presume you have 3 permissions.

    Check the first one - user accepts - so it's OK

    Check the second one - user accepts - so it's OK

    Check the third one - user does NOT accept - so app does not execute.

    Enjoy !!
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    Edit: don't put this code in Activity_Resume. Request the permissions right before you need them.
    Activity_Resume can be called multiple times during the requests and it can cause some requests to fail.

    Code:
    Sub Activity_Resume
       
    For Each permission As String In Array(rp.PERMISSION_READ_CONTACTS, rp.PERMISSION_READ_CALL_LOG, rp.PERMISSION_CALL_PHONE)
           rp.CheckAndRequest(permission)
           
    Wait For Activity_PermissionResult (permission As String, Result As Boolean)
           
    If Result = False Then
               
    ToastMessageShow("No permission!"True)
               
    Activity.Finish
               
    Return
           
    End If
       
    Next
       
    'we have permission!
    End Sub
     
    Last edited: Jun 12, 2019
  6. Shay

    Shay Well-Known Member Licensed User

    Great thanks, b.t.w I saw app's that created layout just for the permissions as first screen, which I think good approach
     
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