From a9877ec0a13c11625eb7ca59486be006d43ebda7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Katzer?= Date: Tue, 3 Jan 2017 22:06:50 +0100 Subject: [PATCH] Allow app to move to foreground (Android) --- src/android/BackgroundMode.java | 42 +++++++++++++++++++++++++-------- www/background-mode.js | 11 +++++++++ 2 files changed, 43 insertions(+), 10 deletions(-) diff --git a/src/android/BackgroundMode.java b/src/android/BackgroundMode.java index fa70495..fdebc8f 100644 --- a/src/android/BackgroundMode.java +++ b/src/android/BackgroundMode.java @@ -114,6 +114,11 @@ public class BackgroundMode extends CordovaPlugin { return true; } + if (action.equalsIgnoreCase("foreground")) { + moveToForeground(); + return true; + } + if (action.equalsIgnoreCase("enable")) { enableMode(); return true; @@ -162,6 +167,33 @@ public class BackgroundMode extends CordovaPlugin { stopService(); } + /** + * Move app to background. + */ + private void moveToBackground() { + Intent intent = new Intent(Intent.ACTION_MAIN); + + intent.addCategory(Intent.CATEGORY_HOME); + cordova.getActivity().startActivity(intent); + } + + /** + * Move app to foreground. + */ + private void moveToForeground() { + Context context = cordova.getActivity(); + String pkgName = context.getPackageName(); + + Intent intent = context + .getPackageManager() + .getLaunchIntentForPackage(pkgName); + + intent.addFlags( + Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_SINGLE_TOP); + + context.startActivity(intent); + } + /** * Enable the background mode. */ @@ -308,16 +340,6 @@ 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 063705d..50d3367 100644 --- a/www/background-mode.js +++ b/www/background-mode.js @@ -110,6 +110,17 @@ exports.moveToBackground = function () { } }; +/** + * Move app to foreground when in background (Android only). + * + * @return [ Void ] + */ +exports.moveToForeground = function () { + if (this.isActive() && device.platform == 'Android') { + cordova.exec(null, null, 'BackgroundMode', 'foreground', []); + } +}; + /** * Override the back button on Android to go to background * instead of closing the app.