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

So your idea is to use Firebase and send a notification just to prevent the app to go in "suspended state" and be killed?


Because if yes, at this point any notification can wake up the application (right?)
In the code there is this command:
B4X:
StartServiceAt(Me, DateTime.Now + 10 * DateTime.TicksPerMinute, True)

That restart the service every 10 minutes.
And in the service start event there is:
B4X:
Service.StartForeground(nid, CreateNotification(" ..."))

So every 10 minutes a notification is created already, i think
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Not quilty! You must be thinking of someone else, I don't do webby stuff (or background stuff for that matter).
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

OliverA

Expert
Licensed User
Longtime User
And it didn't triggger the event Destroy, so nothing you can do (?)
If your goal is to self-start your app locally every nth minute and keep it running for a long time, than my answer is going to be a no. Device manufacturers are trying to improve battery life of their devices. This has nothing to do with the usage of B4A. Neither switching to Kotlin, nor Java directly would solve the issue. One way around this could be contacting the device manufacturer(s) and negotiating with them about letting your app run for a long time (good luck?)

So what is a developer to do? Well, if the devices are connected most of the time, then use an external means of "waking" the app, with FCM being one way to do such.
StartServiceAt(Me, DateTime.Now + 10 * DateTime.TicksPerMinute, True)
Service.StartForeground(nid, CreateNotification(" ..."))
But this is a local notification that your local app generates and since your app was killed, any future notifications that it has set may be killed also. I've not read anywhere here on the forum that the device manufacturers have gone as far yet as to block external messages from waking up an app. The downside of this is that the device has to be connected in order for this work around to work.
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
yes but.. in this way the user will continuously receive notification...

Nah. If you send data messages (yes, there are different fcm message types) then you decide what your app does then. FCM was just an idea.

Android doesn't really like if code runs in the background (this is the reason why there are notifications). Every action should be triggered by the user.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
it's not an easy one @Mike1970. I'm finding that Huawei, Xiaomi, Lenovo etc etc etc that run Android 8+ are a complete pain when it comes to killing my Widgets, oddly enough Samsungs appears to be okay though. Actually my test device Huawei Y7 2019 actually brings up a huge red warning in the notification area warning the user that apps including mine might be trying to keep the device from sleeping my widgets restarts every 5 seconds which updated the screen (timers are not accurate enough to long running periods and and talking about days).

It's a nightmare...
 
Upvote 0

Mike1970

Well-Known Member
Licensed User
Longtime User
So i can send notification and NOT display it to the user??
 
Upvote 0

techknight

Well-Known Member
Licensed User
Longtime User
Its only going to get worse. Starting with Android Q, background apps will no longer be able to jump into the foreground/launch activities anymore. BTW. Just a heads up. This basically points to a sign that in android's future, there will be no more background apps.
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
Have you read this somewhere ?
 
Upvote 0

Jmu5667

Well-Known Member
Licensed User
Longtime User
We have apps running in the background for months on Android 9.0, such as PTT (push to talk), HELLO (corporate whatsapp type). How are you managing your background services peter ?

Regards

john.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
Have you read this somewhere ?

Hello Jmu,
He's 100% correct about that fact, note I said fact.
Android Q places restrictions on when apps can start activities. You can install Android Q (10) on a device like have already done and see it for yourself. There is a work around for it though but it means going into developers options.

BTW when in my state ment above 8 meant to type 9+ and not 8+, and the restrictions are even worse on my Android Q device. My widgets worked fine on 8 and mostly okay on 9 (apart from on Huawei etc devices), but on Q my widgets are a complete nightmare.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…