How to Create an Android Activity and Service That Use Separate Processes

How to create an Android Activity and Service that use separate processes

Definitely possible. See the process attribute for service in AndroidManifest.xml

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

To quote:

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. It has the same name as the application package. The element's process attribute can set a different default for all components. But 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 (':'), 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.

Start a service in a separate process android

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.

Android service in separate process

Ok, I figured it out: When I swipe my application from recent apps, both processes (main and service) closed, then serivce restarted. I solved it by adding startForeground(R.string.app_name, new Notification()); in onCreate of my service (Can a service be killed by a task killer). Thanks all :)

Every Activity in Android is a Process,or One Application is one process

All activities inside an application run in one process?

It depends on value of android:process attribute in application manifest.

if attribute android:process is not defined for Application/Activity tags in the manifest, by default all the activities will run in single process (process name will be name of the package defined in manifest)

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.so.test" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Activity1">
</activity>
<activity android:name=".Activity2">
</activity>
<activity android:name=".Activity3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

In the above manifest all activities run in process com.so.test,ps command output in adb shell:

# ps
app_39 668 33 84492 20672 ffffffff afd0c51c S com.so.test

If android:process is specified for Activity new process will be created with the same userid and the activity runs in that process.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.so.test" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />

<application android:icon="@drawable/icon" android:label="@string/app_name"
android:process="com.so.p1">
<activity android:name=".Activity1">
</activity>
<activity android:name=".Activity2" android:process="com.so.p2">
</activity>
<activity android:name=".Activity3" android:process="com.so.p3">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

If the manifest is defined like above

Activity1 runs in com.so.p1 process
Activity2 runs in com.so.p2 process
Activity3 runs in com.so.p3 process

ps output in adb shell

# ps
app_39 650 33 83192 20900 ffffffff afd0c51c S com.so.p1
app_39 659 33 83188 20864 ffffffff afd0c51c S com.so.p2
app_39 668 33 84492 20672 ffffffff afd0c51c S com.so.p3

If an Activity needs to be run in another process not defined in this manifest, both APKs should be signed with the same certificate.

How to run two activities in different processes

In your manifest,set the activities to run on different processes as in my below example

         <activity
android:theme="@style/AppTheme.NoActionBar"
android:name=".ui.MapsActivity"
android:process=":MapView"
android:label="@string/app_name">
</activity>
<activity
android:theme="@style/AppTheme.NoActionBar"
android:name=".ui.MapsActivity02"
android:process=":MapView02"
android:label="@string/app_name">
</activity>

Android Service running on separate Process gets killed when I swipe out my App (running in other process)

So, according to your hints (and so new keywords for me to look for) and after some additional research by myself, I think I have solved my problem. During my research I have found an very interisting blog post on this topic, maybe also for you, which is why I would like to share it with you: http://workshop.alea.net/post/2016/06/android-service-kill/ .

After verifying and going through the steps in this article everything seems to work fine (so startForeground seems to solve the problem). I want to point out here, that I have only tested it, with my service instance still running in separate process, so manifest entries as is above.

The actual thing which really confused me at the beginning was my android studio debug session being killed everytime, just after swiping out my app from recent apps (menu). This made me think my service being killed by the system as well. But according to the article (I have added some logs to the callback methods provided) when

  1. Opening my app

  2. starting service

  3. swiping out app

  4. starting app again and finally

  5. calling service again,

I only received callbacks to the methods as if my service would still be running. Having an explicit look at DDMS (tool) also prooved my 2nd process, and thus my service, being still alive. Having verified this, I then cleared all my app data and repeated the steps above (excluding step no. 5). Having had a look in the database afterwards, prooved the data having been downloaded by the service.

For the curious of you:

The process of swiping out my app from recent apps (and thus having the onTaskRemoved callback method being called) lead to another problem. It somehow increases the startId parameter of onStartCommand by 1 so that my DelayedStopRequest malfunctiones and doesn't stop my service anymore.

This means: Repeating above steps 1 - 3 makes me receive startId = 1 in onStartCommand. By calling stopSelfResult(1) later on (which was the latest startId) it returnes false and the service keeps running. Continuing to follow step 4 + 5 then, makes onStartCommand being called with startId = 3 (but should actually be 2! which is skipped somehow). Calling stopSelfResult(3) with parameter 3 later on is then going to stop the service again (also visible in screenshots).

I hope my answer is correct so far (, understandable) and also helpful for you. Thank you for all of your answers which provided beneficial input and also pointed me to the solution. The android version I have been working with is:

4.1.2 - Jelly Bean | API Level : 16

I also added screenshots of the log entries from DDMS (imgur is rejecting my uploads so you'll temporarily have a link to my dropbox):
screenshots from logs from DDMS



Related Topics



Leave a reply



Submit