How to Run the Mobile Project in the Background?

1 favourites
  • 4 posts
From the Asset Store
Full game Construct 2 and Construct 3 to post on Google Play
  • Project Context/Purpose: In a part of my Android project, the user can set their training time per repetition and rest, for example: "I want to run for 1 minute, rest for 30 seconds, and I want to do 3 repetitions of this." The app counts the 1 minute, triggers a sound alert indicating the rest time, and starts counting the 30 seconds.

    However, this obviously only works if the app is open, and in this context, I need it to run in the background, as it is not possible to count the time difference from when the app is closed until it is opened again, since the user will not open the app during the run. I generated a Cordova export and added the "cordova-plugin-background-mode" plugin, configured it in the config, everything is set up, I even made this script to disable it as soon as the project opens, since I don't need it running in the background all the time, only at specific moments:

    console.log("background-mode.js loaded");

    document.addEventListener('deviceready', function () {

    console.log("Cordova is ready");

    cordova.plugins.backgroundMode.disableWebViewOptimizations();

    cordova.plugins.backgroundMode.disableBatteryOptimizations();

    cordova.plugins.backgroundMode.setDefaults({

    title: 'App in Background',

    text: 'Your app is running in the background',

    icon: 'icon', // Icon name in the res/drawable folder

    color: 'F14F4D', // Notification color

    resume: true,

    hidden: false,

    bigText: false

    });

    cordova.plugins.backgroundMode.enable();

    }, false);

    And I created two functions to enable and disable it:

    function enableBackgroundMode() {

    console.log("Enabling background mode");

    cordova.plugins.backgroundMode.enable();

    }

    function disableBackgroundMode() {

    console.log("Disabling background mode");

    cordova.plugins.backgroundMode.disable();

    }

    It is called when the user starts the run and disabled when they finish. I can see from the console log that it is running as it should, but whenever the phone locks the screen or the app goes into the background, the project pauses anyway. Is there any way to work around this?

  • I am not sure what you are trying to do, but most people that want something like what you are describing are usually interested in keeping some kind of simulation going, even when the game is closed.

    Games that do this keep track of the last known time they were active and then look at the new current time when the game becomes active again, with both numbers they calculate the difference.

    With that difference the game then simulates all the things that would have happened in that time, should it have been active.

    This is the correct way to approach the problem, because you can never know what the OS will decide is better for your application. This is particularly true for mobile devices where the OS will aggressively try to save battery power, and in most cases that means stopping any process which is deemed not useful, such as applications that are in the background and not being interacted with.

  • I understand your point, but as I mentioned, accounting for and calculating the app's inactivity time will not work for my project, as I need the app to continue counting the user's training time even if it is in the background or with the screen locked. This is necessary so that at specific moments it can trigger a sound alert indicating when it is time to run or rest, and the sound alert only happens at specific moments.

    The way you explained it, it will only work if the user returns to the app, then it will calculate if the time has come to trigger the alert. But that's not what I want, nor is it what the user expects. If you download an app to manage your running time, to notify you when to run and when to stop, you at least expect it to do so with the screen locked or while you are using another app, and not to stop working in either of those cases.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • In that case I don't think Construct is the right tool for the job. Web applications just don't lend themselves to that use case because you don't have control over what the underlaying browser wants to do with them.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)