How can I make application to never exit but to work in background, or how can I call a function on app exit?

I’m using IONIC to build one application. I’m using cordova background mode package. I manage to make it to work in background, when display is turned of and etc. I’m also using Power Management package from cordova. My code in app.component file is like below:

initializeApp() {
    this.platform.ready().then(() => {
        this.statusBar.styleDefault();
        this.splashScreen.hide();
        this.statusBar.overlaysWebView(true);
        this.statusBar.isVisible = true;
        this.statusBar.show();

        this.backgroundMode.enable();

        this.powerManagement.acquire()
            .then(() => {
            })
            .catch(() => {
            });
    });
}

In my AndroidManifest.xml I’m using code like this:

<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />

But when I close my application from opened applications list it kills application and doesn’t proceed to run in background.

2 Likes