Service help

anaylor01

Well-Known Member
Licensed User
Longtime User
I have a pedometer app that I want to create a service for. So when the service starts I need it to keep track of the distance and the time. The code below is the code that does this in the app but I can't figure out how to do it in a service. I thought I could just call the sub from the service but that isn't working. Any help would be greatly appreciated. Thanks.

B4X:
Sub GPS_LocationChanged (Location1 As Location)
Dim km As String
If er = "kph" Then
km = " km"
Else
km = " miles"
End If

    latstop = Location1.Latitude
    lonstop = Location1.Longitude
    
    lblLat.Text = "Lat = " & NumberFormat(Location1.Latitude,0,8)
    lblLon.Text = "Lon = " & NumberFormat(Location1.Longitude,0,8)
    lblSpeed.Text =NumberFormat((Location1.Speed * 2.23693629),2,2) & " " & er

    If latstart = 0 AND lonstart = 0 Then
        latstart = latstop
        lonstart = lonstop
        Return
    End If
    Distance =  Distance + GetDistance(latstart,lonstart,latstop,lonstop)
    lblDistance.Text = NumberFormat(Distance,0,3) & km
    latstart = latstop
    lonstart = lonstop
End Sub

Sub GPS_UserEnabled (Enabled As Boolean)
    'ToastMessageShow("GPS device enabled = " & Enabled, True)
End Sub

Sub btnTurnOnGps_click

    If btnTurnOnGps.Text = "Start" Then
    lblavgspeed.Visible = False
    lbltotaltime.Visible = False
    StartService(PedService)
    tmr.Enabled = True
        btnTurnOnGps.Text = "Stop"
        btnTurnOnGps.TextColor = Colors.white
        cols1(0) = Colors.Red
        cols1(1) = Colors.Red
        gd1.Initialize("TOP_BOTTOM",cols1)
        gd1.CornerRadius = 11
        btnturnongps.Background = gd1
        GPS1.Start(0, 0) 'Listen to GPS with no filters.
        latstart = 0
        lonstart = 0
        Distance = 0
  Else
        btnTurnOnGps.Text = "Start"
        btnTurnOnGps.TextColor = Colors.Black
        gps1.Stop
        tmr.Enabled = False
        btnturnongps.Color = Colors.RGB(34, 139, 34)
        lblavgspeed.Text = "Average speed = " & NumberFormat(distance/(avgspeed/60),0,2) & er
        lblavgspeed.Visible = True
        lbltotaltime.Visible = True
        lbltotaltime.text ="Time: " &  NumberFormat(avgspeed/60,0,2)
        avgspeed = 0
  End If
End Sub

Sub GetDistance(lat1 As Double, long1 As Double, lat2 As Double, long2 As Double) As Double
    Dim rlat1, rlong1, rlat2, rlong2, p1, p2, p3, ret As Double
    
    rlat1 = DEGREES_TO_RADIANS * lat1
    rlong1 = DEGREES_TO_RADIANS * long1
    rlat2 = DEGREES_TO_RADIANS * lat2
    rlong2 = DEGREES_TO_RADIANS * long2
    p1 = Cos(rlat1) * Cos(rlong1) * Cos(rlat2) * Cos(rlong2)
    p2 = Cos(rlat1) * Sin(rlong1) * Cos(rlat2) * Sin(rlong2)
    p3 = Sin(rlat1) * Sin(rlat2)
    ret = p1 + p2 + p3
    If ret >= 1 Then
        Return 0
    Else
        ret = ACos(ret)
        ret = (ret * EARTH_RADIUS)
        Return ret
    End If
End Sub
Sub tmr_tick
avgspeed = ".00" + avgspeed + 1
End Sub
 
Top