how to start a service properly and keep it alive? - android

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.

Related

Android Foreground Service Crashed

I have been facing issues with Android service and their restart behavior. At present i have a Service (SERVICE 1) and a thread which is used to move files from one folder to the other. This works great.
But when I am in my app and start the moving service, and i swipe the app away from recent tasks(systemui.RecentTasks) screen the service is stopped with a log "Scheduling restart of crashed service". This service is a foreground service with an active notification.
I do have another service (SERVICE 2) which is also a foreground service with notification. This service also creates a thread to run a background operation. But when I swipe the app this service is not killed and there is not log from ActivityManager.
The final scenario is when the SERVICE 1 stopped frequently from recent tasks within small intervals , again and again, the SERVICE 2 is also stopped now with the same log. I can understand this, as the system detects frequent crash from this app, it tries give less priority to it even if it has foreground service.
My question is why this strange behavior. Both are running in foreground and with an active notification. why does the SERVICE 1 Is stopped and SERVICE 2 is not? I don't find enough resources of service restart behavior when it is crashed. Also i don't find information as how the android system manages to schedule a restart. FYI SERVICE 1 returns START_REDELIVER_INTENT and SERVICE 2 returns START_STICKY.
Kindly help me with this issue. Please. Thanks

Keep a Service started at BOOT running even if Application stopped

I am working on a app that during boot time starts an activity that logs in to my server (needs an activity to log in through facebook) using a service (initiated with startService). This service establishes XMPP listeners and does nothing after that, just waits for connection. I want this service to run all the time the device is up.
The problem is that the activity stops after a while and my service is also stopped. The service returns START_STICKY so I was expecting it to hang around. Also the service doesn't do anything except wait for connection.
The activity has the properties:
android:excludeFromRecents="true"
android:noHistory="true"
android:launchMode="singleInstance"
so that it does not show up in the task list (when user long presses the home button).
The activity is stopped when the user long presses the home button and the service also ends. I am thinking its possible that the application exited, that's why the service also ends. I could not find any way to keep the activity from not stopping. Maybe its stopping because of the above properties.
So what can I do to keep the service running all the time. How can I keep the application from being removed. I read somewhere that if I keep a while loop running in the service then START_STICKY can keep the service around??
I can use AlarmManager to start the service but I don't want it to stop easily and then have to restart it every time.
I don't want to run a foreground service. I can not run the service in a different process since I am using existing code that does not do IPC. Any help will be appreciated. Thanks.
There are two things to keep running a service indefinitely; create the service using startService() and return START_STICKY from onStartCommand(). You seem to be doing this both. With these two steps, the service may be shut down by the system but it should restart almost immediately.
The only suggestion I have is to create a separate thread in the service. This is because by default, started services run in the application main thread. If the service is constantly doing certain task, it may block the main thread and kill the application. Google doc has an example of implementing this:
http://developer.android.com/guide/components/services.html#ExtendingService

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

Bind unbound Services in Android

I'm developing a Android Service. I would like the service to run even when the application not is active. So I start it without binding it:
startService(new Intent(Service.class.getName()));
Now it will run continuously until I choose to stop it, right?
If I, from another activity, bind the service will it stop when I unbind it?
Not necessarily. However, you have to remember that if there is memory pressure, it may be killed depending on the priorities (and if it is unbound, any visible app will probably have higher priority). The lifecycle is described here:
http://developer.android.com/reference/android/app/Service.html#ProcessLifecycle
When the last client unbinds from the service, the system destroys the service (unless the service was also started by startService()).
It mean that your service will not stop even you unbind service from all activity in case of you had started service as startService()
For more details about bound service follow bellow link
Bound services

Categories

Resources