Services in separate processes

1. Running a service in its own process

You can also specify that your Service runs in a separate process via the <android:process=":process_description"> attribute.

<service
        android:name="WordService"
        android:process=":my_process"
        android:icon="@drawable/icon"
        android:label="@string/service_name"
        >
</service>

The colon prefix in front of the name tells Android that the Service is private to its declaring application. If the colon is not used, the Service would be a global process and can be used by other Android applications.

Running a service in its own process will not block the application in case the service performs long running operations in its main thread. But as the services runs in its own process, you need to use some interprocess communication (IPC) to communicate to your service from other parts.

Even if the service runs in its own process, you need to use asynchronous processing to perform network access, because Android does not allow network access in the main thread of a process.

5.2. When to run a service in a separate process?

Running a service in its own process gives it its own memory address space and a garbage collector of the virtual machine in this process does not affect the application process.

Application rarely need to run a service in its own process. Running services in its own process makes the communication with other Android components and the service harder to implement.

Note: If you want to make a service to other Android application available, they must run in their own process.

No comments:

Post a Comment