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

[Android] fatalTried to start task BackgroundTask while in foreground, but this is not allowed #25

Closed lucianomlima closed 6 years ago

lucianomlima commented 6 years ago

I can not understand this error. My configuration is that below:

import React, { Component } from 'react';
import BackgroundTask from 'react-native-background-task';

// Async requests
const financialUpdate = async () => {};
const salesUpdate = async () => {};

BackgroundTask.define(async () => {
  console.log('=> Running background tasks...');
  await Promise.all([
    financialUpdate(),
    salesUpdate()
  ]);

  BackgroundTask.finish();
  console.log('=> Finish!');
});

export default class Home extends Component {
  componentDidMount() {
    this.enableBackgroundFetch();
  }

  async enableBackgroundFetch() {
    console.log('Enabling background fetch...');
    const status = await BackgroundTask.statusAsync();

    if (status.available) {
      console.log('Enabled!');
      BackgroundTask.schedule({
        period: 1800
      });
    }
  }
}

I supressed non relevant imports and methods. What I'm doing wrong?

wodCZ commented 6 years ago

Same issue here, app crashes on this error every time user want's to open the app after some time it was in background. Crash happens immediately after an attempt to open the app.

Here is some output from logcat:

logcat output around when the crash happened ``` 02-06 10:12:09.173 1579-4648/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 10:12:09.244 1579-4145/? I/ActivityManager: Start proc 9806:cz.NuArt.moyobo/u0a168 for activity cz.NuArt.moyobo/.MainActivity caller=com.miui.home 02-06 10:12:09.283 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 10:12:09.284 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 10:12:09.284 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=9806 TaskId:2969 02-06 10:12:21.200 1579-4650/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|654|176439445621391|382|2 02-06 10:12:21.201 1579-4650/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|415|176440742744464|157|2 02-06 10:12:21.205 3881-3897/? D/MiuiPerfServiceClient: interceptAndQueuing:9806|cz.NuArt.moyobo|654|382|running|null 02-06 10:12:21.207 3881-3897/? D/MiuiPerfServiceClient: interceptAndQueuing:9806|cz.NuArt.moyobo|415|157|running|null 02-06 10:12:24.631 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 10:18:31.369 1579-4278/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 10:18:31.426 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 10:18:31.426 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 10:18:31.426 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=9806 TaskId:2969 02-06 10:19:05.872 1579-4281/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|310|176592141294198|150|2 02-06 10:19:05.878 3881-3900/? D/MiuiPerfServiceClient: interceptAndQueuing:9806|cz.NuArt.moyobo|310|150|unknown|null 02-06 10:19:06.337 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 10:20:14.460 1579-4820/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 10:20:14.541 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 10:20:14.541 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 10:20:14.542 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=9806 TaskId:2969 02-06 10:20:15.998 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 10:20:52.740 3990-6029/? I/WtMemStrategy: process cz.NuArt.moyobo uid 10168 pid 9806 strategy:41800 02-06 10:20:52.741 1579-4821/? I/ActivityManager: Killing 9806:cz.NuArt.moyobo/u0a168 (adj 700): com.android.systemui: One Key Clean from pid 3990 02-06 10:20:53.493 1579-23284/? I/WindowManager: WIN DEATH: Window{a5d9b2d u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity} 02-06 10:20:53.494 1579-4823/? D/ActivityManagerInjector: cz.NuArt.moyobo is killed by AMS with adj 1001 ,do not allow restart 02-06 10:21:10.189 1579-4648/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 10:21:10.251 1579-4281/? I/ActivityManager: Start proc 12785:cz.NuArt.moyobo/u0a168 for activity cz.NuArt.moyobo/.MainActivity caller=com.miui.home 02-06 10:21:10.289 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 10:21:10.290 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 10:21:10.290 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=12785 TaskId:2984 02-06 10:21:21.641 1579-3981/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|555|176727290904823|401|2 02-06 10:21:21.642 1579-3981/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|205|176728093255344|91|2 02-06 10:21:21.646 3881-15397/? D/MiuiPerfServiceClient: interceptAndQueuing:12785|cz.NuArt.moyobo|555|401|running|null 02-06 10:21:21.647 3881-15397/? D/MiuiPerfServiceClient: interceptAndQueuing:12785|cz.NuArt.moyobo|205|91|running|null 02-06 10:21:26.744 1579-3979/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|207|176733943572477|95|2 02-06 10:21:26.747 3881-13444/? D/MiuiPerfServiceClient: interceptAndQueuing:12785|cz.NuArt.moyobo|207|95|running|null 02-06 10:25:08.261 1579-1622/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.266 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.270 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.278 1579-4645/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.288 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.291 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.293 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.308 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.326 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 10:25:08.571 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=3 02-06 10:25:08.574 1579-1627/? I/WindowManager: Waiting for drawn Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=4 02-06 10:25:08.574 1579-1627/? D/WindowManager: Window drawn win=Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity} 02-06 10:25:08.687 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 10:25:08.687 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=12785 TaskId:2984 02-06 10:25:33.361 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 11:39:36.051 1579-4649/? I/WindowManager: WIN DEATH: Window{fe9b014 u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity} 02-06 11:39:36.052 1579-3981/? I/ActivityManager: Process cz.NuArt.moyobo (pid 12785) has died 02-06 11:50:23.648 1579-1616/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 11:50:23.725 1579-4281/? I/ActivityManager: Start proc 21914:cz.NuArt.moyobo/u0a168 for activity cz.NuArt.moyobo/.MainActivity caller=com.miui.home 02-06 11:50:23.767 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 11:50:23.767 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 11:50:23.767 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=21914 TaskId:2984 02-06 11:50:24.384 21914-21914/? E/AndroidRuntime: FATAL EXCEPTION: main Process: cz.NuArt.moyobo, PID: 21914 java.lang.IllegalStateException: Tried to start task BackgroundTask while in 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:127) at android.os.Handler.handleCallback(Handler.java:754) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:163) at android.app.ActivityThread.main(ActivityThread.java:6205) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794) 02-06 11:50:24.391 1579-23284/? W/ActivityManager: Force finishing activity cz.NuArt.moyobo/.MainActivity 02-06 11:50:24.899 1579-1621/? W/ActivityManager: Activity pause timeout for ActivityRecord{f0c2f97 u0 cz.NuArt.moyobo/.MainActivity t2984 f} 02-06 11:50:25.027 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 11:50:34.922 1579-1621/? W/ActivityManager: Activity destroy timeout for ActivityRecord{f0c2f97 u0 cz.NuArt.moyobo/.MainActivity t2984 f} 02-06 11:50:39.057 1579-23284/? I/ActivityManager: Killing 21914:cz.NuArt.moyobo/u0a168 (adj 0): crash 02-06 11:51:03.797 1579-4145/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=cz.NuArt.moyobo/.MainActivity bnds=[487,567][609,689] (has extras)} from uid 10022 on display 0 02-06 11:51:03.851 1579-3592/? I/ActivityManager: Start proc 22029:cz.NuArt.moyobo/u0a168 for activity cz.NuArt.moyobo/.MainActivity caller=com.miui.home 02-06 11:51:03.903 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 11:51:03.903 3990-4102/? I/StatusController: Last foreground:com.miui.home uid:10022 Current foreground:cz.NuArt.moyobo uid:10168 02-06 11:51:03.903 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=22029 TaskId:2994 02-06 11:51:15.332 1579-4820/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|723|178387374899862|435|2 02-06 11:51:15.333 1579-4820/? D/PerfShielderService: cz.NuArt.moyobo|cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity|508|178388848336945|132|2 02-06 11:51:15.338 3881-3897/? D/MiuiPerfServiceClient: interceptAndQueuing:22029|cz.NuArt.moyobo|723|435|running|null 02-06 11:51:15.340 3881-3897/? D/MiuiPerfServiceClient: interceptAndQueuing:22029|cz.NuArt.moyobo|508|132|running|null 02-06 11:57:07.590 1579-1622/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.594 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.597 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.615 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.663 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.675 1579-21966/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.863 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.868 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.871 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=1 02-06 11:57:07.886 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=3 02-06 11:57:07.890 1579-1627/? I/WindowManager: Waiting for drawn Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity}: removed=false visible=true mHasSurface=true drawState=4 02-06 11:57:07.890 1579-1627/? D/WindowManager: Window drawn win=Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity} 02-06 11:57:08.206 3990-4102/? I/WtProcessController: MOVE TO FOREGROUND: cz.NuArt.moyobo 10168 02-06 11:57:08.206 3990-4102/? I/WtProcessController: FOREGROUND INFO: name=cz.NuArt.moyobo uid=10168 pid=22029 TaskId:2994 02-06 11:57:13.328 3990-4102/? I/StatusController: Last foreground:cz.NuArt.moyobo uid:10168 Current foreground:com.miui.home uid:10022 02-06 12:19:03.754 24860-24879/? D/Cal:D:MiPushManager: shouldInit(): 22029:24860, com.android.calendar:cz.NuArt.moyobo 02-06 12:19:03.921 24881-24910/? D/Cal:D:MiPushManager: shouldInit(): 22029:24881, com.android.calendar:cz.NuArt.moyobo 02-06 12:19:22.569 24373-24849/? E/SystemUpdateLocalAppManager: getDetailAppInfo null: cz.NuArt.moyobo 02-06 12:30:40.307 25903-25921/? D/Cal:D:MiPushManager: shouldInit(): 22029:25903, com.android.calendar:cz.NuArt.moyobo 02-06 12:47:52.279 26988-28084/? E/SystemUpdateLocalAppManager: getDetailAppInfo null: cz.NuArt.moyobo 02-06 13:00:01.683 29000-29017/? D/Cal:D:MiPushManager: shouldInit(): 22029:29000, com.android.calendar:cz.NuArt.moyobo 02-06 13:09:36.953 3990-6028/? I/WtMemStrategy: process cz.NuArt.moyobo uid 10168 pid 22029 strategy:41800 02-06 13:09:36.953 1579-21966/? I/ActivityManager: Killing 22029:cz.NuArt.moyobo/u0a168 (adj 901): com.android.systemui: One Key Clean from pid 3990 02-06 13:09:37.749 1579-4650/? I/WindowManager: WIN DEATH: Window{86bfeaf u0 cz.NuArt.moyobo/cz.NuArt.moyobo.MainActivity} 02-06 13:09:37.750 1579-4281/? D/ActivityManagerInjector: cz.NuArt.moyobo is killed by AMS with adj 1001 ,do not allow restart ```
lucianomlima commented 6 years ago

I change to react-native-background-fetch that supports now Android and iOS.