Reputation: 5767
just saw Google I/O 2010 - Android REST client applications where it was said: start long running operations from a Service And as I understand (My English is poor) the main purpose of this is to prevent the app to be killed by the system. Right? Is it good practice to perform all long running operation not only web ones from a Service?
As I understand if I create an object in activity and then the app process will be killed I will lose this object, but if I create this object in a service and after process will be killed - my object still will be alive. Right?
(SEPARATE THREAD) VS (SERVICE + SEPARATE THREAD)
What is different in the context of my question?
Upvotes: 0
Views: 199
Reputation: 5767
1. The Service API in Android is one of the key mechanisms for applications to do work in the background.
But it can easily implemented using a simple thread.
BUT: *Once an application is no longer visible to the user it is generally considered expendable and a candidate to be killed by the system if it ever needs memory elsewhere. The main way applications get around this is by starting a Service component, which explicitly tells the system that they are doing some valuable work and would prefer that the system not kill their process if it doesn't truly need to.*
So as I understand that is difference between a regular thread and a Service for our case.
Upvotes: 0
Reputation: 12048
Not exactly ...
You are somewhow mixing the concepts Destroyed
and Killed
Destroyed
This is something that your application should be prepared to deal with. For may different reasons (i.e. device orientation changes, and other) the OS may destroy your activity (usually calling onDestroy()
beforehand, so you have opportunity to save what's important) and all objects in your activity will be lost. However you application may contain more then one activitiy and/or service, and having one activity being destroyed, doesn't mean the others will be estroyed as well.
Killed The OS may decide for some reason kill your application. This is done by killing the process used by your application, which is the process where run all activities and services that compose your application. Your application will not have any warning, and can't do nothing to prevent this. All objects in your apllication (used by activities and services) will be lost.
Having said that, whats´s the advantage of a Service
in a long running activity?
Let's use a simple example:
If your activity is in the midle of uploading a large file to a server, and the user decide to change device orientation, the activity will be destroyed and the upload interrupted. Even if restart the uploading when the activity restarts, it would start from begining again.
If you have a service doing the upload stuff under request from an activity, even if your activity is destroyd, the service remains active and the upload continues without imterruption.
Notes: If you have a really long running service (that will be running for more then half an hour) then make it as foregroung service, otherwise it will be killed anyway.
Upvotes: 2