0

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

Ankur Samarya
  • 219
  • 1
  • 3
  • 15

2 Answers2

1

use below code for restart service after close app

public class MyService extends Service {

@Override
public int onStartCommand(final Intent intent, final int flags,
                          final int startId) {
    super.onStartCommand(intent, flags, startId);
    return Service.START_STICKY;
}

@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    Intent restartService = new Intent(getApplicationContext(),
            this.getClass());
    restartService.setPackage(getPackageName());
    PendingIntent restartServicePI = PendingIntent.getService(
            getApplicationContext(), 1, restartService,
            PendingIntent.FLAG_ONE_SHOT);
    AlarmManager alarmService = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
    alarmService.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + 100, restartServicePI);
    Toast.makeText(this, "onTaskRemoved", Toast.LENGTH_SHORT).show();
    super.onTaskRemoved(rootIntent);
}}

above method onTaskRemoved restart your service after 100 mili seconds.

Sandip
  • 593
  • 4
  • 20
  • why should i use onTaskRemoved. Service should not get killed unless memory become low. According to Android doc this is default functionality. – Ankur Samarya Sep 20 '17 at 13:59
0
@Override
    public void onTaskRemoved(Intent rootIntent) {
}

above method will be called when app is removed from recent. But there would be no context. So you need to do your task when context is available. So place code inside,

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    
   //do your operations
    return START_REDELIVER_INTENT;
}

Remember inside onStartCommand you should return either START_REDELIVER_INTENT or START_STICKY. You can get difference from here.

And one more thing, onStartCommand will revoke automatically only if startService is called once from anywhere of your code.

So, run service by calling

startService(new Intent(context, serviceName.class));

Following above codes onStartCommand will be revoked periodically if service is not stopped.

Community
  • 1
  • 1
Exigente05
  • 2,161
  • 3
  • 22
  • 42
  • why should i use onTaskRemoved. Service should not get killed unless memory become low. According to Android doc this is default functionality. – Ankur Samarya Sep 20 '17 at 14:00
  • here is an explanation - https://stackoverflow.com/questions/20392139/close-the-service-when-remove-the-app-via-swipe-in-android?rq=1 – Exigente05 Sep 20 '17 at 18:11