Bhavesh Jethani
Bhavesh Jethani

Reputation: 3875

Stop service on swipe to remove

App killed from Recently open application list by swipe.

In my application,
I am running service in background only when application is available in foreground or in background. If user has sent application in background by pressing home button of mobile then also service should be run.


But when user remove application from recently opened application then
1) Is any methods invoke automatically when removed from recent app list?
2) Which method will be invoke?

Upvotes: 1

Views: 2739

Answers (3)

Bhavesh Jethani
Bhavesh Jethani

Reputation: 3875

public boolean isAppRunning()
    {
        boolean appFound = false;
        final ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        final List<RunningTaskInfo> recentTasks = activityManager.getRunningTasks(Integer.MAX_VALUE);

        for (RunningTaskInfo recentTask : recentTasks)
        {

            if (recentTask.baseActivity.getPackageName().equals("your.packagename"))
            {
                appFound = true;
                break;
            }

        }
        return appFound;
    }

using this method i have solved my problem if i got false then i will close my service.

need to add permission:

<uses-permission android:name="android.permission.GET_TASKS" />

Hope its worked

Upvotes: 2

SourabhTech
SourabhTech

Reputation: 1377

You should Add the permission in you manifets

 Like: android:excludeFromRecents="true"

 Code:<activity
        android:excludeFromRecents="true"
        android:name="Example.com.MainActivity"
        android:label="@string/app_name" >
          <intent-filter>
             <action android:name="android.intent.action.MAIN" />                       
             <category android:name="android.intent.category.LAUNCHER"/>
         </intent-filter>
      </activity>

Upvotes: 1

prijupaul
prijupaul

Reputation: 2086

1.You can stop the service in the activities onDestroy method.

2.or In the manifest file, you can also add stopwithtask=true

<service 
    android:name="com.mycompany.TestService"
    android:stopWithTask="true" 
  />

3.or You could also check whether 'onTaskRemoved' is invoked.onTaskRemoved doc

Upvotes: 4

Related Questions