Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - keeping the service alive even parent process is killed

I am new to Android. Right now I am just trying some examples which includes Services. In one of the example, service is created as a separate background process. Using something like this

android:process=":background"  

They said now service will be having separate process environment and thus it is separate from the one(Application) that is started it. When I killed the parent process i.e.Application, the service is also getting killed. Is this normal behavior or not? Because what I understand from that article is Service wont get affected since it is different process environment. Please correct me if I am wrong. Thanks in advance

UPDATE: Even I saw the same behavior if I use :remote.

like image 535
CrazyCoder Avatar asked Feb 23 '12 06:02

CrazyCoder


People also ask

Does Android service run on main thread?

Service runs in the main thread of its hosting process; the service does not create its own thread and does not run in a separate process unless you specify otherwise.

What happens when Android decides to shut down a process?

Android might decide to shut down a process at some point, when resources are required by other processes that are more immediately serving the user. Application components running in the process that's killed are consequently destroyed.

How to stop a service Android?

A service is started when component (like activity) calls startService() method, now it runs in the background indefinitely. It is stopped by stopService() method. The service can stop itself by calling the stopSelf() method.


2 Answers

Just a copy from the Android document:

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed. If the process name begins with a lowercase character, a global process of that name is created. A global process can be shared with other applications, reducing resource usage

http://developer.android.com/guide/topics/manifest/application-element.html#proc

like image 104
Jammy Lee Avatar answered Oct 13 '22 10:10

Jammy Lee


Afaik, there are two types of services,

1) background : which run in the same process of your application.

2) Remote :If we want to make this service run in a remote process (instead of the standard one for its .apk), we can use android:process in its manifest tag to specify one: ,

we can also use other strings then background and remote. here is service lifecycle

like image 31
AAnkit Avatar answered Oct 13 '22 10:10

AAnkit