Android Question Device doesn't ask for runtime permission

Discussion in 'Android Questions' started by marcick, Jun 8, 2019.

  1. marcick

    marcick Well-Known Member Licensed User

    I have this code for asking runtime permission and it works fine on most of devices.
    On a Huawey tablet (Android 9) it works fine in debug and release, but when the app is downloaded from PlayStore the permission request is not executed. In fact, if I go in setting-app-my app-permission I see that all the permissions are unflagged (of course the app then crash).
    On other devices like my Samsung (same Android 9) the permission request is always executed, also when the app is downloaded from PlayStore.
    Any Idea ?

    Code:
    Sub Activity_Create(FirstTime As Boolean)
        
    If Starter.PermissionOk=False Then
            
    wait for (CheckPermission1) complete (result As Boolean)
            
    If result=False Then
                
    Activity.Finish
                
    Return
            
    End If
            
    wait for (CheckPermission2) complete (result As Boolean)
            
    If result=False Then
                
    Activity.Finish
                
    Return
            
    End If
            
    wait for (CheckPermission3) complete (result As Boolean)
            
    If result=False Then
                
    Activity.Finish
                
    Return
            
    End If
            
    wait for (CheckPermission4) complete (result As Boolean)
            
    If result=False Then
                
    Activity.Finish
                
    Return
            
    End If
        
    End If
        
        
    Log("All permission ok")
        
    File.WriteString(Starter.AppPath,"PermOk","")
        Starter.PermissionOk=
    True
    End Sub

    Sub CheckPermission1 As ResumableSub
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
        
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
        
    Return Result
    End Sub
    Sub CheckPermission2 As ResumableSub
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
        
    Return Result
    End Sub
    Sub CheckPermission3 As ResumableSub
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_COARSE_LOCATION)
        
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
        
    Return Result
    End Sub
    Sub CheckPermission4 As ResumableSub
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)
        
    wait for Activity_PermissionResult(Permission As String,  Result As Boolean)
        
    Return Result
    End Sub
    These the relevant code in the manifest

    Code:
    ......
    CreateResourceFromFile(Macro, FirebaseNotifications.FirebaseNotifications)

    AddApplicationText(
    <meta-data
      android:name=
    "com.google.android.geo.API_KEY"
      android:value=
    "AIzaS.......................hJmc"/>
    )   

    AddPermission(android.permission.ACCESS_NETWORK_STATE)
    AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
    AddPermission(android.permission.READ_PHONE_STATE)
    AddPermission(android.permission.PERMISSION_ACCESS_FINE_LOCATION)

    AddApplicationText(
      <provider
      android:name=
    "android.support.v4.content.FileProvider"
    ......
     
  2. Erel

    Erel Administrator Staff Member Licensed User

  3. marcick

    marcick Well-Known Member Licensed User

    Ok, I'm trying to follow your suggestion but I'm not able to work with this code, the app exit without ask for permission

    This is my (suppose wrong) code:

    Code:
    Sub Process_Globals
        
    Dim rp As RuntimePermissions
    End Sub

    Sub Globals

    End Sub

    Sub Activity_Create(FirstTime As Boolean)
        
    Activity.LoadLayout("Layout1")   
    End Sub

    Sub Activity_Resume
        
    For Each permission As String In Array(rp.PERMISSION_WRITE_EXTERNAL_STORAGE, rp.PERMISSION_ACCESS_FINE_LOCATION, rp.PERMISSION_ACCESS_COARSE_LOCATION, rp.PERMISSION_READ_PHONE_STATE)
            rp.CheckAndRequest(permission)
            
    Wait For Activity_PermissionResult (permission As String, Result As Boolean)
            
    If Result = False Then
                
    ToastMessageShow("E' necessario concedere le autorizzazioni richieste !"True)
                
    Activity.Finish
                
    Return
            
    End If
        
    Next
        
    'we have permission!
        Log("All permission ok")
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)

    End Sub

    Sub Button1_Click
        
    ExitApplication
    End Sub
    And this is my manifest

    Code:
    'This code will be applied to the manifest file during compilation.
    'You do not need to modify it in most cases.
    'See this link for for more information: https://www.b4x.com/forum/showthread.php?p=78136
    AddManifestText(
    <uses-sdk android:minSdkVersion=
    "19" android:targetSdkVersion="26"/>
    <supports-screens android:largeScreens=
    "true"
        android:normalScreens=
    "true"
        android:smallScreens=
    "true"
        android:anyDensity=
    "true"/>)
    SetApplicationAttribute(android:icon, 
    "@drawable/icon")
    SetApplicationAttribute(android:
    label"$LABEL$")
    'End of default text.
    AddManifestText(<uses-permission
    android:name=
    "android.permission.WRITE_EXTERNAL_STORAGE"
    android:maxSdkVersion=
    "19" />
    )
    AddManifestText(<uses-permission
    android:name=
    "android.permission.ACCESS_FINE_LOCATION"
    android:maxSdkVersion=
    "19" />
    )
    AddManifestText(<uses-permission
    android:name=
    "android.permission.ACCESS_COARSE_LOCATION"
    android:maxSdkVersion=
    "19" />
    )
    AddManifestText(<uses-permission
    android:name=
    "android.permission.READ_PHONE_STATE"
    android:maxSdkVersion=
    "19" />
    )
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Where does it come from?

    There is one specific case where you should use maxSdkVersion and it is not relevant here. Delete all of this. Let the compiler add the permissions for you.
     
  5. marcick

    marcick Well-Known Member Licensed User

    .. I don't know :confused::cool::rolleyes:

    Anyway, also deleting thet part in the manifest, the code does not work. The app exits without asking the permissions
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    Click on Logs - List Permissions. Do you see the relevant permissions?
    If you are running the code that you posted above then they will not be listed (because they are not required).

    You can add the permissions with AddPermission.
     
  7. marcick

    marcick Well-Known Member Licensed User

    ok, but looks like the above code does not work correctly

    I addes this to the manifest:

    Code:
    AddPermission(android.permission.WRITE_EXTERNAL_STORAGE)
    AddPermission(android.permission.ACCESS_FINE_LOCATION)
    AddPermission(android.permission.READ_PHONE_STATE)
    As the app is started first time, it asks for the first two permission, then exits with the ToastMessageShow
    I starts it again then it asks for the third permission.

    Also, if I then uncheck the three permissions in Settings-App, the app does not ask them again
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    This is the expected behavior. The user doesn't want to grant the permission.

    Don't use toast messages. Add Log messages to better understand what happens.

    It sounds to me that everything is working correctly. If you think otherwise then please upload the project and explain the steps to reproduce it.
     
  9. marcick

    marcick Well-Known Member Licensed User

    If the user doesn't want to grant the permission, I expect the app inform the user that if it doesn't accept the app can't work.
    I attach here the zip. On my device the app asks the first two (of three) permission, then exits.
     

    Attached Files:

  10. Erel

    Erel Administrator Staff Member Licensed User

    I see the problem.

    First it is better to check it with this code:
    Code:
    Sub Button1_Click
       
    For Each permission As String In Array(rp.PERMISSION_WRITE_EXTERNAL_STORAGE, rp.PERMISSION_ACCESS_FINE_LOCATION, rp.PERMISSION_READ_PHONE_STATE)
           rp.CheckAndRequest(permission)
           
    Wait For Activity_PermissionResult (permission As String, Result As Boolean)
           
    Log(permission & ": " & Result)
       
    Next
    End Sub
    You shouldn't call this code from Activity_Resume. It causes the requests to be made multiple times as the activity is paused and resumed when the permission dialog appears.

    Your best option is to always request the permissions just before you need them.
     
  11. marcick

    marcick Well-Known Member Licensed User

    Ok. I was following this and this
    I'll try as you suggest, thanks
     
  12. Erel

    Erel Administrator Staff Member Licensed User

  13. marcick

    marcick Well-Known Member Licensed User

    I put it in activity create, just after the layout and seems to work, thougth I have to deal now with the starter service that do something that need permission ....
    Anyway the behaviour lokks like this:

    If the user doesn't grant the permissions, everytime the app is started it asks permissions again.
    If the user grants them, then also if he "ungrant" in Settings-app-permission the app does not ask again, it says "permission ok". Better like 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