How to detect app removed from the recent list?

后端 未结 3 517
忘掉有多难
忘掉有多难 2020-11-29 09:22

I\'m working on a music app, and I want stop the service when user removes the application from the recent list and not when the first activity is destroyed(because when use

3条回答
  •  没有蜡笔的小新
    2020-11-29 10:06

    Im posting this answer, since the chosen as best solution was not working for me.

    This is the updated version:

    First create this service class:

     public class ExitService extends Service {
    
    
        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    
        @Override
        public void onTaskRemoved(Intent rootIntent) {
            System.out.println("onTaskRemoved called");
            super.onTaskRemoved(rootIntent);
            //do something you want before app closes.
            //stop service
            this.stopSelf();
        }
    }
    

    Then, declare this way your service in the manifest label:

    
    

    Now, just start the service wherever you want to do something before your app closing.

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

提交回复
热议问题