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.
Related
I want to do this acts with my Service :
1)start App
3)start a Service by App
4)update my UI by Service
5)close App but the Service keep work
6)sart App again
7)connect to Service (not run new Service)
I want in my service public variable during living Service do not change.
If you return START_STICKY from onStartCommand() then your Service will run unstil it is explicitly stopped. You can then decide how you want to stop your Service, either by something calling stopService() or by the Service calling stopSelf() when it no longer wants to run.
Please also note that Android can kill your Service pretty much whenever it wants to. If you have returned START_STICKY then Android will restart your Service after it kills it, but there is nothing you can do to ensure that Android will not kill your Service.
I am wondering what is the best way to keep a service running while the application is running (may be in background) and then the service stopping when the application has ended (the application in this case has completely stopped, not in the background).
Another sub-question is: How to stop a service when application stops?
I realize one solution is to use BoundServices but is this the best way or good enough?
For example if an activity binds a service and then the system kills the activity and brings
it alive again then how will the service behave? Or are there other issues I am not aware of?
What would be the best way to implement this? Thanks.
Check out http://developer.android.com/guide/components/bound-services.html.
A bound service is the server in a client-server interface. A bound service allows components (such as activities) to bind to the service, send requests, receive responses, and even perform interprocess communication (IPC). A bound service typically lives only while it serves another application component and does not run in the background indefinitely.
You have two options to start the service:
1. bindService() to activity
2. startForeground() and bind while it runs
Option 1 will only run as long as the activity is in view and the runnable active. (example use would be to use service to download a file)
Option 2 will run as long as the application is running (even when the activity is in the background) This option requires that your service be listed in the notification bar.
You can have a service stop itself by calling stopSelf(int) (I dont think this works if a activity is currently bound) or you can call stopService() from an activity.
As for the system killing the activity question.... Without knowing what your service will be handling its hard to give advice on how to handle this situation. For the most part a service running in the foreground will be the last resource the system will try to reclaim. If the system kills the activity the services onDestory method will be called where you should do some clean up so that the next time it starts you can continue in a safe manor.
http://developer.android.com/images/service_lifecycle.png
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.
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.
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