Android Question Scheduled Services from killed app? [SOLVED]

W. Graf

Member
Licensed User
Longtime User
Good morning,

I have a problem with my app and I think, I have found the origin. But I'm not sure, if this is possible. Maybe you can help me?

My app contains a service, which is scheduled every 10-60 seconds. In most cases, everything is working fine. But rarely, my app crashes when service is starting at scheduled time. I think, that OS kills my app (because of low memory) before my service starts again at scheduled time.
For me, it looks like:
1.) I start my app
2.) Process-global variables are set and service is scheduled with StartServiceAt(...)
3.) OS kills my app (because of low Memory)
4.) My scheduled service starts, but my process-global variables are empty
Is this situation (step 4) possible? I'm testing on Android 4.3, 4.4, 6.0, 7.1.

By the way: do you have an idea, how can I simulate the situation, when Android kills my app from Memory? I want to test my app for this situations before I deliver it to the users? For example: if I go into the app-manager from Android (Settings - App Manager) and stop my app. Is this the same behaviour like Android do it, when memory is running out?

Thank you in advance!!

Best regards,
Wolfgang
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
My app contains a service, which is scheduled every 10-60 seconds.
Newer devices will not let you start a service with such short interval if the process is not currently running. The limit is around 15 - 30 minutes.

But rarely, my app crashes when service is starting at scheduled time.
You need to monitor the logs and find why it crashes.
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Thank you for your response, but these were not my questions.

I think I know, why it crashes (I've already monitored and read many logs). Because of the logs, I have an assumption. To confirm my assumption, I need to know, if Step 4 (see my previous post) is possible...: Is it possible, that OS kills my app, but nevertheless my scheduled Service still starts?
 
Upvote 0

W. Graf

Member
Licensed User
Longtime User
Ok, I have found my answers...

About Question 1 (global variables are lost):
If you schedule a service to start at a specified time (StartServiceAt()) and the OS kills your app, then the scheduled service still starts at the specified time. But in this case, the app itself restarts (Starter.Service_Create(), Starter.Service_Start() will be executed), but the Main-Activity does not start. The global variables were lost and had to be set again (for example in Starter.Service_Create()).

About Question 2 (simulate the behaviour of Android, to kill an app):
Yes you can simulate it. You have to swipe out the app from the recent-app-list. If you do this, then a scheduled service will still start. But if you go in the Android App Manager (Settings - App Manager) and stop your app, then the whole process is completly ended and your scheduled service will NOT start. It seems, that swiping out an app from recent-app-list is between the states "totally stopped" and "running" ...

These are my findings from today. I hope, somebody will help it in future, but be aware of that I'm a beginner in Android-development. So maybe I'm wrong with my conclusions. I have no idea, why somebody has designed an OS with such unpredictable behaviour. Maybe I'm too old for todays OSes ... :-(

I wish you a great day!
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…