Skip to content

Commit 766140a

Browse files
committed
A separate background boolean API added.
1 parent 79da4a8 commit 766140a

File tree

2 files changed

+49
-2
lines changed

2 files changed

+49
-2
lines changed

src/android/BackgroundModeExt.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public class BackgroundModeExt extends CordovaPlugin {
9090
private int prevVisibility;
9191
final String RECEIVER = ".AlarmReceiver";
9292
final int TIMEOUT = 120 * 1000;
93-
93+
private boolean isOnBg = false;
9494

9595
private class AlarmReceiver extends BroadcastReceiver {
9696
private PowerManager.WakeLock wakeLock;
@@ -116,7 +116,7 @@ public void onReceive(Context context, Intent intent) {
116116
wfl.acquire();
117117
}
118118

119-
if(isDimmed()) {
119+
if(isOnBg()) {
120120
getApp().runOnUiThread(() -> {
121121
View view = webView.getEngine().getView();
122122
int visibility = view.getVisibility();
@@ -172,6 +172,12 @@ public boolean execute (String action, JSONArray args,
172172
break;
173173
case "disableWake":
174174
disablePartialWake();
175+
break;
176+
case "toBackground":
177+
toBackground();
178+
break;
179+
case "fromBackground":
180+
fromBackground();
175181
break;
176182
case "appstart":
177183
openAppStart(args.opt(0));
@@ -403,6 +409,21 @@ private boolean isDimmed()
403409
return !pm.isInteractive();
404410
}
405411

412+
private void toBackground()
413+
{
414+
isOnBg = true;
415+
}
416+
417+
private void fromBackground()
418+
{
419+
isOnBg = false;
420+
}
421+
422+
private boolean isOnBg()
423+
{
424+
return isOnBg;
425+
}
426+
406427
/**
407428
* Wakes up the device if the screen isn't still on.
408429
*/

www/background-mode.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,32 @@ exports.moveToForeground = function()
246246
}
247247
};
248248

249+
/**
250+
* Disable visibility and keepalive in background (Android).
251+
*
252+
* @return [ Void ]
253+
*/
254+
exports.fromBackground = function()
255+
{
256+
if (this._isAndroid)
257+
{
258+
cordova.exec(null, null, 'BackgroundModeExt', 'fromBackground', []);
259+
}
260+
};
261+
262+
/**
263+
* Enable visibility and keepalive in background (Android).
264+
*
265+
* @return [ Void ]
266+
*/
267+
exports.toBackground = function()
268+
{
269+
if (this._isAndroid)
270+
{
271+
cordova.exec(null, null, 'BackgroundModeExt', 'toBackground', []);
272+
}
273+
};
274+
249275
/**
250276
* Exclude the app from the recent tasks list (Android only).
251277
*

0 commit comments

Comments
 (0)