Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: exact android alarm has 18 hour window of error #3262

Open
8 tasks done
insertokname opened this issue Sep 20, 2024 · 5 comments
Open
8 tasks done

[Bug]: exact android alarm has 18 hour window of error #3262

insertokname opened this issue Sep 20, 2024 · 5 comments
Labels
bug Something isn't working triage

Comments

@insertokname
Copy link

Platform

Android 14

Plugin

android_alarm_manager_plus

Version

4.0.4

Flutter SDK

3.22.2

Steps to reproduce

Add the package to the project, and in the main file schedule an exact alarm to run in 1 minute.
I have also set battery optimizations to unrestricted through android settings.

  await AndroidAlarmManager.initialize();

  final startAt = DateTime.now().add(const Duration(minutes: 1));

  await AndroidAlarmManager.periodic(
    const Duration(days: 1),
    0,
    callbackDispatcher,
    startAt: startAt,
    exact: true,
    wakeup: true,
    rescheduleOnReboot: true,
    allowWhileIdle: true,
  );

callbackDispatcher is a simple function that just logs something.
the alarm will not run in exactly 1 minute, and looking at the output of adb shell dumpsys alarm

RTC_WAKEUP #1: Alarm{37037cd type 0 origWhen 1726819223832 whenElapsed 2140998 com.insertokname.sala_management}
      tag=*walarm*:com.insertokname.sala_management/dev.fluttercommunity.plus.androidalarmmanager.AlarmBroadcastReceiver
      type=RTC_WAKEUP origWhen=2024-09-20 11:00:23.832 window=+18h0m0s0ms repeatInterval=86400000 count=0 flags=0x8
      policyWhenElapsed: requester=-6m29s581ms app_standby=-7m29s545ms device_idle=-- battery_saver=-6m15s240ms tare=-7m29s545ms gms_manager=--
      whenElapsed=-6m15s240ms maxWhenElapsed=+17h53m30s419ms
      operation=PendingIntent{d566882: PendingIntentRecord{1870c8d com.insertokname.sala_management broadcastIntent}}
        

I can see a window of 18 hours. Does this mean that the alarm can have an error of up to 18 hours? If so is there any way of working around it?

Code Sample

await AndroidAlarmManager.initialize();

  final startAt = DateTime.now().add(const Duration(minutes: 1));

  await AndroidAlarmManager.periodic(
    const Duration(days: 1),
    0,
    callbackDispatcher,
    startAt: startAt,
    exact: true,
    wakeup: true,
    rescheduleOnReboot: true,
    allowWhileIdle: true,
  );

Logs

since logs are 6k lines:

https://justpaste.it/7tvwd

Flutter Doctor

[✓] Flutter (Channel stable, 3.22.2, on NixOS 24.11 (Vicuna) 6.10.1, locale en_US.UTF-8)
    • Flutter version 3.22.2 on channel stable at
      /nix/store/c9fxvvl8n4v0w679p82wpgspzn4kyqbr-flutter-wrapped-3.22.2-sdk-links
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision nixpkgs000 (), 1970-01-01 00:00:00
    • Engine revision edd8546116
    • Dart version 3.4.3
    • DevTools version 2.34.3

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_HOME =
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • ANDROID_SDK_ROOT =
      /nix/store/v8zpb7k7604z4zyxfcvbqa3ksb68lw0g-androidsdk/libexec/android-sdk
    • Java binary at:
      /nix/store/l7pwy1rxdrb13svqandm720d06fycd8c-openjdk-17.0.11+9/lib/openjdk/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.11+9-nixos)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE =
      /nix/store/rf8lcgpmylhd19cwzs18cvn8r7mxv7f9-chromedriver-unwrapped-127.0.6533.88/b
      in/chromedriver

[✓] Linux toolchain - develop for Linux desktop
    • clang version 18.1.8
    • cmake version 3.29.6
    • ninja version 1.12.1
    • pkg-config version 0.29.2

[!] Android Studio (not installed)
    • Android Studio not found; download from
      https://developer.android.com/studio/index.html
      (or visit https://flutter.dev/docs/get-started/install/linux#android-setup for
      detailed instructions).

