Android Question startserviceat with startserviceforeground

josejad

Expert
Licensed User
Longtime User
Hi everybody¡¡

I'm triying to do an app to track our employes during working hours.
I've tested succesfully the FusedLocationProvider example in a service, and I can schedule it with startserviceat (some strange thing happend sometimes, but that's another story).

The point is I want the workers know when they are being tracked. I mean, we want a notify icon while the location service is active.
If I put the notification code and startserviceforeground in the same service that fusedlocationprovider service, I get the notify icon just while the fusedlocation locating is happening (less than a second)

If I have a service with the notify icon, start it foreground and call the fusedlocation service from this service, I just get the position one time (I've tried the startserviceat in both services, with same result)

Service1:
B4X:
Sub Service_Start (StartingIntent As Intent)
    Notification1.Sound = False
    Notification1.SetInfo("My Company", "Tracking", Main)
    Service.StartForeground(1, Notification1)
    StartService(LocationService)
    '******I've tried the next line here and in LocationService too
    StartServiceAt(LocationService,DateTime.Now + (5 * DateTime.TicksPerMinute),True) '(60 * DateTime.TicksPerMinute)
    Service.StopAutomaticForeground 
End Sub

Thanks¡¡
 

josejad

Expert
Licensed User
Longtime User
Thanks Erel, I tried that way too, but then I don't get the "tracking" icon always showing while the service is scheduled.

I tried this way (and some others)

LocationService:
B4X:
Sub Service_Create
    FusedLocationProvider1.Initialize("FusedLocationProvider1")
    FusedLocationProvider1.Connect
End Sub

Sub Service_Start (StartingIntent As Intent)
    Notification1.Initialize
    Notification1.Sound = False
    Notification1.SetInfo("My Company", "Tracking", Main)
    Service.StartForeground(1, Notification1)
    'I've tried next line in FusedLocationProvider1_LocationChanged too
    StartServiceAt(Me,DateTime.Now + (0.1 * DateTime.TicksPerMinute),True) '(60 * DateTime.TicksPerMinute)
    'Service.StopAutomaticForeground
End Sub

Sub FusedLocationProvider1_LocationChanged(Location1 As Location)
    Log("FusedLocationProvider1_LocationChanged " & DateTime.Time(DateTime.Now))
    LastLocation=Location1
    Starter.ListaLoc.Add(DateTime.Time(LastLocation.Time)&" ("&LastLocation.Latitude&", "&LastLocation.Longitude&")")
    Starter.kvs.Put("Listado", Starter.ListaLoc)
    CallSub(Main, "UpdateUI")
    'StartServiceAt(Me,DateTime.Now + (0.15 * DateTime.TicksPerMinute),True) '(60 * DateTime.TicksPerMinute) es una hora
    'StopService("LocationMonitor")
    'Service.StopAutomaticForeground
End Sub

Sub Service_Destroy
    FusedLocationProvider1.Disconnect
    StopService(Me)
End Sub

In main
B4X:
Sub BtnStartStop_Click
    ServiceState=  Not(ServiceState)
    If ServiceStateThen
        StartService(LocationService)
        ToastMessageShow("Service started", False)
    Else
        StopService(LocationService)
        ToastMessageShow("Service stopped", False)
    End If
End Sub
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Thanks Erel. That's the problem. If I divide the services, I get the icon, and I can start/stop the service with icon with no problem. When I put them togheter, I've got no icon (or it appear so quickly I can't see it), but I want it remains until I stop the service from main activity.
The interval is too small for testing (to see if the service is running and I get location updates), when it works, interval will be 30 minutos or so.
I will keep testing, thanks¡¡
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
No way, a lot of test, and a lot of changes but I can't get it working.
Even rigth now in logs I get:
** Service (locationmonitor) Start **
** Receiver (locationmonitor) OnReceive **
but I get no updates.

Attached a small project. Thanks againd for your help
 

Attachments

  • LocationTest.zip
    47.3 KB · Views: 273
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
1. Note that the call to Service.StopAutomaticForeground is important.
2. You should set the notification icon with: Notification.Icon = "icon"
3. The actual problem seems to be in the way you request locations. Try it with this code:
B4X:
Sub FusedLocationProvider1_ConnectionSuccess
   Log("FusedLocationProvider1_ConnectionSuccess")
   Dim LocationRequest1 As LocationRequest
   LocationRequest1.Initialize
   LocationRequest1.SetInterval(0)   
   LocationRequest1.SetPriority(LocationRequest1.Priority.PRIORITY_HIGH_ACCURACY)
   Dim LocationSettingsRequestBuilder1 As LocationSettingsRequestBuilder
   LocationSettingsRequestBuilder1.Initialize
   LocationSettingsRequestBuilder1.AddLocationRequest(LocationRequest1)
   FusedLocationProvider1.CheckLocationSettings(LocationSettingsRequestBuilder1.Build)   
   FusedLocationProvider1.RequestLocationUpdates(LocationRequest1)
End Sub
 
Upvote 0
Top