Android Question Serious malfunction of a timer ?

AlpVir

Well-Known Member
Licensed User
Longtime User
My app records the GPS position to a file every 30 seconds.
This is the beginning of the file (in Italy).
44.386376,6.963991,0,5538,,,08:23:41
44.386376,6.964008,0,5536,,,08:24:11
44.386379,6.964004,0,5528,,,08:24:41
44.386391,6.963748,0,5512,,,08:25:11
44.386445,6.963634,0,5524,,,08:25:41
44.386445,6.963634,0,5524,,,08:26:11
44.386448,6.963608,0,5516,,,08:26:41
44.386453,6.963313,0,5529,,,08:27:11
44.3864,6.962883,0,5519,,,08:27:41
44.38633,6.962685,0,5521,,,08:28:11

As you can see, the recordings follow one another exactly every 30 seconds.
This is the code
B4X:
RegistraOgni=30000
If Not (TimerRegistra.IsInitialized) Then
    T1=Main.RegistraOgni
    TimerRegistra.Initialize("TimerRegistra",T1)
    TimerRegistra.Interval=T1
End If

The same app, however, on another smartphone in Finland, does not behave in the same way.
I have put in brackets the actual interval in seconds which is very often very different from 30 seconds.
This is the beginning of the file (in Finland).
68.913047,27.00802,0,505,,,12:38:46
68.913046,27.00801,0,518,,,12:41:20 (154)
68.913046,27.00801,0,518,,,12:49:50 (510)
68.913046,27.00801,0,518,,,13:09:53 (1203)
68.913046,27.00801,0,518,,,13:20:03 (610)
68.927047,27.04802,0,505,,,13:25:54 (351)
68.927047,27.04802,0,505,,,13:26:24 (30)
68.927047,27.04802,0,505,,,13:26:54 (30)
68.927044,27.04804,0,501,,,13:27:24 (30)
68.927044,27.04804,0,501,,,13:27:54 (30)
later in the file the problem occurs again
68.951205,27.11586,0,557,,,15:25:14
68.951205,27.11586,0,557,,,15:26:38 (84)
68.951205,27.11586,0,557,,,15:37:57 (679)
68.951205,27.11586,0,557,,,15:49:55 (708)
68.921410,27.05074,0,534,,,15:55:01

What could be the causes of this serious malfunction? The cold, the overloaded smartphone, ...?
 

emexes

Expert
Licensed User
Your brain seems to work in a similar way to mine... ?

Oddly enough, it was a BlueVision dolphin, just visible in the background here to left of wizard's head:

1712415896624.png


BTW check out the café owner, tell me if he reminds you of anyone ?

https://web.archive.org/web/2024040...en-arches-bulldoze-hippie-20121018-27tz7.html
 
Upvote 0

bocker77

Active Member
Licensed User
Longtime User
Just a suggestion, why not skip the timer and run a service continuously. I have an app that does that without causing any issues on the OS. If you would like me to post my service code then let me konw. It is getting GPS coordinates.
 
Upvote 0

bocker77

Active Member
Licensed User
Longtime User
Here you go.

B4X:
B4A=true
Group=Default Group
ModulesStructureVersion=1
Type=Service
Version=10.5
@EndOfDesignText@
#Region  Service Attributes 
    #StartAtBoot: False
    
#End Region

Sub Process_Globals    
    Private nid As Int = 1
    Public GPS1 As GPS
    Private Tracking As Boolean
    Private lock As PhoneWakeState
    Dim PES As PhoneEvents
    Dim PID As PhoneId
    Dim bNoNetwork As Boolean
    Private tmr As Timer
    Dim strState As String
    Dim iCount As Int
End Sub
' Get the GPS ball started
Sub Service_Create
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    GPS1.Initialize("gps")
    If GPS1.GPSEnabled = False Then
        ToastMessageShow("Please enable the GPS device.", True)
        StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
    End If
    ' These keep battery usage down
    lock.PartialLock
    lock.KeepAlive(False)
    ' Determine when there is a network change
    PES.InitializeWithPhoneState("PES", PID)
End Sub
' Continuously run the service
Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification)
    Track
End Sub
' Startup GPS
Public Sub Track
    If Tracking Then Return
    GPS1.Start(0,0) 'Listen to GPS with no filters
    Tracking = True
