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.
Check out the
process
attribute forservice
inAndroidManifest.xml
. You need to change yourandroid:process
value to start with a:
.http://developer.android.com/guide/topics/manifest/service-element.html
The relevant section:
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.
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.
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 ForegroundService
), even if it is in a separate process, the OS can kill it any time. In your case, if you close theActivity
(s)/kill theApplication
, 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 theService
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 theonStartCommand
of service, returnSTART_STICKY
. This will tell the OS, if for any reason you need to close theService
run it again when you have enough resources. In your case, when the user closes theActivity
/kill theApplication
theService
process will be killed anyway, however it will normally reopen.