I have a doubt in Android services. I wrote a simple application with a service that runs continuously and upload data to server. My doubt is that will the service stop when I receive a phone call? Will it run in background when I make phone call or will it stop. Please give me an answer.
Thank you.
Activities stop, however services do not.
http://developer.android.com/guide/components/services.html
Services run on the background no matter what happen on the screen. It only stops if you stop it using stop or exit function of its main application or you stop the services in Settings.
Cheers,
The short answer is no. Unless the available phone memory was already low to begin with because you have a lot of apps running in the background, only then might Android forcibly close the service to accommodate the phone call.
Related
1. I am trying to make a timer which will run even when the app is killed. I have found a solution to use a service. But the service restarts every time i kill the app.
According to my research the workaround is to put the service in global process, i have tried that but does not work for me.
I am using android studio 2.0 and API 23 as a target.
Even if i manage to get the timer working in background, it will still stop if the device is turned off or in case of dead battery, so would it be a good idea to run the timer on server?
Please help.
Running timer in service makes battery low very fastly. I dont know your requirement but you can use datestamp according to device time.
I am developing an android application related to gps. that is sending location information periodically to the server. sometime i am getting process killing issue in some devices.
if my application running in foreground then its working file. but while we minimize app and move it to back or once phone going to sleep then app automatically clearing all application data and variables. and then when we tried to resume activity back, it showing blank information and generating exception.
how to prevent clear app data while app running in backgtound? i want to keep this information as it is. this issue only arise in some devices not all.
i have also tried research on Google, but not getting any good solution.
Please Help me. Thank you.
You can't do it like that. If you want your application to keep sending data to a remote service even if the app is in the background, you need to do it a long running Service. Keep in mind that a service runs by default in the main thread, so you need to run your comms with the server in a background thread (however you like it, asynctask, wtv). Then, to really make sure Android doesn't kill your service if it's running low on memory, you need to set your service as a foreground service with a notification.
That way, even if your app is sent to the background you're sure Android the communication will continue.
I am trying to make an app which has got background service feature.But i need it to run continously till app is uninstalled.How can i achieve it using service.Can anyone help me out.Thanks in advance.
You can't do these kind of assumptions on Android. Your app and related service might be killed for whatever. You can't guarantee that.
Read the section on "Creating a started Service" here in Services
Once a service is created, it will continue to run until it stops itself, or is stopped by the system. Why do you need it to run forever?
I have started a service from my application and from that service a worker thread is started .I want my service to run even application goes background and until the user kills/exits the application.
But some cases my service got killed due to low memory ,then used sticky service or making the app to foreground to restart the service.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method, but in this case how we can control that thread.
Please let me know is it the right approach ,and is this usecase achievable
I want my service to run even application goes background and until the user kills/exits the application.
This is not possible. The user can always get rid of your app, via Force Close in Settings, or via some device's version of the recent-tasks list.
But some cases my service got killed due to low memory
No, your process is terminated for low memory.
My issue is I dont want to lose the data between service ending and restarting time ,so is it possible to start another thread from service ondestroy method
No, because your process is being terminated.
Please let me know is it the right approach
Probably not. Very few apps need a service that runs constantly, which is why Android, and its users, go to great lengths to control such services. I would recommend that you try to find some solution to whatever your problem is that does not need a service running constantly.
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.)