Start a Service in a Separate Process Android

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 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

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 :)

Can you start an IntentService on a separate process?

1) Is it possible to start an IntentService on a separate process? How? If so, is it mandatory to bind to it?

Yes, you can start an IntentService in a separate process. Simply add android:process=":whatever" to the manifest entry for that service.

No, you don't need to bind to it. You can communicate with it by sending it Intents using startService()

2) Is it possible to start an IntentService on a separate process AND run it in the foreground?

Yes (see above). To make your service run in the foreground it can call startForeground() whenever it wants to do that. The service itself is in control of whether it runs in the foreground or background.

3) What's the difference between android:isolatedProcess and android:process? See: http://developer.android.com/guide/topics/manifest/service-element.html

android:process allows you to control in which process each particular component runs (by specifying the name of the process). You can group components of your application to run in separate processes (for example, all UI components in one process and all services in another). The default behaviour is that all components of an application run in the same process.

android:isolatedProcess is a flag (true/false) that you can set if you want a particular service component to run in a separate process isolated from the rest of your application. The isolated process doesn't have any of the permissions that are granted to the rest of your application. Normally, permissions are granted to an application and all components of the application have all the permissions that the application gets. android:isolatedProcess is only available starting with API level 16 (Jellybean). See http://aleksmaus.blogspot.de/2012/09/a-feature-of-android-jelly-bean.html and Advantage of introducing Isolatedprocess tag within Services in JellyBean[Android]

Android - How to decide whether to run a Service in a separate Process?

The first place to start is by reading through the description of component lifecycles. The take away from that is you really are not guaranteed that a Service or other component will be allowed to run for a long period of time.

However, it does sound like a Service is the right choice for the functionality you describe. This is because you are doing some operations that are not user facing. Going back to the lifecycle description, any time an Activity is not in the foreground, it is essentially a candidate for being killed.

What you should consider doing is using AlarmManager to periodically trigger your Service. You might want also to look at using the WakefulIntent library that @CommonsWare has created.

There is a good article describing multitasking and processes on the Android blog called Multitasking the Android Way that might get at some of the more details regarding processes you are interested in. For example:

A common misunderstanding about
Android multitasking is the difference
between a process and an application.
In Android these are not tightly
coupled entities: applications may
seem present to the user without an
actual process currently running the
app; multiple applications may share
processes, or one application may make
use of multiple processes depending on
its needs; the process(es) of an
application may be kept around by
Android even when that application is
not actively doing something.

Android service running on separate process is not Created

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
startService(new Intent(this, Smart113MainService.class));
}
else {
startForegroundService(new Intent(this, Smart113MainService.class));
}

Move startForeground code to onCreate()

@Override
public void onCreate() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(NOTIFICATION_ID, notification);
}
...
}

to remove notification onDestroy()

@Override
public void onDestroy() {
...
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
stopForeground(true); //true will remove notification
}
...
}

Update

You are not getting debug point in different process because you are selecting main process from attach debugger. Select right process to debug.

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.



Related Topics



Leave a reply



Submit