web-dev-qa-db-fra.com

Android Alarm Manager ne fonctionne pas pour Flutter Project App

Error J'ai installé le Android plugin Alarm Manager dans mon application New Flutter. J'utilise l'exemple de code du plugin - mais cela donne une erreur dans la console.

Veuillez suggérer comment faire fonctionner Android plugins de gestionnaire d'alarmes. Comment puis-je intégrer Android_alarm_manager de Dart à l'application afin que les utilisateurs reçoivent une alarme lorsque l'heure qu'ils ont choisie dans le calendrier arrive?

J'utilise le code de ce lien: https://github.com/flutter/plugins/tree/master/packages/Android_alarm_manager

 //////  main.Dart://///////

    import 'Dart:isolate';
    import 'package:Android_alarm_manager/Android_alarm_manager.Dart';
    import 'package:flutter/material.Dart';

    void printHello() {
      final DateTime now = DateTime.now();
      final int isolateId = Isolate.current.hashCode;
      print("[$now] Hello, world! isolate=${isolateId} function='$printHello'");
    }
    void main() async {
      final int helloAlarmID = 0;
      await AndroidAlarmManager.initialize();
      runApp(MaterialApp(home: Application()));
      await AndroidAlarmManager.periodic(const Duration(minutes: 1), helloAlarmID, printHello);
    }
    class Application extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(),
        );
      }
    }


///////////////Application.Java/////////////////////

    package io.flutter.plugins.androidalarmmanagerexample;
    import io.flutter.app.FlutterApplication;
    import io.flutter.plugin.common.PluginRegistry;
    import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
    import io.flutter.plugins.GeneratedPluginRegistrant;

    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);
        }
    }



///// When I run this code it gives error in console as given below://////

E/flutter ( 6831): [ERROR:flutter/lib/ui/ui_Dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke interface method 'void io.flutter.plugin.common.PluginRegistry$PluginRegistrantCallback.registerWith(io.flutter.plugin.common.PluginRegistry)' on a null object reference, null)
E/flutter ( 6831): #0      JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.Dart:149:7)
E/flutter ( 6831): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.Dart:302:33)
E/flutter ( 6831): <asynchronous suspension>
E/flutter ( 6831): #2      AndroidAlarmManager.initialize (package:Android_alarm_manager/Android_alarm_manager.Dart:76:10)
E/flutter ( 6831): <asynchronous suspension>
E/flutter ( 6831): #3      main (package:alarmdemo/main.Dart:12:29)
E/flutter ( 6831): <asynchronous suspension>
E/flutter ( 6831): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (Dart:ui/hooks.Dart:189:25)
E/flutter ( 6831): #5      _rootRun (Dart:async/zone.Dart:1124:13)
E/flutter ( 6831): #6      _CustomZone.run (Dart:async/zone.Dart:1021:19)
E/flutter ( 6831): #7      _runZoned (Dart:async/zone.Dart:1516:10)
E/flutter ( 6831): #8      runZoned (Dart:async/zone.Dart:1500:12)
E/flutter ( 6831): #9      _runMainZoned.<anonymous closure> (Dart:ui/hooks.Dart:180:5)
E/flutter ( 6831): #10     _startIsolate.<anonymous closure> (Dart:isolate/runtime/libisolate_patch.Dart:300:19)
E/flutter ( 6831): #11     _RawReceivePortImpl._handleMessage (Dart:isolate/runtime/libisolate_patch.Dart:171:12)
E/flutter ( 6831): 
E/flutter ( 6831): [ERROR:flutter/lib/ui/ui_Dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method AlarmService.initialized on channel plugins.flutter.io/Android_alarm_manager_background)
E/flutter ( 6831): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.Dart:300:7)
E/flutter ( 6831): <asynchronous suspension>
E/flutter ( 6831): #1      _alarmManagerCallbackDispatcher (package:Android_alarm_manager/Android_alarm_manager.Dart:49:12)
E/flutter ( 6831): #2      _runMainZoned.<anonymous closure>.<anonymous closure> (Dart:ui/hooks.Dart:189:25)
E/flutter ( 6831): #3      _rootRun (Dart:async/zone.Dart:1124:13)
E/flutter ( 6831): #4      _CustomZone.run (Dart:async/zone.Dart:1021:19)
E/flutter ( 6831): #5      _runZoned (Dart:async/zone.Dart:1516:10)
E/flutter ( 6831): #6      runZoned (Dart:async/zone.Dart:1500:12)
E/flutter ( 6831): #7      _runMainZoned.<anonymous closure> (Dart:ui/hooks.Dart:180:5)
E/flutter ( 6831): #8      _startIsolate.<anonymous closure> (Dart:isolate/runtime/libisolate_patch.Dart:300:19)
E/flutter ( 6831): #9      _RawReceivePortImpl._handleMessage (Dart:isolate/runtime/libisolate_patch.Dart:171:12)
E/flutter ( 6831): 

Voici mon code mis à jour:

/////////////main.Dart//////////

    void printHello() {
      final DateTime now = new DateTime.now();
      final int isolateId = Isolate.current.hashCode;
      print("[$now] Hello, world! isolate=${isolateId} 
      function='$printHello'");
    }

    void main() async {
    runApp(MaterialApp(home: Application()));
    }

     class Application extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Container(
            child: Center(
              child: RaisedButton(
                child: Text('Hello'),
                onPressed: () {
                  runAlarm();
                },
              ),
            ),
          ),
        );
      }

  void runAlarm() {
    AndroidAlarmManager.oneShot(
      Duration(seconds: 10),
      0,
      printHello,
      wakeup: true,
    ).then((val) => print(val));
  }
  static void alarmTest() {
    print("test");
  }

}

