Stop my app from sleeping (Cordova / Android)

I have an Android app that I am creating from a node.js webpack project.

When I install my application on my phone, I notice that it is sleeping when the phone is sleeping. For example, I have a javascript timer that stops receiving:

pingTimer=setInterval(ping,pingInterval); 

for pingInterval. How to disconnect the application from sleep? In the end, I want my application to fall asleep, but now stopping sleep from sleeping is my best option.

UPDATE

I followed the instructions described here:

http://www.greenbot.com/article/2993199/android/how-to-turn-off-doze-mode-for-specific-apps-in-android-marshmallow.html

but no luck.

+5
source share
3 answers

The best option for you is the WakeLock api

Add permission to manifest file for wakeLock

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

then you can add the following according to your changes, as shown in the MDN behavior

 function forPingTimer(){ var lock = window.navigator.requestWakeLock('screen'); //set timeout or until the timer expires } 

and release the lock using the lock.unlock(); function lock.unlock(); .

OR

For the cordova application, you can also use the insomnia plugin . The changes that must be made to the config file are specified in the documents and you can simply use it as follows:

 function forPingTimer(){ //as long as the app runs or set the timeout here or wrap it in a promise //Simply calling window.plugins.insomnia.keepAwake() to keep awake } //window.plugins.insomnia.allowSleepAgain() to sleep again until the timer after the timer is fulfilled 
+7
source

The easiest way to prevent the pairing of your Android cordon app is to use the Insomnia-PhoneGap plugin. This plugin is also supported on most platforms such as Android, iOS and windows.

This plugin is easy to use. The keepAwake function in the plugin prevents the device from sleeping, and a call to the allowSleepAgain function allows the device to sleep again. You can find more information about the official insomnia plugin plugin .

Hope this helps. Greetings.

+4
source

To simply understand the syntax, install the cordova-plugin-insomnia plugin. Then enable a simple simple timer in your code:

 // start an interval timer var mainloopid = setInterval(mainloop, 1000); function mainloop { // call the plugin every (say) one second to keep your app awake window.plugins.insomnia.keepAwake(); } 

You should periodically call the plugin's keepAwake method (at least before your device wants to sleep.) This is necessary for all hours of display, and the interval timer can be part of your timer, which is used to update seconds in hours. This solution overrides the setting on your device, which says, for example, "sleep in 15 seconds." This is equivalent to setting up an Android device that never sleeps (while the application is running).

0
source

Source: https://habr.com/ru/post/1261154/


All Articles