Saturday, 3 June 2017

How to detect app removed from the recent list

  No comments
To detect when user swipe out the app from recent list, you need to run the background service in your application which help you to notify.
Below are the following ways:

Method 1: If you want to stop the service when user remove the application from recent list

<service
           android:name=".background service name"
           android:enabled="true"
           android:exported="false"
           android:stopWithTask="true" /> 

Method 2: If you want to execute something before stopping the service.

<service
           android:name=".background service name"
           android:enabled="true"
           android:exported="false"
           android:stopWithTask="false" />
Suppose we have service class with name GangsOfCoderService.class (this service class will notify when user remove application from recent).


public class GangsOfCoderService extends Service {

   @Override
   public void onCreate() {
       Log.d("tag", "onCreate");
       super.onCreate();
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
       Log.d("tag", "onStartCommand");
       return START_STICKY;
   }


   @Nullable
   @Override
   public IBinder onBind(Intent intent) {
       Log.d("tag", "onBind");
       return null;
   }

   @Override
   public void onTaskRemoved(Intent rootIntent) {

       super.onTaskRemoved(rootIntent);
       //do something you want
       //stop service
       Log.d("tag", "onTaskRemoved");
       this.stopSelf(); //this line is used to stop the service
   }


}
Hope this post help you

No comments :

Post a Comment

Loading...