How to Pause or Stop the callback function of Android Alarm Manager in Flutter?

824

You have insert this line of code to stop or pause alarm

cancel = await AndroidAlarmManager.cancel(id);

pass the id of alarm that you want to stop.

Share:
824
Chethan CV
Author by

Chethan CV

Updated on December 20, 2022

Comments

  • Chethan CV
    Chethan CV over 1 year

    I am working on an Application wherein which I will have to pause or stop the callback function of Android Alarm Manager every time the user locks his phone.I dont know how to detect the user locks his phone as well.

    I tried using Application life cycle management but the result for when the user both locks and minimises the App are one and the same.Below is the code.Please have a look:

    import 'package:flutter/material.dart';
    import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
    import 'package:intl/intl.dart';
    
    import 'CommonPicker.dart';
    import 'fileUtils.dart';
    import 'package:android_alarm_manager/android_alarm_manager.dart';
    
    
    void callback(){
      print("I am in the isolate");
      DateTime now=DateTime.now().toLocal();
     print("Date time:");
     print(now);
     //operation to be performed
      print("last line of Isolate");
    }
    
    class AlertTime2 extends StatefulWidget {
      static const routeName = '/alertTime2';
    
      @override
      _AlertTime2State createState() => _AlertTime2State();
    }
    
    class _AlertTime2State extends State<AlertTime2> {
      String mm = "00";
      String ss = "00";
      var mmValue;
      var ssValue;
      var totalTimeInSec;
      String _selectedTime;
    
      @override
      void initState() {
        super.initState();
    
        FileUtils().readFromFile().then((String value) {
          setState(() {
            _selectedTime = value;
          });
        });
      }
    
    
      void alarm_managing_function(int t) async {
        await AndroidAlarmManager.initialize();
        await AndroidAlarmManager.periodic(Duration(seconds: t), 0, callback);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
            body: Column(
          children: <Widget>[
            Padding(
              padding: const EdgeInsets.fromLTRB(30, 70, 0, 0),
              child: Text(
                "Timer for Notification",
                style: TextStyle(
                  fontSize: 50,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(0, 150, 0, 0),
              child: Text(
                "MM-SS",
                style: TextStyle(
                  fontSize: 30,
                  fontWeight: FontWeight.bold,
                ),
              ),
            ),
            Padding(
              padding: const EdgeInsets.fromLTRB(0, 0, 0, 0),
              child: Text(
                "${_selectedTime ?? 'Selected Time'}",
                style: TextStyle(fontWeight: FontWeight.bold, fontSize: 38),
              ),
            ),
            RaisedButton(
              onPressed: () {
                DatePicker.showPicker(context,
                    showTitleActions: true,
                    pickerModel:
                        CustomPicker(currentTime: DateTime.tryParse(_selectedTime)),
                    onConfirm: (time) {
                  setState(() {
                    print(time);
                    _selectedTime = DateFormat("mm-ss").format(time);
                    FileUtils().saveToFile(_selectedTime);
    
                    mm = _selectedTime.substring(0, 2);
                    ss = _selectedTime.substring(3, 5);
                    print(mm);
                    print(ss);
                    mmValue = int.parse(mm);
                    ssValue = int.parse(ss);
                    totalTimeInSec = mmValue * 60 + ssValue;
                    print(totalTimeInSec);
                    DateTime now=DateTime.now().toLocal();
                    alarm_managing_function(totalTimeInSec);
                  });
                }, locale: LocaleType.en);
              },
              child: Text("Show Time picker"),
            ),
    
            ),
          ],
        ));
      }
    }