Keeping alive Intent Service after application is killed - android

I have intent service in my app. This service has some job to do - uploading some files. It runs only when there is something to do.
Service won't upload when some conditions are met, for example no Internet connection. For that reason it registers itself to on broadcast receiver to receive message about Internet connection changes.
The problem is that service is killed with app even if it is doing something, for example:
App is sending intent to service
Service started uploading something, everything fine
X% uploaded, app is killed, service is killed
Internet connection changed - service is woken up.
If service is woken up after app is killed, why is it killed with the app? Is there any way to prevent killing service when app is killed?
I'm killing app manually. I know android could kill my service anytime and I don't want to prevent it. I just want to have this service running after user closed or killed app.

"It runs only when there is something to do." only theoretically :) - maybe that is you what you want to achieve.
"The problem is that service is killed with app even if it is doing something, for example:"
Of course, there will be cases when the user action will end your Service or Intent service.
This is a fail answer.
"Is there any way to prevent killing service when app is killed?"
It is just watch for "parental control" task protection" keywords in Google!

Because you used an intentService that mean the intentService will destroy once the activty destroy
so you have to use Service instead of intentService, so you can uplaod your file in the backgroud.
According to manipuation between the Service and the activty via broadcast receiver or to bind the service to activty.
Edit :
The Service may be triggered from any thread.
The IntentService must be triggered from Main Thread.

If you don't mind showing notification (in your case, you can for example show notification with upload progress), then in your IntentService (or Service) you can call:
startForeground(int id, Notification notification)
This should prevent killing your service when your application is killed.
From documentation: "You can set this flag if killing your service would be disruptive to the user, such as if your service is performing background music playback, so the user would notice if their music stopped playing."

Related

How do I get my app to send a notification when it is closed?

I have an activity that listens to a udp broadcast and triggers an alarm if a certain threshold is reached. But when I exit the app, I no longer receive the notification. How do i keep the app running in the background so that it will send a notification when the threshold is crossed? I have read a bit on services but I am not sure how the logic would work in a relationship between an activity and service. Should the service always be running, should the service start when the app closes? I want the app to trigger an alarm even if another app is running.
How do i keep the app running in the background so that it will send a notification when the threshold is crossed?
Your activity should start a service, when the user asks for this monitoring to go on. That service would be the one that has the socket and the thread for monitoring for UDP broadcasts. Your activity should also have the means for the user to stop this monitoring, if and when the user no longer wants it.
Bear in mind that your service will not run forever. It can go away at any time, either due to user action or an OS decision. However, it will be available for far longer than will your activity.
Should the service always be running, should the service start when the app closes?
Apps do not "close" in Android.

Service restarts when killing activity process

I have an activity that starts a service in a seperate process via the android:process attributes of my service.
My service's onStartCommand() function returns START_STICkY.
I kill my activity's process through swiping it away via the recent apps button, and the service process restarts.
I want to know if there is a way to keep the service running when the activity process is killed.
I want to know if there is a way to keep the service running when the activity process is killed.
No, there is no way to keep the service running. Because,
Closed by the user via the Running Services screen in Settings.
Closed by the user via a task killer.
Closed by Android to free up memory in times of need.
Keep the service running is BAD idea.
But you can do,
--> you can restart you service using BROADCAST RECEIVER.(when incoming call, BOOT complete, SMS received.. etc, use this depend on your requirement)

Service shouldn't stop when the phone is sleeping, or the activity (for starting the service) is not running

I want to run a service to collect the accelerometer sensor information and it shouldn't stop when the phone is sleep or the activity (for starting the service) is not running.
I have to send start and stop commands to the service from the menu activity.
currently I am using a bundled service in the same process of the activity but the problem is that it gets closed as soon as activity is closed (return key pressed).
I am wondering if I use a separate process it will resume even if there is no bundled activity (when activity is closed).
If not, which service model should I choose?
You are probably looking for startService instead of bindService.
http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29
However, even with startService, there are no guarantees the service will remain running "forever" and "always".
WARNING, the options below will consume a lot of battery.
You can increase the chances the service will not be stopped by changing the priority to startforeground (requires a notification).
While the screen is off, the only way to keep the service "alive all the time" is to use Alarm Manager with an RTC_WAKEUP or ELAPSED_REALTIME_WAKEUP schedules.
Less battery...
Practically speaking, however, without startForeground and just using normal RTC or ELAPSED_REALTIME alarm schedules, your service will run most of the time.
You can create a service in the same process with your application, even if your activities all closed, the app still work because your service still alive until you call stopservice (the system will restart your service automatically when it is killed by system). if your service perform complicated communication with activities , i think you should use remote messenger service. During running of service you can bind to service to send and receive data between service and activities.
For more information of service and communicate to service, you can refer here

how to start a service properly and keep it alive?

I know there are other question with the same topic, but I didn't find an answer to my questions.
my goal is to have a service which works on the background as a location listener, and it won't be stopped when the application is stopped (either by a task killer).
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
now, the first problem is that my application crashes but the service has started, and when I run the application again it works.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
and it won't be stopped when the application is stopped (either by a task killer).
Fortunately, this is not possible. If your user wishes your service to stop, the user can stop the service via a task killer or the Manage Services screen in Settings.
currently, I'm starting the service with startService(Intent) if it the service isn't started already and bind to it using bindService(Intent,ServiceConnection, 0).
Usually, you only use one or the other, not both.
the second problem, is that if I kill my application using advanced task killer, it kills my service as well, although in the Service page it says that the service will be stopped when no bounded clients left and if stopService() or stopSelf() have been called.
No, because you called startService() in addition to bindService().
The service stops when the application is closed by the task manager. If this could not be possible every app would have its own service running without any user control over them. You could start the service at boot up and then when the user uses task manager to close, you could restart the service.

Android Service restarts randomly

I'm trying to write a Service which sits and waits for SMS messages (using a BroadcastReceiver which the server registers) and will take some action depending on the details of the SMS message. I use an Activity to startService() and stopService() for the Service. After I close the Activity, the Service continues to sit there with its state kept and its BroadcastReceiver waiting, as it should... However, I find that over time, randomly, the Service will restart itself. That is, onCreate() and onStartCommand() will be called even when the Service has already been started. This happens sometimes when my phone sits idle overnight...
Can anyone provide insight on why this is (phone requests resources and kills a service?), what I can do to prevent it, or what I can do to prevent the state of the service from being lost?
Thanks in advance.
I don't think you need to start service from activity at all. Have a broadcast receiver that listens to your SMS messages and starts the service. After work is done, service should call stopSelf();
I'll try Service.startForeground()
Android sometimes (when it's low on resources, mem/cpu) removes running services and once it's high on resources again brings them back. This is why you see that your services has been restarted a couple of times during night.

Categories

Resources