From d012080d2bf11b6168496636bd09fc988b04631a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 3 Jan 2017 21:45:58 +0100 Subject: [PATCH] Allow app to move to background (Android) --- src/android/BackgroundMode.java | 15 +++++++++++++++ www/background-mode.js | 11 +++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index 3a7f70d..fa70495 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -109,6 +109,11 @@ public class BackgroundMode extends CordovaPlugin { return true; } + if (action.equalsIgnoreCase("background")) { + moveToBackground(); + return true; + } + if (action.equalsIgnoreCase("enable")) { enableMode(); return true; @@ -303,6 +308,16 @@ public class BackgroundMode extends CordovaPlugin { thread.start(); } + /** + * Send app to background. + */ + private void moveToBackground() { + Intent intent = new Intent(Intent.ACTION_MAIN); + + intent.addCategory(Intent.CATEGORY_HOME); + cordova.getActivity().startActivity(intent); + } + /** * Fire vent with some parameters inside the web view. * diff --git a/www/background-mode.js b/www/background-mode.js index c2cf995..81ec97e 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -99,6 +99,17 @@ exports.disableWebViewOptimizations = function () { } }; +/** + * Move app to background (Android only). + * + * @return [ Void ] + */ +exports.moveToBackground = function () { + if (device.platform == 'Android') { + cordova.exec(null, null, 'BackgroundMode', 'background', []); + } +}; + /** * If the mode is enabled or disabled. *