Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 335
Surya Wijaya Madjid Avatar asked Feb 10 '26 00:02

Surya Wijaya Madjid


1 Answers

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.

like image 188
Joe Malin Avatar answered Feb 12 '26 15:02

Joe Malin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!