Search code examples
flutterdartalarmmanageralarm

How to automatically awake the application in flutter?


I am coding an alarm on my own and I would like that the alarm would wake up the phone and show the Alert Dialog. Can someone give me some insight on how to do this please?


Solution

  • You can use android_alarm_maganer to achive what you need. Simply run it every second or so and check if DateTime.now() meets your criteria.

    Example:

    import 'package:android_alarm_manager/android_alarm_manager.dart';
    
    void checkAlarms() {
      if(DateTime().now == alarm){
       //Do something
      }
    }
    
    main() async {
      final int helloAlarmID = 0;
      await AndroidAlarmManager.initialize();
      runApp(...);
      await AndroidAlarmManager.periodic(const Duration(seconds: 1), helloAlarmID, checkAlarms);
    }
    

    Then you can run another Activity as described here