Android Question [SOLVED] - Checking for permissions in B4A results in a continuous loop

rleiman

Well-Known Member
Licensed User
Greetings,

I used some of the code from a tutorial for background location checking in my app and the logs statements get stuck in a loop. This is the tutorial and this is the code based on the tutorial I used in my app.

B4X:
Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)
Wait For Activity_PermissionResult (Permission As String, Result As Boolean)

If Result Then
    Log("We have permission")
Else
    Log("We don't have permission")
End If
I expected a dialog screen to appear asking for the permission since the permission was not set yet. Also the Log displayed is from Result = False. Instead of the screen asking for the permission the Log statement keeps repeating as if it's in a loop.

I initially though something needed to be added to the manifest but the sample app from the tutorial did not have anything added in the manifest editor.

What additional coding to the app or manifest editor should I add to stop the loop?

Thanks.

Screenshot 2020-11-01 at 20.12.52.png
 

JohnC

Well-Known Member
Licensed User
The code you posted should only result in a single pass - no looping.

So, it is looping because of some other code - so please post more code so we can figure out what might be causing the starter.rp.checkandrequest line to be executed repeatedly.
 

rleiman

Well-Known Member
Licensed User
The code you posted should only result in a single pass - no looping.

So, it is looping because of some other code - so please post more code so we can figure out what might be causing the starter.rp.checkandrequest line to be executed repeatedly.
Thanks for the quick response.

I will make a new project and test it first to see if it's doing the same thing.
 

rleiman

Well-Known Member
Licensed User
Thanks for the quick response.

I will make a new project and test it first to see if it's doing the same thing.
Here's what I discovered. First here's the code from a small project using "Default" not pages.

B4X:
#Region  Project Attributes
    #ApplicationLabel: Permission Test
    #VersionCode: 1
    #VersionName:
    'SupportedOrientations possible values: unspecified, landscape or portrait.
    #SupportedOrientations: unspecified
    #CanInstallToExternalStorage: False
#End Region

#Region  Activity Attributes
    #FullScreen: False
    #IncludeTitle: True
#End Region

Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Private xui As XUI
End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_READ_PHONE_STATE)
    Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        Log("We have permission")
    Else
        Log("We don't have permission")
    End If

End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
    xui.MsgboxAsync("Hello world!", "B4X")
End Sub
Having the coding in Activity_Create does not make it repeat but if it's moved into it's own sub routine it repeats so the first part of the problem is solved. The other problem is that the permission dialog screen does not get displayed either in this small app or in my app. I'm not sure why.
 

JohnC

Well-Known Member
Licensed User
The other problem is that the permission dialog screen does not get displayed either in this small app or in my app. I'm not sure why.
My guess is that android thinks the user (you) already gave permission, and that's why it does not display the dialog.

Try changing the package name of the app and see if it will then display the permission prompt.
 
Last edited:

rleiman

Well-Known Member
Licensed User
My guess is that android thinks the user already gave permission, and that's why it does not display the dialog.

Try changing the package name of the app and see if it will then display the permission prompt.
Changing the package name had no effect. Maybe I need to add something in the manifest editor. The Log also still reports False.
 

rleiman

Well-Known Member
Licensed User
Since the first issue was solved then I will mark this post as solved and will start another one for trying to get the permission dialog displayed.

Thanks for all the replies.
 
Top