Android Question Device doesn't ask for runtime permission

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 ?

B4X:
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

B4X:
......
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"
......
 

Erel

Administrator
Staff member
Licensed User

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:

B4X:
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

B4X:
'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" />
)
 

marcick

Well-Known Member
Licensed User
ok, but looks like the above code does not work correctly

I addes this to the manifest:

B4X:
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
 

Erel

Administrator
Staff member
Licensed User
Also, if I then uncheck the three permissions in Settings-App, the app does not ask them again
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.
 

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.
 

Attachments

Erel

Administrator
Staff member
Licensed User
I see the problem.

First it is better to check it with this code:
B4X:
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.
 

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.
 
Top