Solution 1 :
You will have around ~40 seconds to call your function and call the completion handler in the background state or phone is in the sleep state.
If you don’t finish it on time, iOS will kill your app. If you don’t obey the rules, iOS will give you less chances to wake up. For more detailed usage of Background Modes, you may check following tutorial
It is due to app developers abusing this mechanism, killing their customers’ device battery.
Note: Watchdog Timeout is ~40 sec.
Problem :
I am using BackgroundTimer
from react-native-background-timer
in a React native project for Android.
I want a user to be able to record audio after a set duration (e.g. record for 5 minutes after 30 minutes).
The issue I have is that it works “sometimes”. It seems the OS is often killing the process – especially if the time to execute is longer.
Code extract:
import BackgroundTimer from 'react-native-background-timer';
...
// Start a timer that runs once after X milliseconds
const timeout1 = BackgroundTimer.setTimeout(() => {
// Should work when when app is the the background
this.onStartRecord();
const timeout2 = BackgroundTimer.setTimeout(() => {
this.onStopRecord();
BackgroundTimer.clearTimeout(timeout2);
}, millisRecording);
// Clear first timeout
BackgroundTimer.clearTimeout(timeout1);
}, millisUntilRecordMoment);
Thes rest of the code is here: https://github.com/bentaly/listen/blob/master/App.tsx
I have also tried building this in Expo previously which didn’t work.
Is there anyway to guarantee this operation will work?
Comments
Comment posted by Ben Taliadoros
Hi Muhammad, did you mean to link an article? Also this question specifically asked about Android
Comment posted by Muhammad Numan
“It seems the OS is often killing the process – especially if the time to execute is longer.” it seems of ios question
Comment posted by Ben Taliadoros
OS stands for operating system, such as iOS, Android, Windows, Linux etc
Comment posted by Muhammad Numan
@BenTaliadoros there was the same issue on ios, he played silent 1-second audio every 8 seconds to prevent killing the process. but he said I got this issue on android only Huawei devices
Comment posted by Ben Taliadoros
I will but I won’t be able for a few days, if it works I’ll let you know and you can update as a separate answer