#Region Service Attributes
#StartAtBoot: true
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
Dim myWifi As ABWifi
Dim MyLocation As Location
Dim MySQL As SQL
Dim n As Notification
Dim GPSf As GPS
Dim GpsTimer As Timer
Dim ScanTime As Int = 0
Private rp As RuntimePermissions
Dim sNotif As Notification
Dim madbicon As Bitmap
Dim sn As NB6
End Sub
Sub Service_Create
madbicon = LoadBitmapResize(File.DirAssets, "nicon.png", 24dip, 24dip, False)
'sn.Initialize("default", Application.LabelName, "LOW").AutoCancel(True).SmallIcon(madbicon)
'sNotif = sn.Build(Application.LabelName, madb.lStrings.Get("search") & "...", Null, Main)
'sNotif.Initialize
'sNotif.Vibrate = False
'sNotif.Light = False
'sNotif.Sound = False
'sNotif.Icon = "icon"
'sNotif.SetInfo(Application.LabelName, madb.lStrings.Get("search") & "...", Main)
'Service.AutomaticForegroundNotification = sNotif
madb.InitLanguage
madb.InitDBs
MySQL.Initialize(File.DirInternal, "madb.s3db", False)
MyLocation.Initialize
If madb.SetupGetValue("finderenabled","true") = "false" And madb.FinderOneRun = 0 Then
Service.StopAutomaticForeground
StopService(Me)
End If
Log("LastVisit: " & madb.SetupGetValue("lastvisit",DateTime.Now))
Log("LastVisited Field: " & madb.SetupGetValue("lastvisitedfield","none"))
End Sub
Sub Service_Start (StartingIntent As Intent)
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_WHEN_NEEDED
n.Cancel(1)
If rp.Check(rp.PERMISSION_ACCESS_FINE_LOCATION) Then
GPSf.Initialize("GPS")
GpsTimer.Initialize("timer1", 1000)
ScanTime = 0
Log("Scanne Visit")
'StartServiceAt("", DateTime.Now + 60 * DateTime.TicksPerMinute, True)
If GPSf.GPSEnabled = False Then
Log("Scanne mit Netzwerkzellen/WIFI")
'ToastMessageShow("Please enable the GPS device.", True)
'StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Dim r As Boolean
r = myWifi.ABGetLocation("myWifi")
'r = myWifi.GetLastKnownLocation("myWifi")
If r = False Then
GPSf.Stop
Service.StopAutomaticForeground
StopService(Me)
Else
'
End If
Else
Log("Scanne mit GPS")
GpsTimer.Enabled = True
'GPSf.Start(1000, 0) 'Listen to GPS with no filters.
End If
'wieder starten im nächsten Intervall
StartServiceAt("", DateTime.Now + ( madb.FinderRescanMinutes * DateTime.TicksPerMinute) , False)
'StartServiceAt("", DateTime.Now + ( 1 * DateTime.TicksPerMinute) , False)
madb.FinderOneRun = 0
Else
Service.StopAutomaticForeground
StopService(Me)
End If
Service.StopAutomaticForeground
End Sub
Sub Service_Destroy
GPSf.Stop
End Sub
Sub myWifi_FoundLocation ()
Dim ResLocation As ABFoundLocation
ResLocation = myWifi.ABGetFoundLocation()
MyLocation.Latitude = ResLocation.Latitude
MyLocation.Longitude = ResLocation.Longitude
madb.MyLocation = MyLocation
FindField ( MyLocation )
End Sub
Sub FindField ( Loc As Location ) As String
'Umfeld
Dim fLat As Float
Dim tLat As Float
Dim fLong As Float
Dim tlong As Float
Dim MyLat As Float
Dim MyLong As Float
Dim Cursor1 As Cursor
MyLat = NumberFormat(Loc.Latitude,0,6)
MyLong = NumberFormat(Loc.Longitude,0,6)
fLat = MyLat - 0.004
tLat = MyLat + 0.004
fLong = MyLong - 0.004
tlong = MyLong + 0.004
'sql
Cursor1 = MySQL.ExecQuery("SELECT * FROM airfields Where Latitude between " & fLat & " and " & tLat & _
" and Longitude between " & fLong & " and " & tlong & " Order By name" )
'go
Log("Finder found: " & Cursor1.RowCount)
If Cursor1.RowCount = 1 Then
Cursor1.Position = 0
'Checke ob selber platz innerhalb 10 Stunden
If madb.SetupGetValue("lastvisitedfield","0") = Cursor1.GetString("id") And madb.SetupGetValue("lastvisit", 0 ) > ( DateTime.Now - (DateTime.TicksPerHour * 10)) Then
Log("Always logged")
Service.StopAutomaticForeground
StopService(Me)
Else
'n.Initialize
'n.Icon = "icon"
'n.Sound = True
'n.Vibrate = False
'n.Light = True
'n.AutoCancel = True
'n.SetInfo2(madb.lStrings.Get("field_visit_notification"),Cursor1.GetString("name"),Cursor1.GetString("id"),field)
'n.Notify(1)
'Notification new mit NB6
Dim noti As NB6
'madbicon = LoadBitmapResize(File.DirAssets, "nicon.png", 24dip, 24dip, False)
noti.Initialize("default", Application.LabelName, "DEFAULT").AutoCancel(True).SmallIcon(madbicon)
n = noti.Build(Cursor1.GetString("name"), madb.lStrings.Get("field_visit_notification"), Cursor1.GetString("id"), field)
n.Notify(1)
'merken
madb.SetupSaveValue("lastvisit",DateTime.Now)
madb.SetupSaveValue("lastvisitedfield",Cursor1.GetString("id"))
madb.SetupSaveValue("lastvisitedname",Cursor1.GetString("name"))
Log("Visit Notification erzeugt")
Service.StopAutomaticForeground
StopService(Me)
End If
Else
Service.StopAutomaticForeground
StopService(Me)
End If
Cursor1.Close
End Sub
Sub GPS_LocationChanged (Location1 As Location)
MyLocation = Location1
GPSf.Stop
madb.MyLocation = MyLocation
Log("GPS Standort gefunden")
FindField ( MyLocation )
End Sub
Sub timer1_tick
ScanTime = ScanTime + 1
Log("Finder: " & ScanTime)
If ScanTime > madb.FinderTryGPSSeconds Then
GPSf.Stop
GpsTimer.Enabled = False
ScanTime = 0
Dim r As Boolean
Log("Kein GPS gefunden, versuche Netzwerkstandort")
r = myWifi.ABGetLocation("myWifi")
'r = myWifi.GetLastKnownLocation("myWifi")
If r = False Then
Service.StopAutomaticForeground
StopService(Me)
Else
'StopService(Me)
End If
End If
End Sub
'Return true to allow the OS default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
Return True
End Sub