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:
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
- Created a B4XPages project
- Copied and pasted manfest settings and starter.bas file
- Added files to new project: tracker.bas and startAtBootReceiver.bas
- Copied following code over to new project
-
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
- Renamed above sub Activity_Resume to CheckAndGetAllPermissions
- 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