In a service, use Timer to get a GPS location every minute ,Running through the a day,I found that the use of Timer does not trigger action by per minute.
Phone in Sleep, Timer does not normal work?:sign0085:
Phone in Sleep, Timer does not normal work?:sign0085:
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 SQL As SQL
Dim gps1 As GPS
Dim pid As PhoneId
Dim domainName As String
Dim servicePath As String
Dim serviceName As String
Dim StrUtils As StringUtils
Dim lastKnownLocation As Location
Dim lastKnownLocationTimeMillis As Long
Dim isGpsFix As Boolean
Dim startTimeMillis As Long
Dim TimerCheckGPS As Timer'每秒钟检查GPS定位结果
Dim TimerFetchGPS As Timer'按时定位
Dim gettingGPS As Boolean
End Sub
Sub Service_Create
Dim s As String
s=DBUtils.CopyDBFromAssets("gpsdb.db")
SQL.Initialize(s,"gpsdb.db",False)
gps1.Initialize("GPS")
lastKnowLocation=Null
lastKnowLocationTimeMillis=0
isGPSFix=False
TimerCheckGPS.Initialize("TimerCheckGPS",1000)'每称钟检查一次定位结果
TimerFetchGPS.Initialize("TimerFetchGPS",1000*60)'一分钟定位一次
domainName = "192.168.7.5:9022"
httpUtils.CallbackActivity="GPSService"
httpUtils.CallbackJobDoneSub="JobDone"
httpUtils.CallbackUrlDoneSub="UrlDone"
' domainName = manager.GetString("key1")
servicePath = "/datasnap/rest/TService/"
serviceName = "http://"&domainName&servicePath
End Sub
Sub Service_Start (StartingIntent As Intent)
If GPS1.GPSEnabled = False Then
ToastMessageShow("Please enable the GPS device.", True)
StartActivity(GPS1.LocationSettingsIntent) 'Will open the relevant settings screen.
Else
TimerFetchGPS.Enabled=True
TimerFetchGPS_tick
'GPS1.Start(1000*60, 0) '参数1:间隔的毫秒,参数2:间隔的米
End If
End Sub
Sub Service_Destroy
gps1.Stop
Log("GPS Stop")
End Sub
Sub GPS_LocationChanged (Location1 As Location)
lastKnownLocation = Location1
lastKnownLocationTimeMillis =DateTime.Now
Log("Latiitude="&Location1.Latitude)
Log("Longitude="&Location1.Longitude)
Log("Time="&Location1.Time)
'Log( "Lat = " & Location1.ConvertToMinutes(Location1.Latitude))
'Log( "Lon = " & Location1.ConvertToMinutes(Location1.Longitude))
Log( "Speed = " & Location1.Speed)
Log("Now="&DateTime.Now)
Log("IMSI="&pid.GetSubscriberId)
'Dim s As String
'DateTime.DateFormat = "yyyy-MM-dd"
's = serviceName&"getMapPosition/"&StrUtils.EncodeUrl(Location1.Longitude, "UTF8")&"/"&StrUtils.EncodeUrl(Location1.Latitude, "UTF8")&"/"&StrUtils.EncodeUrl(DateTime.Date(DateTime.Now), "UTF8")&"/"&StrUtils.EncodeUrl(pid.GetSubscriberId, "UTF8")
'httpUtils.PostString(s, s, ListOfMaps)
End Sub
Sub GPS_GpsStatus (Satellites As List)
Log("进入GPSStatus事件...")
If (DateTime.Now - lastKnownLocationTimeMillis < 1000*3) Then
isGpsFix = True
Else
isGpsFix = False
lastKnownLocation = Null
End If
End Sub
Sub StopCheckGPS
isGPSFix=False
gettingGPS=False
TimerCheckGPS.Enabled=False
gps1.Stop
End Sub
Sub WriteLogDB(LocationState As String)
Dim ListOfMaps As List
ListOfMaps.Initialize
'Dim id As Int
Dim m As Map
m.Initialize
If isGPSFix=False Then
m.Put("Long", 0)
m.Put("Lat", 0)
Else
m.Put("Long", LastKnownLocation.Longitude)
m.Put("Lat", LastKnownLocation.Latitude)
End If
'm.Put("GetTime",DateTime.Now)
m.Put("GetTime",DateTime.Date(DateTime.Now)&DateTime.Time(DateTime.Now))
Log(DateTime.Date(DateTime.Now)&DateTime.Time(DateTime.Now))
m.Put("IMSI",pid.GetSubscriberId)
m.Put("Memo",LocationState)'定位的状态,是否正常定位
ListOfMaps.Add(m)
DBUtils.InsertMaps(SQL, "gpsInfo", ListOfMaps)
End Sub
Sub TimerCheckGPS_tick
Log("正在检查GPS定位信息....")
If gettingGPS Then
If isGPSFix Then
'LastKnowLocation为获取的最新位置
WriteLogDB("正常定位")
StopCheckGPS
Else
'超时
If DateTime.Now-StartTimeMillis>1000*20 Then
WriteLogDB("定位超时")
StopCheckGPS
Log("定位超时!")
End If
End If
Else
gettingGPS=True
startTimeMillis=DateTime.Now
gps1.Start(0,0)
End If
End Sub
Sub TimerFetchGPS_tick
If gettingGPS=False Then
TimerCheckGPS.Enabled=True
End If
End Sub
Sub JobDone(Job As String)
Dim s As String
If HttpUtils.IsSuccess(job) Then
s = httpUtils.GetString(job)
Log(s)
End If
End Sub
Sub UrlDone(Url As String)
Log(Url & "done")
End Sub
Last edited: