← Plugins / Background Mode
This community plugin keeps your app running after it moves to the background, instead of being paused by the OS. It's useful for music players, trackers and timers that need to keep working while the user is in another app.
cordova plugin add cordova-plugin-background-mode
The plugin is exposed as cordova.plugins.backgroundMode after deviceready:
| Method | Description |
|---|---|
enable() / disable() | Turn background mode on or off. |
isActive() | Returns true while the app is actually running in the background. |
on(event, cb) | Listen for enable, disable, activate, deactivate or failure. |
setDefaults(options) | Configure the Android foreground notification (title, text, icon, silent). |
Enable background mode and keep a timer ticking once the app is hidden:
document.addEventListener("deviceready", onReady, false);
function onReady() {
var bg = cordova.plugins.backgroundMode;
bg.setDefaults({ title: "DedrisApp", text: "Running in the background" });
bg.enable();
bg.on("activate", function () {
console.log("Now in the background — keep working");
});
}
disable() as soon as the background work is done to return battery to normal.setDefaults.