16

I am referring to android design considerations: AsyncTask vs Service (IntentService?)

According to the discussion, AsyncTask does not suit, because it is tightly "bound" to your Activity

So, I launch a Thread (I assume AsyncTask and Thread belong to same category), have an infinity running loop in it and did the following testing.

  • I quit my application, by keeping pressing on back soft key, till I saw home screen. Thread is still running.
  • I kill my application, by going to Manage apps -> App -> Force stop. Thread is stopped.

So, I expect after I change from Thread to Service, my Service will keep alive even after I quit or kill my app.


Intent intent = new Intent(this, SyncWithCloudService.class);
startService(intent);

public class SyncWithCloudService extends IntentService {
    public SyncWithCloudService() {
        super("SyncWithCloudService");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        int i = 0;
        while (true) {
            Log.i("CHEOK", "Service i is " + (i++));
            try {
                Thread.sleep(1000);
            } catch (InterruptedException ex) {
                Log.i("CHEOK", "", ex);
            }
        }
    }
}

    // Doesn't matter whether I use "android:process" or not.
    <service 
        android:name="com.xxx.xml.SyncWithCloudService" 
        android:process=".my_process" >
    </service>

However, my finding is that,

  • I quit my application, by keeping pressing on back soft key, till I saw home screen. Service is still running.
  • I kill my application, by going to Manage apps -> App -> Force stop. Service is stopped.

It seems that the behaviour of Service and Thread are the same. So, why I should use Service instead of Thread? Is there anything I had missed out? I thought my Service suppose to keep running, even after I kill my app?

Community
  • 1
  • 1
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

5 Answers5

12

Nope. Service will stop running when you kill your application. When you kill your application all components of it are killed (activities, services, etc.).

In general the behaviour of Thread and Service are similar. However, If you start a Thread from an Activity and then shutdown the activity (ie: quit your application), eventually Android will notice that your process has no active components in it (since Android doesn't recognize your Thread as an active component) and it will just kill your process, thereby killing the thread.

However, if you have a Service running, then Android will notice that you have a service running and not kill it so readily. However, it is still possible that Android will kill your service process if it isn't "in use".

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 2
    But my testing is that, when I quit my application (pressing back soft key several time till I reach home screen), the thread is still running. Is it because, I am just lucky enough that OS doesn't kill my thread? – Cheok Yan Cheng Jun 28 '12 at 09:40
  • 2
    Yes, your thread will run for some period of time until Android decides to kill the process. It doesn't do it right away, it only does it if and when it feels like it. You have no control over when the process will be killed. – David Wasser Jun 28 '12 at 09:51
  • OK. Thanks. I will test to run it for several hours and see how it works. For service, is Android "guarantee" not killing it? As I have an infinity loop running within it (For testing). – Cheok Yan Cheng Jun 28 '12 at 09:59
  • 3
    No, as I said before, Android will also kill your Service if it thinks it isn't needed, or if other applications need the memory. However, it will kill processes that don't have running services before it will kill processes that do have running services. – David Wasser Jun 28 '12 at 10:03
4

Actually there are different kinds of services you can implement. Use a Service instead of IntentService. There you need to look at START_STICKY , START_NOT_STICKY and START_REDELIVER_INTENT you can keep your service running in background even if your activity dies. Android services

Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
1

You are using startService(). The Service will run until it's code is done, or until Android decides it should be killed. Look up on bound services. On your Activity.onDestroy() you should call unbindService().

tolgap
  • 9,629
  • 10
  • 51
  • 65
1

In your IntentService you can override onStartCommand() returning START_REDELIVER_INTENT

Then if killed, your service will be restarted automatically by the system after some time with the same Intent.

Be sure to call the super implementation on onStartCommand() like this:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent,flags,startId);
    return START_REDELIVER_INTENT;
}
Ismael
  • 369
  • 3
  • 7
1

You can invoke setIntentRedelivery(true) in the constructor of the IntentService

ElOjcar
  • 301
  • 2
  • 4
  • 12