43

I want to start a service in a separate process (i.e when I go to my Application manager in the settings and then go to running services, it should show my service in a separate process).

My Android Manifest is as follows:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.timerapp.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <service android:name="com.example.timerapp.WorkerThread"
        android:process="com.moizali"></service>
</application>

I am starting the service in my MainActivity so obviously when I kill the application the service shuts down as well. Can anyone tell me how to start the service as a different process.

SoH
  • 2,180
  • 2
  • 24
  • 53
  • 3
    You may find it more precise to test your results with the adb shell `ps` command or the DDMS process list, as that settings menu display seems to have some odd "user friendly translation". – Chris Stratton Mar 19 '14 at 18:11

2 Answers2

53

Check out the process attribute for service in AndroidManifest.xml. You need to change your android:process value to start with a :.

http://developer.android.com/guide/topics/manifest/service-element.html

The relevant section:

If the name assigned to this attribute begins with a colon (':'), a new process, private to the application, is created when it's needed and the service runs in that process. If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

The other answer provided doesn't really answer the question of how to start a service in a separate process.


Defining a Process of a Service

The android:process field defines the name of the process where the service is to run. Normally, all components of an application run in the default process created for the application. However, a component can override the default with its own process attribute, allowing you to spread your application across multiple processes.

If the name assigned to this attribute begins with a colon (':'), the service will run in its own separate process.

<service
  android:name="com.example.appName"
  android:process=":externalProcess" />

If the process name begins with a lowercase character, the service will run in a global process of that name, provided that it has permission to do so. This allows components in different applications to share a process, reducing resource usage.

Graham
  • 7,431
  • 18
  • 59
  • 84
adao7000
  • 3,632
  • 2
  • 28
  • 37
  • I get an error when I try to bind to the service using this method. – TheRealChx101 Jul 03 '17 at 17:06
  • 4
    @TheRealChx101 you cannot directly bind to a service in a different process. You will need to define an Android interface (AIDL). See this answer: https://stackoverflow.com/a/42514893/4730000 – Daniels Šatcs Jun 10 '18 at 12:41
  • 5
    What is the use of running in different process? In that case also it is going to be killed when original application gets killed. – Vivek MVK Aug 31 '18 at 15:05
  • if i am making a AIDL service in another process does it have to called:remote or can i use any name i want ? android:process=":remote", is this just the common way so we know its remote but it can be any name right ? – j2emanue Jul 21 '19 at 15:06
  • I didn't understand manifest that much - what do write at the place of android:name=" com.example.appName" ok as it says I have to add the name of my app which is package="com.example.doghowlapp" but when I write doghowlapp it doesn't work – Vasant Raval Feb 13 '22 at 08:15
28

Running on a separate process will not solve your problem. I had the same issue and this is a normal behavior of Android. When you start a Service (not a Foreground Service), even if it is in a separate process, the OS can kill it any time. In your case, if you close the Activity(s)/kill the Application, the OS will normally close the service even if they are in separate processes. You have two options:

1- Start your service as a Foreground Service. In this case the Service will not be closed due to almost any condition. Be aware that the foreground service is designed for specific applications and you will have a sticky notification in the notification center and status bar.

2- Make your service as a Start service (not an IntentService). Then on the onStartCommand of service, return START_STICKY. This will tell the OS, if for any reason you need to close the Service run it again when you have enough resources. In your case, when the user closes the Activity/kill the Application the Service process will be killed anyway, however it will normally reopen.

fawaad
  • 341
  • 6
  • 12
Amir
  • 1,722
  • 22
  • 20
  • 1
    what to use if I am implementing music player? I am using Service but it's closed when I open some heavy apps even when I start using startForeground() – mmaksitaliev Aug 20 '16 at 15:40
  • Start your service as a foreground service. (https://developer.android.com/guide/components/services.html#Foreground) – Amir Aug 20 '16 at 16:47
  • @Amir what if i create a service in another process but the USER terminates the app (clears the activity stack), will the service still be running ? since the USER terminated and not the OS, im wondering if there is a difference and if the service can continue if user killed the app instead of OS ? – j2emanue May 11 '19 at 05:22
  • @j2emanue, First, In android when you create a new process, it would be a sub-process inside the main app process. It is not a separate process like other operation systems. Second, there is no difference, when the app is killed by user or OS, the all sub-processes would be killed, and then if the situation is according to the answer above, it could start again – Amir May 11 '19 at 15:13
  • 1
    The process gets its own process ID. There is no concept of sub-process as such in android. If you declare "android:process" tag then it will share the process with the existing(if any) process provided both the app are signed with the same certificate. – Saty Dec 15 '20 at 07:00