Stop the service when uninstalling

The application is killed from the recently opened list of applications by scrolling.

In my application,
works in the background only when the application is available in the foreground or in the background . If the user has sent the application in the background by pressing the "home" button on the mobile phone, you should also start the service .


But when the user remove the application from the recently opened application , then
1) Are any methods performed automatically when they are removed from the list of recent applications?
2) What method will be called?

  • My requirements: When the application is removed from the recently opened application at that time, I will stop the service that is already running in backgound.

    • Problem:
      1) I have N activities ...
      2) I also want to know that when the application will be removed from the recent list. Android OS is not called the onDestroy () method. Actions that are in the action stack.
+2
source share
3 answers
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 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 it worked

+2
source

1.You can stop the service in onDestroy methods.

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

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

3.or You can also check if 'onTaskRemoved' is called. onTaskRemoved doc

+4
source

You must add permission to your manifests.

  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> 
+1
source

Source: https://habr.com/ru/post/972256/


All Articles