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

while i am using "android_alarm_manager_plus", I failed it to create "createAlarm" in the callback function #9

Open
boziyoung opened this issue Sep 26, 2022 · 2 comments
Assignees
Labels
bug Something isn't working question Further information is requested

Comments

@boziyoung
Copy link

can you tell me what i should do ? please

Reproducible code:

import 'package:flutter/material.dart';
// ignore: depend_on_referenced_packages
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:flutter_alarm_clock/flutter_alarm_clock.dart';

void main() async {
  // initialize AlarmClockManager service

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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  //  定时器任务
  // Future<void> main() async {
  //   final int periodicId = 0;
  //   final int oneshotId = 0;
  //   final int oneshotAtId = 0;

  //   print("main alarm run");

  // }

  bool isOn = false;
  bool edit = true;
  int alarmId = 1;
  TextEditingController hc = TextEditingController();
  TextEditingController mc = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            TextField(
              enabled: edit,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                labelText: "Hour",
              ),
              controller: hc,
            ),
            TextField(
              enabled: edit,
              keyboardType: TextInputType.number,
              decoration: InputDecoration(
                labelText: "minutes",
              ),
              controller: mc,
            ),
            Transform.scale(
              scale: 2,
              child: Switch(
                  value: isOn,
                  onChanged: ((value) {
                    setState(() {
                      isOn = value;
                      edit = !edit;
                    });
                    if (isOn == true) {
                      var h = hc.text;
                      var m = mc.text;
                      print("$h $m");
                      AndroidAlarmManager.oneShotAt(
                        DateTime(2022, 09, 26, int.parse(h), int.parse(m), 00),
                        alarmId,
                        setAlarmClock,
                        // () => FlutterAlarmClock.createAlarm(23, 59),
// your code
                      );
                    } else {
                      AndroidAlarmManager.cancel(alarmId);
                      print('Alarm Timer Canceled');
                    }
                  })),
            ),
            TextButton(
                onPressed: () {
                  setAlarmClock();
                  // FlutterAlarmClock.createAlarm(23, 59);
                },
                child: const Text("clock"))
          ],
        ),
      ),
      // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

void setAlarmClock() {
  print('Alarm Fired at ${DateTime.now()}');
  FlutterAlarmClock.createAlarm(23, 59);
}

issue:

E/MethodChannel#flutter_alarm_clock(26487): Failed to handle method call
E/MethodChannel#flutter_alarm_clock(26487): kotlin.UninitializedPropertyAccessException: lateinit property activity has not been initialized
E/MethodChannel#flutter_alarm_clock(26487): at tech.sebastjanmevlja.flutter_alarm_clock.FlutterAlarmClockPlugin.createAlarm(FlutterAlarmClockPlugin.kt:111)
E/MethodChannel#flutter_alarm_clock(26487): at tech.sebastjanmevlja.flutter_alarm_clock.FlutterAlarmClockPlugin.onMethodCall(FlutterAlarmClockPlugin.kt:57)
E/MethodChannel#flutter_alarm_clock(26487): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/MethodChannel#flutter_alarm_clock(26487): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/MethodChannel#flutter_alarm_clock(26487): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/MethodChannel#flutter_alarm_clock(26487): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/MethodChannel#flutter_alarm_clock(26487): at android.os.Handler.handleCallback(Handler.java:938)
E/MethodChannel#flutter_alarm_clock(26487): at android.os.Handler.dispatchMessage(Handler.java:99)
E/MethodChannel#flutter_alarm_clock(26487): at android.os.Looper.loopOnce(Looper.java:233)
E/MethodChannel#flutter_alarm_clock(26487): at android.os.Looper.loop(Looper.java:344)
E/MethodChannel#flutter_alarm_clock(26487): at android.app.ActivityThread.main(ActivityThread.java:8212)
E/MethodChannel#flutter_alarm_clock(26487): at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#flutter_alarm_clock(26487): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
E/MethodChannel#flutter_alarm_clock(26487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
E/flutter (26487): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(error, lateinit property activity has not been initialized, null, kotlin.UninitializedPropertyAccessException: lateinit property activity has not been initialized
E/flutter (26487): at tech.sebastjanmevlja.flutter_alarm_clock.FlutterAlarmClockPlugin.createAlarm(FlutterAlarmClockPlugin.kt:111)
E/flutter (26487): at tech.sebastjanmevlja.flutter_alarm_clock.FlutterAlarmClockPlugin.onMethodCall(FlutterAlarmClockPlugin.kt:57)
E/flutter (26487): at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:262)
E/flutter (26487): at io.flutter.embedding.engine.dart.DartMessenger.invokeHandler(DartMessenger.java:295)
E/flutter (26487): at io.flutter.embedding.engine.dart.DartMessenger.lambda$dispatchMessageToQueue$0$io-flutter-embedding-engine-dart-DartMessenger(DartMessenger.java:319)
E/flutter (26487): at io.flutter.embedding.engine.dart.DartMessenger$$ExternalSyntheticLambda0.run(Unknown Source:12)
E/flutter (26487): at android.os.Handler.handleCallback(Handler.java:938)
E/flutter (26487): at android.os.Handler.dispatchMessage(Handler.java:99)
E/flutter (26487): at android.os.Looper.loopOnce(Looper.java:233)
E/flutter (26487): at android.os.Looper.loop(Looper.java:344)
E/flutter (26487): at android.app.ActivityThread.main(ActivityThread.java:8212)
E/flutter (26487): at java.lang.reflect.Method.invoke(Native Method)
E/flutter (26487): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:584)
E/flutter (26487): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1034)
E/flutter (26487): )
E/flutter (26487): #0 StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:653:7)
E/flutter (26487): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:296:18)
E/flutter (26487):
E/flutter (26487):

@mevljas mevljas added bug Something isn't working question Further information is requested labels Oct 1, 2022
@mevljas
Copy link
Owner

mevljas commented Oct 1, 2022

Are you trying to run this in the background by any chance?

@mevljas mevljas self-assigned this Oct 1, 2022
@boziyoung
Copy link
Author

Are you trying to run this in the background by any chance?

yes, i want to achieve the function that if it meets any certain conditions, it can automatically create an alarm clock in the background

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

No branches or pull requests

2 participants