plus_plugins icon indicating copy to clipboard operation
plus_plugins copied to clipboard

[Bug]: Alarm Callback Gets Paused Until the App is Reopened

Open ImranR98 opened this issue 2 years ago • 2 comments

Platform

Android 14

Plugin

android_alarm_manager_plus

Version

3.0.3

Flutter SDK

3.13.9 (stable)

Steps to reproduce

  1. Register a periodic task that runs for a few minutes at most (10 seconds at least).
  2. Ensure you have a way of seeing the task progress (via logs or a notification).
  3. Run the app in the foreground and observe the periodic task running every X seconds as expected.
  4. Close the app (using the phone's back button - do not force stop or clear from memory).
  5. Keep looking at the logs. We expect the periodic task to continue running every X seconds as expected.
  6. Instead, the task gets paused before it can complete (this doesn't always happen but you should see it every 2-3 loops at least).
  7. When you reopen the app, the task continues as normal.

This only seems to happen on Android 14 (API 34). On older versions, the task runs normally every time.

I could not attach the full logs in the logs section (comment too long) so I only included the most relevant parts. The full log is here: logs.txt

Code Sample

import 'package:flutter/material.dart';
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';

@pragma('vm:entry-point')
Future<void> bgTask() async {
  print('BG task: started');
  for (int i = 0; i < 10; i++) {
    await Future.delayed(const Duration(seconds: 1));
    print('BG task: loop $i');
  }
  print('BG task: ended');
}

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await AndroidAlarmManager.initialize();
  runApp(const MainApp());
  const int bgTaskId = 0;
  await AndroidAlarmManager.periodic(
      const Duration(minutes: 1), bgTaskId, bgTask);
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Center(
          child: Text('Hello World!'),
        ),
      ),
    );
  }
}

Logs

I started the app and waited for the task to start after 1 minute. In the meantime, I moved the app to the background using the back button. When the task started, it ran for 9 seconds (as seen in the logs) then got paused. I waited a while and brought the app back to the foreground. At that point, the task continued and finished. I then waited a few seconds to see if there would be any additional logging (there wasn't) then hit ctrl-c to kill Flutter (end of logs).


...
[   +4 ms] The Flutter DevTools debugger and profiler on sdk gphone64 x86 64 is available at: http://127.0.0.1:9101?uri=http://127.0.0.1:43395/Symp0AhletI=/
[+7696 ms] D/EGL_emulation( 4143): app_time_stats: avg=3922.47ms min=1.28ms max=7843.65ms count=2
[ +470 ms] I/AndroidAlarmManagerPlugin( 4143): onDetachedFromEngine
[  +62 ms] W/WindowOnBackDispatcher( 4143): sendCancelIfRunning: isInProgress=falsecallback=android.view.ViewRootImpl$$ExternalSyntheticLambda17@bc3245c
[+51842 ms] D/CompatibilityChangeReporter( 4143): Compat change id reported: 194532703; UID 10187; state: ENABLED
[        ] D/CompatibilityChangeReporter( 4143): Compat change id reported: 253665015; UID 10187; state: DISABLED
[   +9 ms] W/v.imranr.killme( 4143): Cleared Reference was only reachable from finalizer (only reported once)
[  +72 ms] I/flutter ( 4143): BG task: started
[+1012 ms] I/flutter ( 4143): BG task: loop 0
[+1001 ms] I/flutter ( 4143): BG task: loop 1
[+1005 ms] I/flutter ( 4143): BG task: loop 2
[+1004 ms] I/flutter ( 4143): BG task: loop 3
[+1001 ms] I/flutter ( 4143): BG task: loop 4
[+1003 ms] I/flutter ( 4143): BG task: loop 5
[+1004 ms] I/flutter ( 4143): BG task: loop 6
[+1002 ms] I/flutter ( 4143): BG task: loop 7
[+1003 ms] I/flutter ( 4143): BG task: loop 8
[+32064 ms] I/AndroidAlarmManagerPlugin( 4143): onAttachedToEngine
[   +5 ms] D/AutofillManager( 4143): Fill dialog is enabled:false, hints=[password, passwordAuto, creditCardNumber, creditCardSecurityCode, creditCardExpirationDate]
[  +49 ms] W/OpenGLRenderer( 4143): Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
[        ] W/OpenGLRenderer( 4143): Failed to initialize 101010-2 format, error = EGL_SUCCESS
[   +8 ms] E/OpenGLRenderer( 4143): Unable to match the desired swap behavior.
[ +135 ms] W/AlarmService( 4143): Attempted to start a duplicate background isolate. Returning...
[ +234 ms] I/flutter ( 4143): BG task: loop 9
[        ] I/flutter ( 4143): BG task: ended

Flutter Doctor

[✓] Flutter (Channel stable, 3.13.9, on Fedora Linux 39 (Workstation Edition) 6.5.7-300.fc39.x86_64, locale en_CA.UTF-8)
    • Flutter version 3.13.9 on channel stable at /home/imranr/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision d211f42860 (2 days ago), 2023-10-25 13:42:25 -0700
    • Engine revision 0545f8705d
    • Dart version 3.1.5
    • DevTools version 2.25.0

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /home/imranr/Android/Sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /home/imranr/android-studio/jbr/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /usr/bin/chromium-browser

[✓] Linux toolchain - develop for Linux desktop
    • clang version 17.0.2 (Fedora 17.0.2-1.fc39)
    • cmake version 3.27.7
    • ninja version 1.11.1
    • pkg-config version 1.9.5

[✓] Android Studio (version 2022.3)
    • Android Studio at /home/imranr/android-studio
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b829.9-10027231)

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Fedora Linux 39 (Workstation Edition) 6.5.7-300.fc39.x86_64
    • Chrome (web)    • chrome • web-javascript • Chromium 118.0.5993.70 Fedora Project

[✓] Network resources
    • All expected network resources are available.

• No issues found!

Checklist before submitting a bug

  • [X] I searched issues in this repository and couldn't find such bug/problem
  • [X] I Google'd a solution and I couldn't find it
  • [X] I searched on StackOverflow for a solution and I couldn't find it
  • [X] I read the README.md file of the plugin
  • [X] I'm using the latest version of the plugin
  • [X] All dependencies are up to date with flutter pub upgrade
  • [X] I did a flutter clean
  • [X] I tried running the example project

ImranR98 avatar Oct 28 '23 03:10 ImranR98

Try overriding/handling sendCancelIfRunning:

sendCancelIfRunning: isInProgress=false callback=android.view.ViewRootImpl$$ExternalSyntheticLambda

and somehow setting isInProgress=true instead

(I haven't figured out how yet.)

Another test to try (but not allowed in Play store, at least not without a very good reason):

intent.action = Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS
intent.data = Uri.parse("package:$packageName")

halsampson avatar Nov 11 '23 15:11 halsampson

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 15 days

github-actions[bot] avatar Feb 10 '24 00:02 github-actions[bot]