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).
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.
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