I have found one way to do this…..
-
Make one service like this
public class OnClearFromRecentService extends Service { @Override public IBinder onBind(Intent intent) { return null; } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.d("ClearFromRecentService", "Service Started"); return START_NOT_STICKY; } @Override public void onDestroy() { super.onDestroy(); Log.d("ClearFromRecentService", "Service Destroyed"); } @Override public void onTaskRemoved(Intent rootIntent) { Log.e("ClearFromRecentService", "END"); //Code here stopSelf(); } }
-
Register this service in Manifest.xml like this
<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />
-
Then start this service on your splash activity
startService(new Intent(getBaseContext(), OnClearFromRecentService.class));
And now whenever you will clear your app from android recent Then this method onTaskRemoved()
will execute.
NOTE: In Android O+ this solution only works when the app is full-time in foreground. After more than 1 minute with the app in background, the OnClearFromRecentService (and all other Services running) will be automatically force-killed by the system so the onTaskRemoved() will not be executed.