End Sub
' Send GPS coordinates to the main module - the engine that makes everything work
Sub GPS_LocationChanged (Location1 As Location)
    ' Stop sending locations if in the process of loading the List, comparing for markers or no network connection
    If Main.bLoadingCLV Or _
       Main.bComparing Or _
       bNoNetwork Then Return
    CallSub2(Main, "LocationChanged", Location1)
End Sub
' Send a cautionary notification about app's battery usage
Sub CreateNotification As Notification
    Dim n As NB6    
    Dim cs As CSBuilder
    
    Dim bmpSign As Bitmap = LoadBitmapResize(File.DirAssets, "sign.png", 24dip, 24dip, False)
    Dim title As Object = cs.Initialize.Color(Colors.Red).Append("Caution:").PopAll
    Dim Content As Object
    Content = cs.Initialize.Append("This app may drain your battery!").PopAll
    Dim largeIcon As Bitmap = LoadBitmapResize(File.DirAssets, "Small Cartoon.png", 256dip, 256dip, True)
    n.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(bmpSign)
    n.LargeIcon(largeIcon)
    Return n.Build(title, Content, "tag", Me)
End Sub
'ConnectivityChanged - There was a change in the state of the WIFI network or the MOBILE network (other network).
'NetworkType - WIFI Or MOBILE.
'State - One of the following values: CONNECTING, CONNECTED, SUSPENDED, DISCONNECTING, DISCONNECTED, UNKNOWN.
Sub PES_ConnectivityChanged (NetworkType As String, State As String, Intent As Intent)
'    ToastMessageShow("NetworkType: " & NetworkType & CRLF & "State: " & State, True)
    If State = "CONNECTING" Or State = "DISCONNECTING" Then Return
    
    strState = State
    If State = "DISCONNECTED" Or _
       State = "SUSPENDED" Or _
       State = "UNKNOWN" Then
        iCount = 0
        tmr.Initialize("timer", 2000)
        tmr.Enabled = True
        Return
    Else If State = "CONNECTED" Then
        If bNoNetwork Then
            CallSub2(Main, "ConnectionLost", "Reconnected")
            bNoNetwork = False
'        Else If NetworkType = "MOBILE" Then
'            CallSub(Main, "GoingMobi")
        Else
            Return
        End If
    End If
    End Sub
' Determine if the internet connection is really down or just in the process of switching between wifi and mobile
Sub timer_Tick
    If strState = "CONNECTED" Then 
        tmr.Enabled = False
    End If
    iCount = iCount + 1
    If iCount > 5 Then
'        Log("No Network")
        tmr.Enabled = False        
        bNoNetwork = True
        CallSub2(Main, "ConnectionLost", "Disconnected")
    End If
End Sub
' End the service module
Sub Service_Destroy
    If Tracking Then
        GPS1.Stop
    End If
    Tracking = False
    lock.ReleaseKeepAlive
    lock.ReleasePartialLock
End Sub

Even though I have a cautionary note to the user I have never experienced any battery issues while running the app. I have taken long trips across the US with it running with no problems.

I have a toggle button to turn GPS on. I thought this code may help you also so I added it. This in the main module.

B4X:
' GPS Change
Private Sub tglGPS_CheckedChange(checked As Boolean)
    SP.Play(LoadId1, 1, 1, 1, 0, 1)
    ' Start Service that starts GPS
    If checked Then
        If Not(bSpeech) Then
            ' turn speech on
            bSpeech = True
            tglSpeak.SetBackgroundImage(bmpOn)
            SendMsg(4, "Speech' is needed for marker detection")
        End If
        If Not(bWeb) Then
            pnlProtectCLV.BringToFront
        End If
        tglGPS.SetBackgroundImage(bmpOn)
        ProgressDialogShow("Waiting for GPS to initialize...")
        StartService(GPSrtns)
        bGPS = True    
        Return
    ' Stop Service that stops GPS
    Else
        bGPS = False
        tglGPS.SetBackgroundImage(bmpOff)
        StopService(GPSrtns)
        ' turn speech off
        bSpeech = False
        tglSpeak.SetBackgroundImage(bmpOff)
        lblLat.Text = ""
        lblLon.Text = ""
    End If
End Sub

Good luck and I hope this helps you out!
 
Upvote 0
Top