Android Question Continous background GPS tracking

Mike1970

Well-Known Member
Licensed User
Longtime User
Hi everyone, i'm coding an app for quite a whille (before the update), and i quitted beacuse the SO kill my foreground process while the app is in background, so after an random period of time it stop working.
I wish to know if there is a way with the new update to prevent the OS to kill my process/service

(I already tried the Erel's example of gps tracking, my phone kill even that after a while.
My phone: Meizu m5c)
 
Last edited:

Brandsum

Well-Known Member
Licensed User
Did you get any log when the service stop working? Is there any other app in your device that runs in the background without any issue?
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Did you get any log when the service stop working? Is there any other app in your device that runs in the background without any issue?

No, any log, the OS kills all the services silently.

For example in settings>Apps>Now i can see what apps/services are running and the time elapsed from opening.

My app there isn't because it was killed after 2/3 hours, but the other apps are still running after 6+ hours at this point
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
it is VERY VERY ANNOYING, i can't do what i want to do, i have to verify if a person is in a determinate place until the users decide to close the app himself.
But if the OS kill the app when it want, so i can't develop my app correctly....
 
Last edited:
Upvote 0

walterf25

Expert
Licensed User
Longtime User
it is VERY VERY ANNOYING, i can't do what i want to do, i have to verify if a person is in a determinate place until the users decide to close the app himself.
But if the OS kill the app when it want, so i can't develop my app correctly....
You can have your service running in the background but you need to create a foreground service and a notification will show the whole time until you or the user decides to kill the device.
Do a search for foreground services.

Walter
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can have your service running in the background but you need to create a foreground service and a notification will show the whole time until you or the user decides to kill the device.
Do a search for foreground services.
This is implemented in the background location tracking example linked above. Not all devices let foreground services to be kept alive for a long time.

Trying adding geofence features: https://www.b4x.com/android/forum/threads/84767/#content
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
OffTopic
show the whole time until you or the user decides to kill the device.
THAT is not a good idea to kill the DEVICE instead of the App. :D
 
Upvote 0

dgoss

Member
Licensed User
Longtime User
This works for me, i run an app that uses Gps for up to 15Hrs a day 6 days a week with no problems.
This is all in a Service

This in Service Create
B4X:
    Service.AutomaticForegroundMode = Service.AUTOMATIC_FOREGROUND_NEVER 'we are handling it ourselves

This in Service
B4X:
Sub Service_Start (StartingIntent As Intent)
    Service.StartForeground(nid, CreateNotification("..."))
    StartServiceAt(Me, DateTime.Now + 30 * DateTime.TicksPerMinute, False)
End Sub

B4X:
Sub CreateNotification (Body As String) As Notification
   Dim notification As Notification
   notification.Initialize2(notification.IMPORTANCE_HIGH)
   notification.Icon = "icon"
   notification.SetInfo("App Title", Body, Main)
   Return notification
End Sub
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
OffTopic

THAT is not a good idea to kill the DEVICE instead of the App. :D
:p:p:p

I've already had one too many beers.

I meant kill the service :D

Walter
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
You can have your service running in the background but you need to create a foreground service and a notification will show the whole time until you or the user decides to kill the device.
Do a search for foreground services.

Walter
It's alrady a foreground service, that part is taken from @Erel example.
It's that the problem...i don't know what to do anymore
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
This works for me, i run an app that uses Gps for up to 15Hrs a day 6 days a week with no problems.
This is all in a Service


Yes, this is the code implemented in Erel's example, right?

The only difference with yours is that I set
B4X:
notification.IMPORTANCE_LOW
instead of
B4X:
notification.IMPORTANCE_HIGH
And that i restart the service every 10 minutes instead of 30
 
Last edited:
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
Trying adding geofence features
Yeah, i discovered that thing yesterday, but my phone killed also that one, but now i realized that maybe i have to start the service as foreground service right? Because the original code doesn't.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
And that i restart the service every 10 minutes instead of 30
This could be part of the problem. You're not expected to restart a service in such a short time.
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
This could be part of the problem. You're not expected to restart a service in such a short time.

I tried with 15minutes, but it was killed anyway.
Now i'm trying with an interval of 30 minutes just in case (but the purpose of the app it's count how many time a person stay in a determinate place, if the precision is +-30min it is very imprecise)
 
Upvote 0
Top