Android Question Android 12 - Timer ticks not always firing

Jmu5667

Well-Known Member
Licensed User
Longtime User
Hi

Has anyone experienced timer ticks not firing when they should under Android 12. I am currently running some tests on this. This has been noticed in a background service and a class instanced within a long running background service.

Regards

John.
 

DonManfred

Expert
Licensed User
Longtime User
A Timer can only work in a visible activity or an Service or a class called from a service.

Timer does not tick if your activity is in the background. Not related to Android 12...

Probably best is to switch to b4xpages and forget about such issues.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Attached is log data from the app that show when the ticks occur. The tick should occur every 5 seconds, and every 45 seconds a heartbeat (PING) is sent and the response received. There should be 9 tmrhook_tick events between each 'Send Heartbeat'. You can easly see this is not the case. LIne 350 in the attachment is a very good example of this.

Has anyone experience this ?
 

Attachments

  • missing_ticks.txt
    69.1 KB · Views: 94
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Do you have a foreground service?
Yes, I always use a foreground service. This is a summary of the service lifesycle, svc_service

B4X:
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER
    pw.PartialLock
end sub

Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nID,CreateNotification("Atlas PTT","Offline","icon_offline",Main,False,False))
end sub

Sub Service_Destroy
    Service.StopAutomaticForeground
    pw.ReleasePartialLock
End Sub

Within this I call the folllowing:
B4X:
Sub load_channel_listeners

    Dim rs As ResultSet
    ' // 2021.03.12 - only load listeners that we have access too.
    Try
        ChannelListen.Initialize
        rs = Main.SQLDB.ExecQuery($"
                                    SELECT DISTINCT * FROM ptt_channel_listen 
                                    INNER JOIN ptt_users_channels ON ptt_channel_listen.channel_id=ptt_users_channels.channel_id
                                "$)
        Do While rs.NextRow
            Dim c As cls_channel_listen, id As Int
            id = rs.GetInt("channel_id")
            If ChannelListen.ContainsKey(id) Then
                listenChannel_remove(id)
                ChannelListen.Remove(id)
            End If
            
            If Not(ChannelListen.ContainsKey(id)) Then
                ' // create a listen instance
                ChannelListen.Put(id,c) 
                c.Initialize(id)
                c.enabled = True
            End If
        Loop
        rs.close
    Catch
        log($"svc_service::load_channel_listeners() error - ${LastException.Message}"$)
    End Try
    
End Sub

cls_channel_listen has a timer in it with an interval of 5000 ms
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
My guess is that the OS slows down the CPU when your app in the background.
I suspect you are right. Myself and @OliverA looked at the timer lib and the postDelayed method and it all pointed back to the cpu. The app still functions. It is somthing to be aware of I guess when an app is in background.
 
Upvote 0
Top