Created a IntentService
and put for loop
in onHandleIntent
method. Every time I close app(remove from recent not force close) it got stopped. But onDestroy
did not called.
I also tried on different devices as well. I dont think it is a problem of low memory.
So does Service mean to be use only when app is in foreground?
I have to do some task in backgound off the main thread and service got close as user close the app.
here is my sample code
public class MyIntentService extends IntentService {
private static final String TAG = "MyIntentService";
public MyIntentService() {
super("MyIntentService");
}
@Override
protected void onHandleIntent(Intent intent) {
for (int i = 0; i < 30; i++) {
Log.d(TAG, "onHandleIntent: " + i);
try {
Thread.sleep(600);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void onDestroy() {
super.onDestroy();
Log.d(TAG, "onDestroy: ");
}
}
refs:How to keep an IntentService running even when app is closed?
Service restarted on Application Close - START_STICKY