My application consists of one activity which creates a service. I want the service to be keep running as long as application is running. I know:
It is not guaranteed as Android system can kill activity in low memory conditions and if activity is in background.
The service can be stopped (and killed) by system.
If I bind service to the activity, the activity would get notification in case service is being stopped or started. However, the service may stop running if activity goes in background (onStop()). Please correct me if I am wrong here.
If I bind to service in onResume() of activity and unbind() in onStop(), it might happen that service stops running when my application goes in background. If I bind in onCreate() and unbind() in onDestroy() of activity, would it mean that my activity will keep getting notification from service even when in background.
What is the best way to keep service running and get notification from service to Activity as long as application is running. Please note that there is just one activity in the application so sending activity in background means application goes in background.
Thanks
true
true, but its more rare if us use startForeground()
The service usually won't stop until all activities have unbound. But when the last has, it will. So u can prevent the service from dieing when going to background, if you only unbind in onPause if isFinishing() == true.
see 3.
I personally like to set up a Handler in the Activity and send Messages to it from the service.
If you are binding a Service to your Activity. It simply means that you need service to run as long as your activity is running. If you do not need to bind Service with activity or you do not need to update your UI while your Service is running. you must not bind your Service to your Activity. In this case, for different actions done by Service you can notify user using Android Notifications. Like notifying user that xx download has been completed.
It totally depends upon your purpose that you want to achieve from Service.
if you can use IntentService for your application, you can pass data to the service through an Intent. results can be passed back to the Activity through a ResultReceiver
If you bind your Service to your unique Activity, you'll have it alive as long as the Activity is not terminated or the service isn't unbound. Just bind it on the onCreate() and let it get unbound when stopping your activity (no need to do anything).
You can create a Listener interface within your service, that you'll implement in the Activity, so you can send those notifications from the Service to the Activity. You'll find suitable example and information about this if Googling.
Related
Say I have an Activity that launches a Service using startService() when user presses a button. The service is stopped using another intent in startService() when user presses another button, or it can decide to stop itself. The service wants to run forever, so it returns START_STICKY from onStartCommand(). While the service is running, it connects to some server and receives messages. Activity binds to it and, when bound, displays those messages.
Now, when the Activity is started after an OOM kill, it doesn't know if the Service is started. One can use a static field set in onStartCommand() to tell if the Service is running, but that's prone to backfire sooner or later. Moreover, the Service can be started by the system after the activity has been launched, so using a static field is not an option at all.
The cleanest solution I can come up with is using SharedPreferences to store the state of the service. This is going to fail to be good when the service is killed between SharedPreferences.setBoolean("running", false) and stopSelf(), but such a thing will probably occur rarely.
Also I can try to bind to the Service at all times. This would be very ugly but still not impossible.
Any other solutions? Maybe the very idea is wrong in some way?
Suppose that Activity A starts Service S and binds to Service S.
What will happen to S when A is destroyed?
How can I recreate another Activity that binds to S? The sample code in http://developer.android.com/guide/components/bound-services.html unbind the service in onStop(). I think if I open the app again, a new process is created for another instance of A and S. But I want the new activity to get data from the old service.
What will happen to S when A is destroyed?
If A is the only Activity bound to S and you didn't start the Service via startService(Intent), S will be destroyed. That's because a Service will be alive till the last bound Activity unbounds from the Service. This is documented here.
How can I recreate another Activity that binds to S?
If A is bound and you switch to Activity B via Intent, the Service will be destroyed and recreated when B binds to it.
If you want the Service to be alive even if no Activity is bound to it, you have to call the Servie with startService(Intent). Now it will be around if you explicitly stop it or system means it's time to destroy it. If you don't want this behavior, persist your data and access it at given time.
I think if I open the app again, a new process is created for another instance of A and S
The process remains the same till the process is killed from the system or if you kill the process, which is not recommanded.
Edit:
Only the bound service lifecycle depends on Activities. If you want a stand alone one use startService(). This way it's independent from Activities and runs in background as long as the process of the App is up or you explicitly stop the Service with stopService() / stopSelf(). You could even have a Service in a own App and use IPC to communicate between Apps. It's all a matter of the use case.
As you can see the configuration of a Service is very flexible and you have to decide which fits best for your App.
If You start service through startService() it will keep remaining after Activity finishes.
If You start service through bindService() it will live until last Activity unbounds from it.
Also if service is already started and you call startService() no new instance of service will be created, but in living service method onStartCommand() will be executed.
Almost same when you bind to living service, methon onBind() will be executed.
I have a service which I know is already running, how does my activity communicate with it without restarting the service.
to elaborate, I have a widget which starts a service, upon click an activity gets loaded, in that scenario the service should still be running.
How do I do something along the lines of:
conditionally checking if the service is alive
accessing methods in the service
Basically, the main thing I do not want to do is run startService(new Intent(...)) within my activity. I don't want to run onStart again within my service.
Ideally I can just add some methods within my service class, and call those within my activity, like I would call any other public method in the project.
I want to start some new threads within my service, and I don't want to make a second service class if I don't have to.
Thanks for the insight
The documentation on startService() might help:
Request that a given application service be started. The Intent can either contain the complete class name of a specific service implementation to start, or an abstract definition through the action and other fields of the kind of service to start. If this service is not already running, it will be instantiated and started (creating a process for it if needed); if it is running then it remains running.
...
Returns
If the service is being started or is already running, the ComponentName of the actual service that was started is returned; else if the service does not exist null is returned.
So if your Service is already running, startService() won't start a new version.
I have a android app that has an ongoing service for tracking a persons location. I want to be able to start up the service from within the activity and end it from within the activity. When the activity is running I need to be able to bind to the service and communicate to it via aidl. However I need the service to continue even when the activity has ended. I only want the service to end when the it is told to by the activity.
So far I have my activity and service and they can communicate through a aidl file. But at the moment the service ends when the activity does.
How do I get it to continue running when the activity has ended AND how do I rebind to it when the activity is restarted?
I have figured it out.
I was using the bindService method which created my service for me and bound to it (I need a binding), but if a service is created via a call to bindService the service only lives as long as the binding and so when calling unbindService, which I was doing when my activity was destroyed, the service itself also was destroyed.
However if you create your service with a call to createService and then call bindService you will bind to the already running service. Then when you come to unbind from it, it no longer destroys itself. The service will continue until you call stopService.
Then when you re-enter your activity you can check if the service is already running and if it is just call bindService other wise just call startService followed by bindService
I have a background Service which must run permanently. The service just has to interact with my Activity.
How to i check on activity resume if the service is still running? Is there a better possibility than a static variable in the service class?
Is it of advantage to use a remote service in a separate process(to extend service life time) so that when the activity process gets killed the service is still alive?
I have a background service which must
run permanently.
This is not possible. The user or Android will kill off your service at some point. Please reconsider your architecture.
How to i check on activity resume if
the service is still running?
Generally, you don't.
Is there a better possibility than a static variable in the service class?
That will not work if the user or Android kills off your service.
Is it of advantage to use a remote
service in a separate process(to
extend service life time) so that when
the activity process gets killed the
service is still alive?
A remote service has nothing to do with whether the service runs after activities are destroyed. If you call startService(), the service will run, independent of any activities, until:
you call stopService() from an activity
the service calls stopSelf()
Android terminates the service
the user terminates the service via the Settings application
the user terminates the service via a "task killer" (Android 2.1 and earlier, at least)
Why do you want to know if the Service is running?
If you need something from it, just throw an Intent and if it's not running it will start by the intent.
In relation with the second question:
Your service will not "die" when your Activity closes.
Start the service in startforeground(). It will increase the timespan of the service.