Still struggling here.
According to
https://developer.android.com/topic/performance/vitals/anr: -
An ANR is triggered for your app when one of the following conditions occur:
- Input dispatching timed out: If your app has not responded to an input event (such as key press or screen touch) within 5 seconds.
- Executing service: If a service declared by your app cannot finish executing Service.onCreate() and Service.onStartCommand()/Service.onBind() within a few seconds.
- Service.startForeground() not called: If your app uses Context.startForegroundService() to start a new service in the foreground, but the service then does not call startForeground() within 5 seconds.
- Broadcast of intent: (I don't use this in my app)
- JobScheduler interactions: (I don't use this in my app)
Input dispatching time out I can rule out because the app does not even open when clicked on the icon on an Android 14 phone
There must be something wrong with my Service. I am have now changed from using the standard Service and added this "Starter_Service" module shown below. The Android 14 user says that when they open the app on the phone, there is a blank screen for about 5 or 6 seconds before the Wait/Kill message comes up. It works on an emulator, how do I debug this error short of buying the latest Android phone? Or do I have to convert my app to B4XPages?
[CODE lang="b4x" title="Service"]
#Region Service Attributes
#StartAtBoot: True
#End Region
Sub Process_Globals
Private nid As Int = 1
Private lock As PhoneWakeState
Public rp As RuntimePermissions
Public FLP As FusedLocationProvider
Private flpStarted As Boolean
End Sub
Sub Service_Create
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
FLP.Initialize("flp")
FLP.Connect
lock.PartialLock
End Sub
Sub flp_ConnectionSuccess
Log("Connected to location provider")
End Sub
Sub flp_ConnectionFailed(ConnectionResult1 As Int)
Log("Failed to connect to location provider")
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.StartForeground(nid, CreateNotification("..."))
StartFLP
End Sub
Public Sub StartFLP
If flpStarted Then Return
If Starter.rp.Check(Starter.rp.PERMISSION_ACCESS_FINE_LOCATION) = False Then
Log("No permission")
Return
End If
Do While FLP.IsConnected = False
Sleep(1000)
Loop
Log(flpStarted)
If flpStarted = False Then
FLP.RequestLocationUpdates(CreateLocationRequest)
flpStarted = True
End If
flpStarted = True
End Sub
Private Sub flp_LocationChanged (Location1 As Location)
CallSub2(Main, "Location_Changed", Location1)
End Sub
Private Sub CreateLocationRequest As LocationRequest
Dim lr As LocationRequest
lr.Initialize
lr.SetInterval(10000)
lr.SetFastestInterval(lr.GetInterval / 2)
lr.SetPriority(lr.Priority.PRIORITY_HIGH_ACCURACY)
Return lr
End Sub
Sub CreateNotification (Body As String) As Notification
Dim notification As Notification
notification.Initialize2(notification.IMPORTANCE_LOW)
notification.Icon = "icon"
notification.SetInfo("Tracking location", Body, Main)
Return notification
End Sub
Sub Service_Destroy
If flpStarted Then
FLP.RemoveLocationUpdates
flpStarted = False
End If
lock.ReleasePartialLock
End Sub
[/CODE]
The above service is called in Activity_Resume:-
[CODE lang="b4x" title="Activity"]
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
StartService(Starter_Service)
'Starter.StartFLP
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
[/CODE]