I have an app that every 10 seconds makes a list of the wireless networks it can see and does some other stuff including updating a listing on screen of which networks it can see.
I was having the screen refresh every 10 seconds using a timer. Then, I wanted to switch to using a service so my app could keep recording networks even when the activity was paused. So, I set my timer to run every 10 seconds. The app starts up and the event fires once or twice but then it crashes.
I can't imagine why.
Here is my Activity_Create code that used to initialize the timer but now calls StartService(Agent)
Here is the agent:
In Main, TimerCheckRadio_Tick used to do stuff, but to rule out any code itself causing the problem, I commented it out.
Even this crashes after one or two cycles:
I was having the screen refresh every 10 seconds using a timer. Then, I wanted to switch to using a service so my app could keep recording networks even when the activity was paused. So, I set my timer to run every 10 seconds. The app starts up and the event fires once or twice but then it crashes.
I can't imagine why.
Here is my Activity_Create code that used to initialize the timer but now calls StartService(Agent)
B4X:
Sub Activity_Create(FirstTime As Boolean)
SuppressNextSQLLogging = False
tabsInsideMargin = 10dip
wifi.ABLoadWifi() ' start loading wifi info, asynchronously
Activity.LoadLayout("Main")
tabs.AddTab("Status", "TabStatus")
tabs.AddTab("Places", "TabPlaces")
tabs.AddTab("Reminders", "TabReminders")
tabs.AddTab("Settings", "TabSettings")
tabs.CurrentTab = ActiveTab
'-------------- NO UI CODE Above this point --------------
LogToActivity("KurantReminder startup")
' Initialize database
'File.Delete(File.DirInternal, "KurantReminders.db")
If File.Exists(File.DirInternal, "KurantReminders.db") Then
' good
Else
' we have to create it from scratch
CreateDatabase
End If
db.Initialize(File.DirInternal, "KurantReminders.db", False)
LogToActivity("Database initalization complete")
' Set up display
MyVisibleNetworks.Initialize
MyPlaces.Initialize
DisplayAndLoadActivityLog
'TimerCheckRadio.Initialize("TimerCheckRadio", TimerIntervalWhenInactive) ' start slow because resume will start it up
StartService(Agent)
End Sub
Here is the agent:
B4X:
'Service module
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim ServiceStartCount As Int
End Sub
Sub Service_Create
ToastMessageShow("Service_Create", False)
End Sub
Sub Service_Start (StartingIntent As Intent)
ServiceStartCount = ServiceStartCount + 1
ToastMessageShow("Service_Start, #" & ServiceStartCount, False)
CallSub(Main,"TimerCheckRadio_Tick")
ToastMessageShow("TimerCheckRadio_Tick, #" & ServiceStartCount, False)
StartServiceAt("", DateTime.Now + 10*DateTime.TicksPerSecond, False)
End Sub
Sub Service_Destroy
ToastMessageShow("Service_Destroy", False)
End Sub
In Main, TimerCheckRadio_Tick used to do stuff, but to rule out any code itself causing the problem, I commented it out.
Even this crashes after one or two cycles:
B4X:
Sub TimerCheckRadio_Tick
ToastMessageShow("TimerCheckRadio_Tick", False)
' RefreshVisibleNetworks
' DisplayVisibleNetworks
'
' Dim ThisPlace As Place
' Dim p As Int
' For p = 0 To MyPlaces.Size-1
' ThisPlace = MyPlaces.Get(p)
'
' If ThisPlace.Status = "I Am Here" Then
' RecordTheseNetworksAsBeingAtThisPlace(ThisPlace)
' Else
' If ANetworkAtThisPlaceIsVisible(ThisPlace) Then
' ThisPlace.Status = "Here"
' Else
' ThisPlace.Status = "Not Here"
' End If
' End If
'
' Next
'
' DisplayPlaces
' RefreshStatusDisplay
End Sub