Surya Wijaya Madjid
Surya Wijaya Madjid

Reputation: 1843

Multiple services sharing one process, how many "main" threads?

Let's say I have two applications which share the same user ID and the same process, by declaring in their AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.myapp.appname1" 
    android:sharedUserId="com.myapp.sharedUserId">

    <application android:process="com.myapp.sharedProcess">
        ...
        ...

and

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.myapp.appname2" 
    android:sharedUserId="com.myapp.sharedUserId">

    <application android:process="com.myapp.sharedProcess">
        ...
        ...

If each application has a service defined and both are running at the same time, despite being run under a same process will they get one "main" thread for each? Or they also share one "main" thread?

Any enlightment would be very appreciated. Thanks!

Upvotes: 3

Views: 2838

Answers (1)

Joe Malin
Joe Malin

Reputation: 8641

You can't have more than one application per manifest. Two applications can't normally share the same process, but you can override this with the android:process attribute (see below).

Moreover, the code in a Service runs in the same thread as the Activities in the app, which is the UI thread.

If you have multiple services in the same application, they're in the same process as the application, all on the same thread.

To get around this, you can specify a process name for the application, and also specify a process name for a Service. See the android:process attribute.

This will give you a separate thread for each service, because of course each process has its own threads.

However, depending on what you're using the Services for, you might prefer to have multiple IntentService classes. They all run in the same process as your app, but they each have their own thread that is different from the UI thread.

Of course, you can also hand-code multiple threads for the same Service, but this is harder to implement correctly.

Upvotes: 2

Related Questions