Android Question Unable to convert MyLocation example from activity-based to B4XPages

toby

Well-Known Member
Licensed User
Longtime User
I was able to compile and run successfully the original MyLocation example without any change. Since B4XPages projects are recommended for new projects, I'm trying to convert this project. What I've done:
  1. Created a B4XPages project
  2. Copied and pasted manfest settings and starter.bas file
  3. Added files to new project: tracker.bas and startAtBootReceiver.bas
  4. Copied following code over to new project
  5. B4X:
    Sub Activity_Resume
        Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
        If HasPermission = False Then
            Log("no permission")
            ToastMessageShow("no permission", True)
            Return
        End If
        Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean)
        If Result Then
            If Starter.phone.SdkVersion >= 34 Then
                'This is only required for the case where the Tracker service is started after boot.
                'In other cases it is considered to be running in the foreground.
                If Starter.rp.Check(Starter.BACKGROUND_LOCATION_PERMISSION) = False Then
                    MsgboxAsync("Please allow app to always allow location services", "")
                    Wait For Msgbox_Result (unused As Int)
                    Dim in As Intent
                    in.Initialize("android.settings.LOCATION_SOURCE_SETTINGS", "")
                    StartActivity(in)
                    'catch the Activity_Resume event to prevent an endless loop if the user denies the permission.
                    Wait For Activity_Resume
                End If
            End If
            StartService(Tracker)
        Else
            ToastMessageShow("No permission...", True)
        End If
    End Sub
    
    Private Sub CheckAndRequestNotificationPermission As ResumableSub
        Dim p As Phone
        If p.SdkVersion < 33 Then Return True
        Dim ctxt As JavaObject
        ctxt.InitializeContext
        Dim targetSdkVersion As Int = ctxt.RunMethodJO("getApplicationInfo", Null).GetField("targetSdkVersion")
        If targetSdkVersion < 33 Then Return True
        Dim NotificationsManager As JavaObject = ctxt.RunMethod("getSystemService", Array("notification"))
        Dim NotificationsEnabled As Boolean = NotificationsManager.RunMethod("areNotificationsEnabled", Null)
        If NotificationsEnabled Then Return True
        Dim rp As RuntimePermissions
        rp.CheckAndRequest(rp.PERMISSION_POST_NOTIFICATIONS)
        Wait For Activity_PermissionResult (Permission As String, Result As Boolean) 'change to B4XPage_PermissionResult if B4XPages project
        Log(Permission & ": " & Result)
        Return Result
    End Sub
  6. Renamed above sub Activity_Resume to CheckAndGetAllPermissions
  7. Renamed Activity_PermissionResult to B4XPages_PermissionResult

During debugging, the execution flow didn't return from this line: Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION).

Could someone kindly tell me what I did wrong? project attached. Should I use the default project type instead for this type of apps?

TIA
 

Attachments

  • MyLocationB4XPages.zip
    16.4 KB · Views: 88

JohnC

Expert
Licensed User
Longtime User
What version of android is on the device you are testing this on?
 
Upvote 0

Mariano Ismael Castro

Active Member
Licensed User
Renamed Activity_PermissionResult to B4XPages_PermissionResult
Permission:
Sub CheckAndGetAllPermissions
    Wait For (CheckAndRequestNotificationPermission) Complete (HasPermission As Boolean)
    If HasPermission = False Then
        Log("no permission")
        ToastMessageShow("no permission", True)
        Return
    End If
    
    Starter.rp.CheckAndRequest(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION)
'    Wait For B4XPages_PermissionResult (Permission As String, Result As Boolean)
    Wait For B4XPage_PermissionResult (Permission As String, Result As Boolean)
    If Result Then
        If Starter.phone.SdkVersion >= 34 Then
            'This is only required for the case where the Tracker service is started after boot.
            'In other cases it is considered to be running in the foreground.
            If Starter.rp.Check(Starter.BACKGROUND_LOCATION_PERMISSION) = False Then
                MsgboxAsync("Please allow app to always allow location services", "")
                Wait For Msgbox_Result (unused As Int)
                Dim in As Intent
                in.Initialize("android.settings.LOCATION_SOURCE_SETTINGS", "")
                StartActivity(in)
                'catch the Activity_Resume event to prevent an endless loop if the user denies the permission.
                Wait For CheckAndGetAllPermissions
            End If
        End If
        StartService(Tracker)
    Else
        ToastMessageShow("No permission...", True)
    End If
End Sub

Hi, here's your mistake.
 
Upvote 3

toby

Well-Known Member
Licensed User
Longtime User
@Mariano Ismael Castro,
Thanks so much for catching that error in my code—I really appreciate you taking the time to point it out. I’ve updated it accordingly, and everything’s working as expected now.

Your help means a lot! 🙏
 
Upvote 0
Top