diff --git a/README.md b/README.md index d1e7e09..e0da868 100644 --- a/README.md +++ b/README.md @@ -113,6 +113,13 @@ Override the back button on Android to go to background instead of closing the a cordova.plugins.backgroundMode.overrideBackButton(); ``` +### Recent task list +Exclude the app from the recent task list works on Android 5.0+. + +```javascript +cordova.plugins.backgroundMode.excludeFromTaskList(); +``` + ### Notification To indicate that the app is executing tasks in background and being paused would disrupt the user, the plug-in has to create a notification while in background - like a download progress bar. diff --git a/plugin.xml b/plugin.xml index 77717e0..a3f81ba 100644 --- a/plugin.xml +++ b/plugin.xml @@ -102,6 +102,7 @@ + diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 653f915..350727c 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -22,10 +22,12 @@ package de.appplant.cordova.plugin.background; import android.app.Activity; +import android.app.ActivityManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.ServiceConnection; +import android.os.Build; import android.os.IBinder; import android.view.View; @@ -36,6 +38,7 @@ import org.json.JSONException; import org.json.JSONObject; import java.lang.reflect.Method; +import java.util.List; public class BackgroundMode extends CordovaPlugin { @@ -114,6 +117,10 @@ public class BackgroundMode extends CordovaPlugin { moveToForeground(); } + if (action.equalsIgnoreCase("tasklist")) { + excludeFromTaskList(); + } + if (action.equalsIgnoreCase("enable")) { enableMode(); } @@ -333,6 +340,33 @@ public class BackgroundMode extends CordovaPlugin { thread.start(); } + /** + * Exclude the app from the recent tasks list. + */ + private void excludeFromTaskList() { + ActivityManager am = (ActivityManager) cordova.getActivity() + .getSystemService(Context.ACTIVITY_SERVICE); + + if (am == null || Build.VERSION.SDK_INT < 21) + return; + + try { + Method getAppTasks = am.getClass().getMethod("getAppTasks"); + List tasks = (List) getAppTasks.invoke(am); + + if (tasks == null || tasks.isEmpty()) + return; + + ActivityManager.AppTask task = (ActivityManager.AppTask) tasks.get(0); + Method setExcludeFromRecents = task.getClass() + .getMethod("setExcludeFromRecents", boolean.class); + + setExcludeFromRecents.invoke(task, true); + } catch (Exception e) { + e.printStackTrace(); + } + } + /** * Fire vent with some parameters inside the web view. * diff --git a/www/background-mode.js b/www/background-mode.js index edfa25c..a34d922 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -158,6 +158,17 @@ exports.moveToForeground = function () { } }; +/** + * Exclude the app from the recent tasks list (Android only). + * + * @return [ Void ] + */ +exports.excludeFromTaskList = function () { + if (this._isAndroid) { + cordova.exec(null, null, 'BackgroundMode', 'tasklist', []); + } +}; + /** * Override the back button on Android to go to background * instead of closing the app.