Acquiring an Android WakeLock in NativeScript App


#1

Hey folks. I’ve almost finished an app that integrates timers as part of the UI. The user creates a timer that runs for up to ~100 minutes but could run for as little as 1 second, with the expectation that an alarm is guaranteed to go off at the end of this process, whether or not they’ve been using their phone. When I run this application on my phone, after the phone’s screen has been locked for a while (I’m not sure exactly how long, but probably 10-30 minutes), alarms don’t go off and the app relaunches to the home page. I ideally want the state of the app to be completely preserved and the processing that goes on in my Angular component to continue while the phone is locked. Yes, I know this is a drain on battery life.

Right now, the code I’m using to enforce a partial wake lock in Android looks something like this:

acquireWakeLock() {
        //enforce a new partial wake lock
        const PowerManager = android.os.PowerManager;
        let powerManager = <PowerManager>application.android.context.getSystemService(android.content.Context.POWER_SERVICE);
        this.wakelock = <PowerManager.WakeLock>powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "RoutineTimer");
        this.wakelock.acquire();
        console.log("Acquired new wake lock");
}

(Forgive the verbose references, but I’m putting them here to distinguish between “android” and “application.android”.)

This doesn’t have the desired effect at all. It’s being called from an Angular component but is housed in an Angular service.

Does anyone know what’s up with this or have other recommendations? I’d prefer not to use a full (screen) wake lock, though I know there’s a plugin for that. I’d also prefer not to send in any kind of broadcast from a server for financial reasons.