I'm building an application on Android Nougat, which will work constantly (in active mode and in a doze mode (when phone is sleep)).
The problem is that after about 6 hours of inactivity Android stops the application.
There is a UI with activity and a service. The service uses bluetooth, network and recording to a local database. Approximately once per minute, bluetooth works and the result is recorded in the local database.
After that, the data in the database is sent over the network.
The service runs for 6 hours and after that its work stop. The notification is displayed but the service does not work.
The manager.GetRunningServices method also indicates that the service is started but there are no signs of service.
I tried everything you can:
The service uses the method StartForeground
used wakeLock - does not help
used the whitelist - prolongs for a couple of hours, but in the end, all the same, the service stops working
Used the AlarmManager. It works constantly, but the code in OnReceive indicates that the service is running, but in fact it is not.
I started the service in a separate process so that he would use as little memory as possible, but this also does not help.
Maybe someone has ideas about this?
Thank you.
you can restart the service after 6 hours using the job scheduler. periodically restart your app around 6 hours.
Related
I am developing a flutter application, however I would like a service to be able to run constantly without stopping in order to make an api request every 15 minutes and then send a notification to the user (Android /IOS). I would also like the service to start automatically with the smartphone. I've been stuck on this for more than a week now and I've been browsing the forums looking for a solution but I can't find what I'm looking for. Thank you in advance for any help
You don't do it like that on Android. You cannot count on an application not being killed in the background. Instead, you use JobScheduler or WorkManager to set an alarm and wake you up every so often to perform whatever job you need. These methods can also ensure you're scheduled at startup of the phone.
Also, 15 minutes may or may not happen- Doze mode may cause your app to be delayed and make requests less frequently than that if the phone goes to sleep (although 15 minutes is fairly safe, plus or minus a few).
I have written an VPN using android's VPNService and it works perfectly. When I run it, it creates a foreground service and sends all traffic through my VPN server. It also has an internal reconnecting mechanism to reconnects VPN server if it disconnects for any reason without stopping service itself.
I like to have this VPN service working all the time. But my problem is that this VPN service is stopped occasionally after a completely random period(sometime it takes just 10 minutes, but other times it works for 2-3 days before stopping).
Since the stopping time is completely random and I cannot find any place in code that creates this situation (I have been debugging for weeks), I thought maybe android OS itself stops my VPNService for some reason. I wonder if there is a way to detect if system has stopped my service from outside or not. Any idea?
Unfortunately, Android OS still can terminate the service in low memory and possibly other situations, even if it's a background running service !
It is not only Android, but all Mobile Operating Systems optimize RAM usage by killing background apps.
This is done so that the foreground app can be given top priority. It ensures smooth functioning of the current app and reduces load on the system.
There's are two approaches as mentioned in this post: Background Service getting killed in android
If you are implementing the service, override onStartCommand() and
return START_STICKY as the result. It will tell the system that even
if it will want to kill your service due to low memory, it should
re-create it as soon as memory will be back to normal.
If you are not sure 1st approach will work - you'll have to use
AlarmManager
http://developer.android.com/reference/android/app/AlarmManager.html
. That is a system service, which will execute actions when you'll
tell, for example periodically. That will ensure that if your service
will be terminated, or even the whole process will die(for example
with force close) - it will be 100% restarted by AlarmManager.
I had this issue previously and I've solved it by creating the service running forever even if it's killed manually or from the system it recreates itself.
Not sure if anyone would be able to help out, I have an application that scans and interacts with Bluetooth devices, the application works fine however I'd like to utilise Headless JS background services so that the scanning and interactions work at all times.
The process which I'd like to replicate as a service does the following
scans for Bluetooth devices
when a device is found commands are sent and data is received
the app processed the data and posts it up to the API
the app continues to scan and wait for the next device
I'd like to know how I could manage such a service so that it complies with the following
I would like to ensure the service is always running and there is only 1 instance of it running at any time.
The service will need to be able to start straight away or start as soon as the app is out of memory(i.e. keep a foreground instance of the code running and switch to the background service once that's unreachable).
Any advice on how to set up the scheduling of services would definitely help out!
I developed a VOIP app on Android which is working pretty good since we changed from GCM to our own push service by having a sticky background service running with a TCP connection. I made my own solution instead of GCM because realtime pushes are not really working.
Like I said normally everything works pretty nice and my service works most of the time but there is one big problem I can't seem to solve: When somebody uses games or apps with a lot of RAM usage my service gets killed and killed over again(it gets restarted through a alarm which detects it is not running anymore).
So how am I supposed to have the service running with a ongoing connection when the system kills me all the time? The only thing I can think about is to have a very fast Alarm like checking all 15s if my service is still running which seems like a bad idea.
In several apps we use local services to (for example) record locations. This is working perfect for some time (e.g. 4 hours or 150km) and then Android closes (no longer wants) the service. The service is not crashing it's Android that decides to close the service.
It's written in the docs and it's ok BUT I need to find out that a service has been closed. How can I do that?
I would like to restart our service as soon as possible. How do you guys find out that a service has been closed by Android? Do you use finalize?
Thanks in advance.
Have you tried to return Service.START_STICKY or Service.START_REDELIVER_INTENT from Service.onStartCommand() method?
Your service's onDestroy method should be called as it is destroyed by Android OS. Also you should see in logs that normally android re-boots your service after a short time period, which is shown in the logs as it is destroyed, in my experience its around 15-30 seconds before it is booted again. (Every service you have in your app will be booted in a random order so beware if some services rely on other services in your app.)