Stop service on swipe to remove
Asked Answered
R

3

1

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?

  • My Requirement:
    when application going to remove from recently open application at that time i will stop service which is already running in backgound.

    • Issue Faced:
      1) I have N number of activities...
      2) I also want to know you that when application going to remove from recent list. Android OS not called onDestroy() method even . Activities which is in activity stack.
Reparative answered 11/6, 2014 at 12:56 Comment(0)
R
2
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

Reparative answered 27/6, 2014 at 6:50 Comment(1)
So this is called from the Service...clever since you can't control it from an activity. It always depends if the Service is bound or started.Prohibitive
D
4

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

Dear answered 11/6, 2014 at 13:15 Comment(2)
i have N number of activities... 2) I can not cancel service at onDestroy()... because service should be cancel only above mention situation.Reparative
i also want to know you that. even when application going to remove from recent list. Android OS not called onDestroy() method of any of the activity which in stack.Reparative
R
2
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

Reparative answered 27/6, 2014 at 6:50 Comment(1)
So this is called from the Service...clever since you can't control it from an activity. It always depends if the Service is bound or started.Prohibitive
M
1

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>
Matlock answered 12/6, 2014 at 5:35 Comment(1)
let me know is there any method which will be called when application is going to kill... in inbuild broadcast or interface etcReparative

© 2022 - 2024 — McMap. All rights reserved.