[✓] Connected device (3 available)
    • SM F721B (mobile) • R5CT819305F • android-arm64  • Android 14 (API 34)
    • Linux (desktop)   • linux       • linux-x64      • NixOS 24.11 (Vicuna) 6.10.1
    • Chrome (web)      • chrome      • web-javascript • ChromeDriver 127.0.6533.88
      (a2d0cb026721e4644e489b8ebb07038ca4e4351c-refs/branch-heads/6533@{#1846})

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

! Doctor found issues in 1 category.

Checklist before submitting a bug

  • I searched issues in this repository and couldn't find such bug/problem
  • I Google'd a solution and I couldn't find it
  • I searched on StackOverflow for a solution and I couldn't find it
  • I read the README.md file of the plugin
  • I'm using the latest version of the plugin
  • All dependencies are up to date with flutter pub upgrade
  • I did a flutter clean
  • I tried running the example project
@insertokname insertokname added bug Something isn't working triage labels Sep 20, 2024
@vbuberen
Copy link
Collaborator

Add the package to the project, and in the main file schedule an exact alarm to run in 1 minute.

Did you configure your project to get EXACT_ALARM permission as it is required in last Android versions? It is mentioned in the README of the package as well: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_alarm_manager_plus#getting-started
https://developer.android.com/about/versions/14/changes/schedule-exact-alarms

@insertokname
Copy link
Author

insertokname commented Sep 20, 2024

Did you configure your project to get EXACT_ALARM permission as it is required in last Android versions? It is mentioned in the README of the package as well: https://github.com/fluttercommunity/plus_plugins/tree/main/packages/android_alarm_manager_plus#getting-started https://developer.android.com/about/versions/14/changes/schedule-exact-alarms

Thank you for your response!

Yes i think i have adequately requested the permission.

i have put

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

in my manifest

and used the permission_handler package to request the permission via Permission.scheduleExactAlarm.request()

Sorry for not mentioning this in the original report!

@insertokname
Copy link
Author

insertokname commented Oct 6, 2024

@miquelbeltran
Sorry for the disturbance, but is there any way you could take a look at this? Or direct me to someone that could help?

Could it have something to do with this?

On Android 12 (API level 31) and higher, the system invokes the alarm within one hour of the supplied trigger time, unless any battery-saving restrictions are in effect such as battery saver or Doze.

@miquelbeltran
Copy link
Member

Sorry, no clue. There are a couple of old tickets mentioning this is an issue with periodic alarms e.g. #323 and #487

My only idea for you would be to implement a simple Android (native) that schedules an alarm and see if you encounter the same issue as with the plugin. It could be that the plugin is misusing a parameter, but I have no idea. If you could provide a working Android (kotlin or java) example, we can then compare with the plugin code.

@insertokname
Copy link
Author

insertokname commented Oct 8, 2024

Hello sorry for the late response i couldn't try out a few solutions until now.

Before i get to writing kotlin i just want to make sure that this "recursive initialization" pattern wouldn't be an option:

// ignore_for_file: avoid_print

import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';

@pragma('vm:entry-point')
void printHello() {
  print("\n\n${DateTime.now()}: Ran scheduled task!!!\n\n"); //alarm code here
  initAlarmManager();  // setting another alarm to run in 1 minute
  print("\n\ninitialization done!\n\n");
}

Future<void> initAlarmManager() async {
  await AndroidAlarmManager.initialize();

  final startAt = DateTime.now().add(const Duration(minutes: 1));
  print("\n\nStart at will be at $startAt\n\n");

  final result = await AndroidAlarmManager.oneShotAt(
    startAt,
    0,
    printHello,
    exact: true,
    allowWhileIdle: true,
    wakeup: true,
    rescheduleOnReboot: true,
  );

  print("\n\nresult of the schedule was: $result\n\n");
}

This looks like it's working, running the alarm at a regular 1 minute interval, with exact scheduling, while the screen is off.

Although i get this warning:

W/AlarmService( 7568): Attempted to start a duplicate background isolate. Returning...

Also i know there is a small delay increase in the interval:

W/AlarmService( 7568): Attempted to start a duplicate background isolate. Returning...
I/flutter ( 7568): Start at will be at 2024-10-08 11:50:32.705673
...
W/AlarmService( 7568): Attempted to start a duplicate background isolate. Returning...
I/flutter ( 7568): Start at will be at 2024-10-08 11:51:32.778379

There is a small increase from 32.705673 to 32.778379, i can fix this in my actual use case, i just want to confirm the recursive initialization pattern works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

No branches or pull requests

3 participants