It does not give any Error in console but print two statements as below:

E/AlarmService(11943): Fatal: failed to find callback
I/AlarmService(11943): AlarmService has not yet started.


I also initialize the alarm manager but it gives error in console:


 void runAlarm() {
    AndroidAlarmManager.periodic(
      Duration(seconds: 10),
      0,
      printHello,
      wakeup: true,
    ).then((val) => print(val)).catchError((e) {
      print(e);
    });
  }


Error:

E/flutter ( 6831): [ERROR:flutter/lib/ui/ui_Dart_state.cc(148)] Unhandled Exception: PlatformException(error, Attempt to invoke interface method 'void io.flutter.plugin.common.PluginRegistry$PluginRegistrantCallback.registerWith(io.flutter.plugin.common.PluginRegistry)' on a null object reference, null)
    E/flutter ( 6831): #0      JSONMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.Dart:149:7)
    E/flutter ( 6831): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.Dart:302:33)
    E/flutter ( 6831): <asynchronous suspension>
    E/flutter ( 6831): #2      AndroidAlarmManager.initialize (package:Android_alarm_manager/Android_alarm_manager.Dart:76:10)
    E/flutter ( 6831): <asynchronous suspension>
    E/flutter ( 6831): #3      main (package:alarmdemo/main.Dart:12:29)
    E/flutter ( 6831): <asynchronous suspension>
    E/flutter ( 6831): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (Dart:ui/hooks.Dart:189:25)
    E/flutter ( 6831): #5      _rootRun (Dart:async/zone.Dart:1124:13)
    E/flutter ( 6831): #6      _CustomZone.run (Dart:async/zone.Dart:1021:19)
    E/flutter ( 6831): #7      _runZoned (Dart:async/zone.Dart:1516:10)
    E/flutter ( 6831): #8      runZoned (Dart:async/zone.Dart:1500:12)
    E/flutter ( 6831): #9      _runMainZoned.<anonymous closure> (Dart:ui/hooks.Dart:180:5)
    E/flutter ( 6831): #10     _startIsolate.<anonymous closure> (Dart:isolate/runtime/libisolate_patch.Dart:300:19)
    E/flutter ( 6831): #11     _RawReceivePortImpl._handleMessage (Dart:isolate/runtime/libisolate_patch.Dart:171:12)
    E/flutter ( 6831): 
    E/flutter ( 6831): [ERROR:flutter/lib/ui/ui_Dart_state.cc(148)] Unhandled Exception: MissingPluginException(No implementation found for method AlarmService.initialized on channel plugins.flutter.io/Android_alarm_manager_background)
    E/flutter ( 6831): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.Dart:300:7)
    E/flutter ( 6831): <asynchronous suspension>
    E/flutter ( 6831): #1      _alarmManagerCallbackDispatcher (package:Android_alarm_manager/Android_alarm_manager.Dart:49:12)
    E/flutter ( 6831): #2      _runMainZoned.<anonymous closure>.<anonymous closure> (Dart:ui/hooks.Dart:189:25)
    E/flutter ( 6831): #3      _rootRun (Dart:async/zone.Dart:1124:13)
    E/flutter ( 6831): #4      _CustomZone.run (Dart:async/zone.Dart:1021:19)
    E/flutter ( 6831): #5      _runZoned (Dart:async/zone.Dart:1516:10)
    E/flutter ( 6831): #6      runZoned (Dart:async/zone.Dart:1500:12)
    E/flutter ( 6831): #7      _runMainZoned.<anonymous closure> (Dart:ui/hooks.Dart:180:5)
    E/flutter ( 6831): #8      _startIsolate.<anonymous closure> (Dart:isolate/runtime/libisolate_patch.Dart:300:19)
    E/flutter ( 6831): #9      _RawReceivePortImpl._handleMessage (Dart:isolate/runtime/libisolate_patch.Dart:171:12)
    E/flutter ( 6831): 

enter image description here

13
Manpreet Kaur

Si vous développez cette application pour Android uniquement, vous pouvez créer native foreground/background service Qui s'exécute pour chaque intervalle de temps donné. Ensuite, en utilisant le platform channel, Vous pouvez appeler le service de Dart sur button appuyez sur si vous voulez qu'il s'exécute à chaque fois à certaines conditions ou en utilisant shared prefrence appelez le service natif de initState() dans votre page d'accueil. Comme vous le savez peut-être une fois que le service natif est démarré, il s'invoquera après chaque intervalle de temps donné (comme minute ou 24 heures).

0
Harsha pulikollu

Faites-vous référence à vous Application dans votre AndroidManifest.xml? BTW (j'utiliserais un nom différent pour ne pas avoir de confusion avec Application d'Android, par exemple MyFlutterApplication).

Remarquez dans la documentation du plugin il indique:

Ce qui doit se refléter dans le fichier AndroidManifest.xml de l'application. Par exemple.:

<application
    Android:name=".Application"
    ...

Pour plus d'informations, voir documentation sur les éléments Android Developers .

0