Android Question Background service tracker killed

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, i implemented this code in my app to track the position in background:
https://www.b4x.com/android/forum/threads/background-location-tracking.99873/


But after 4 hours it stopped working... what could be the problem? in the "Service_Create" of the tracking i have:

B4X:
Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves
    GPS.Initialize("gps")
    lock.PartialLock

And i put this in the Starter:

B4X:
Sub Service_TaskRemoved
    'This event will be raised when the user removes the app from the recent apps list.
    StopService(Tracker)
End Sub
 
Last edited:

Mike1970

Well-Known Member
Licensed User
Longtime User
You should.
Now i'm tring, it's working good for now (it seems)

I noticed that implemented in an real application the SO says that the process is draining the battery too much, and when there is that notification the Tracking is disabled, i think the two things are connected...
What do you think?

Edit i unlocked the phone and the "Gps in use" icon (and the notification with longitude etc...) are gone.. but no trace of the SO notification.
As soon as i resumed the app from task manager the gps restarted
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
I noticed that implemented in an real application the SO says that the process is draining the battery too much, and when there is that notification the Tracking is disabled, i think the two things are connected...
What do you think?
Keeping the GPS on can drain the battery. Many manufacturers don't allow apps to run too long in the background, even if there is a foreground service. Especially if the app consumes a lot of power.

I don't know what exactly are you trying to implement however it might be better not to use the GPS. You can start your app every hour and check the last known location with FusedLocationProvider.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I already substitute the gps with the Fused location library, i followed the example “GoogleMaps and FusedLocationProvider” keeping only what i need.
And it closes in the same way
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Keeping the GPS on can drain the battery. Many manufacturers don't allow apps to run too long in the background, even if there is a foreground service. Especially if the app consumes a lot of power.

I don't know what exactly are you trying to implement however it might be better not to use the GPS. You can start your app every hour and check the last known location with FusedLocationProvider.


Ok, now i stop the FusedLocationProvider for 5 minutes and i start it the necessary time to trigger the location_changed one time.

I went to sleep while the phone was running the app, but when i woke up the SO killed all the services (starter too), i went to the phone settings and i saw that there was my app running with: 1 process and 0 services
and it should be: 1 process and 3 Services

What wrong is going on?? D:
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
You can switch to using StartServiceAt with 30+ minutes interval and a foreground service that runs for a limited time.
Sorry Erel, i didn't understand well.
I modified yours GPS Tracker example, and in that service there is the "StartServiceAt" already.


B4X:
Sub Service_Start (StartingIntent As Intent)
        Service.StartForeground(nid, CreateNotification("Wait ..."))
        LatRad = 0.0
        LonRad = 0.0

        StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, True)     '<-----

        timerGPSenabling.Enabled = False 'this code set to false the timer that (after getting a location) triggers every 5 minutes, to enable and disable the GPS
        Track
        StartService(gpsChecker) 'This service is a standard service with a timer that every 5seconds checks if the gps is enabled
End Sub

What do you mean I have to do?
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
StartServiceAt will cause the app to start
Yes it was supposed to do this.
But sometime it stain on for 7 hours, other time for an hour, it's random, the process is killed... i don't understand why, i have to try another device and see what happens..

Check your device settings and see whether there is an option to whitelist your app.
I found a funciton named "App Lock" but it's disabled
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
The expected behavior is that StartServiceAt will cause the app to start. It is possible that the OS doesn't let it. Check your device settings and see whether there is an option to whitelist your app.

I tried to add this permissions

B4X:
AddPermission(android.permission.FOREGROUND_SERVICE)
AddPermission(android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS)

i tried to use a sticky-service

But nothing works. the OS still kill all services
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Already implemented... nothing



It means that you might get better results if you don't try to run too long or too frequently.
Yes, but doing this i make the purpose of the app useless..
The app have to stay opened for hours, in order tu track the user location every 5 minute (enabling an disablin the gps)

now i'm not understanding you tip erel :(
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
I don't think that there is anything that you can do to make it run longer. As written above several times, many devices will not let you run too long in the background.
And there is any way to detect if it kills it?
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Just throwing an idea out. If the phone is connected to the network constantly, then use something like Firebase Cloud Messaging to sent a message to the device at a particular interval (in your case every five minutes). This way the app would not have to be running at all and the incoming FCM message would wake up the app, it can do it's processing and just stop again. Some pointers:
https://www.b4x.com/android/forum/t...d-background-data-notification.73356/#content
https://www.b4x.com/android/forum/t...-messages-firebase-cloud-messaging-fcm.67716/
BTW, I'm pretty sure I've read @agraham state that he has no background apps running and does all his processing via FCM. I guess with this type of solution, you would need something that would generate the FCM message(s) at the given interval desired.
 
Upvote 0
Top