jamesisaac / react-native-background-task

Periodic background tasks for React Native apps, cross-platform (iOS and Android), which run even when the app is closed.
MIT License
1.11k stars 110 forks source link

java.lang.IllegalStateException: Tried to start task BackgroundTask while in foreground, but this is not allowed #65

Open nicatasa opened 5 years ago

nicatasa commented 5 years ago

Phone: Xiaomi redmi note 4x

Tried to start task BackgroundTask while in foreground, but this is not allowed at (Headless.JsTaskContext.java: 92) at(Headless.JsTaskService.java: 125) and etc...

How to fix it ?

BackgroundTask.define(() => { GetCountryNames(); BackgroundTask.finish() }) BackgroundTask.schedule({ period: 10800 })

nicatasa commented 5 years ago

This error happens every 3hours (period: 108000) only xiaomi devices or only my device for some background restrics and permissions.

0xAnakin commented 5 years ago

java.lang.IllegalStateException: at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask (HeadlessJsTaskContext.java:92) at com.facebook.react.HeadlessJsTaskService$2.run (HeadlessJsTaskService.java:125) at android.os.Handler.handleCallback (Handler.java:790) at android.os.Handler.dispatchMessage (Handler.java:99) at android.os.Looper.loop (Looper.java:171) at android.app.ActivityThread.main (ActivityThread.java:6656) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)

Any update on this? the error occurs in multiple Android versions 7-9 and others

JaydeepJikadra commented 5 years ago

Facing the Same Issue.

OS Version: 9

Device: ONEPLUS A6010

RAM Free: 28.8%

Disk Free: 52.2%

0. Crashed: main

   at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:92)
   at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:125)
   at android.os.Handler.handleCallback(Handler.java:873)
   at android.os.Handler.dispatchMessage(Handler.java:99)
   at android.os.Looper.loop(Looper.java:193)
   at android.app.ActivityThread.main(ActivityThread.java:6863)
   at java.lang.reflect.Method.invoke(Method.java)
   at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

--

Fatal Exception: java.lang.IllegalStateException: Tried to start task BackgroundTask while in the foreground, but this is not allowed. at com.facebook.react.jstasks.HeadlessJsTaskContext.startTask(HeadlessJsTaskContext.java:92) at com.facebook.react.HeadlessJsTaskService$2.run(HeadlessJsTaskService.java:125) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6863) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

sefam-vbastien commented 5 years ago

It seems this is a behavior wanted by react native as stated here : https://facebook.github.io/react-native/docs/headless-js-android

By default, your app will crash if you try to run a task while the app is in the foreground. This is to prevent developers from shooting themselves in the foot by doing a lot of work in a task and slowing the UI. You can pass a fourth boolean argument to control this behaviour.

Until you set this boolean to true this will crash.

The library does not set this boolean which means it is set to default (false) https://github.com/jamesisaac/react-native-background-task/blob/3db6df10d80dbbd8e8fd3ee1c964bd332decc5e0/android/src/main/java/com/jamesisaac/rnbackgroundtask/HeadlessTaskService.java#L26

To fix this issue you have 2 choices: Either you set the boolean to true but this will slow your UI if in foreground mode Either you can do the isAppOnForeground check as state in the official documentation.

Hope this help.

qlerebours commented 5 years ago

I tried to set this boolean to true and it seems to be working fine. Thank you

vahidrn98 commented 4 years ago

i was facing this issue even after setting foregroundattribute to true. then i found out i was stupidly setting a periodic job with setInterval() function inside the task. deleting that solved my problem.