When calling:

await AndroidAlarmManager.oneShot(Duration(seconds: 1), 17, callback)

in my timer class, with callback function like:

  static void alarmCallback(){
    print('>>>>>>>>>>>>>>>>>>>> ALARM CALLBACK <<<<<<<<<<<<<<<<< ');
  }

I got error:

E/AndroidRuntime( 9115): FATAL EXCEPTION: AsyncTask #2
E/AndroidRuntime( 9115): Process: com.myapp, PID: 9115
E/AndroidRuntime( 9115): java.lang.RuntimeException: An error occurred while executing doInBackground()
E/AndroidRuntime( 9115):    at android.os.AsyncTask$3.done(AsyncTask.java:354)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:383)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.setException(FutureTask.java:252)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.run(FutureTask.java:271)
E/AndroidRuntime( 9115):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
E/AndroidRuntime( 9115):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
E/AndroidRuntime( 9115):    at java.lang.Thread.run(Thread.java:764)
E/AndroidRuntime( 9115): Caused by: java.lang.RuntimeException: Methods marked with @UiThread must be executed on the main thread. Current thread: AsyncTask #2
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.FlutterJNI.ensureRunningOnMainThread(FlutterJNI.java:794)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.FlutterJNI.dispatchPlatformMessage(FlutterJNI.java:684)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.dart.DartMessenger.send(DartMessenger.java:80)
E/AndroidRuntime( 9115):    at io.flutter.embedding.engine.dart.DartExecutor.send(DartExecutor.java:189)
E/AndroidRuntime( 9115):    at io.flutter.view.FlutterNativeView.send(FlutterNativeView.java:155)
E/AndroidRuntime( 9115):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:98)
E/AndroidRuntime( 9115):    at io.flutter.plugin.common.MethodChannel.invokeMethod(MethodChannel.java:84)
E/AndroidRuntime( 9115):    at io.flutter.plugins.androidalarmmanager.AlarmService.invokeCallbackDispatcher(AlarmService.java:170)
E/AndroidRuntime( 9115):    at io.flutter.plugins.androidalarmmanager.AlarmService.onHandleWork(AlarmService.java:151)
E/AndroidRuntime( 9115):    at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:392)
E/AndroidRuntime( 9115):    at androidx.core.app.JobIntentService$CommandProcessor.doInBackground(JobIntentService.java:383)
E/AndroidRuntime( 9115):    at android.os.AsyncTask$2.call(AsyncTask.java:333)
E/AndroidRuntime( 9115):    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
E/AndroidRuntime( 9115):    ... 3 more
D/[email protected][MainActivity]( 9115): MSG_WINDOW_FOCUS_CHANGED 0 1
D/InputMethodManager( 9115): prepareNavigationBarInfo() [email protected][MainActivity]
D/InputMethodManager( 9115): getNavigationBarColor() -855310
I/Process ( 9115): Sending signal. PID: 9115 SIG: 9
Lost connection to device.

I implemented android_alarm_manager as in official documentation in https://pub.dev/packages/android_alarm_manager

  • added permission, services and receives to manifest
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.WAKE_LOCK" />
        <service
            android:name="io.flutter.plugins.androidalarmmanager.AlarmService"
            android:permission="android.permission.BIND_JOB_SERVICE"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver"
            android:exported="false"/>
        <receiver
            android:name="io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver"
            android:enabled="false">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
  • await AndroidAlarmManager.initialize(); is called when app is lunching
  • PluginRegistrant done with new class (same name as app name in manifest)
<application
        android:name=".Application"
public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        AlarmService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

Alarm is already initialized - before oneShot() is called:

I/AlarmService( 9115): Starting AlarmService...
I/AlarmService( 9115): AlarmService started!

I already was looking for that error, for example here:

Flutter Android Alarm Manager not working

or here:

Android Alarm Manager is not working for Flutter Project App


Solution 1: Omatt

If you're calling await AndroidAlarmManager.initialize(); before runApp() and a runtime error was thrown, add WidgetsFlutterBinding.ensureInitialized(); before the initialization.

main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await AndroidAlarmManager.initialize();
  runApp(...);
}

Also, use android_alarm_manager_plus instead of android_alarm_manager as it has been deprecated.