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?