Android警报管理器不适用于Flutter Project App [英] Android Alarm Manager is not working for Flutter Project App

查看:99
本文介绍了Android警报管理器不适用于Flutter Project App的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我已经在New Flutter应用中安装了Android Alarm Manager插件。我使用了Plugin-的示例代码,但它在控制台中给出了错误。



请提出如何使Android警报管理器插件正常工作的建议。
如何将Dart的android_alarm_manager集成到应用程序中,以便用户在计划中选择的时间到达时收到警报?



我使用此代码链接:

解决方案

经过几个小时的努力,我终于自己解决了这个问题(但感觉更长了!)。突破发生在我真正克隆了



执行完此操作后,您必须 Application.java 的第一行的程序包名称> package io.flutter.plugins.androidalarmmanagerexample; 匹配项目使用的任何软件包(请参见现有 MainActivity.java 的第一行)。如果您不这样做,则gradle找不到它,什么也没有用!



您现在可以按照自述文件,添加权限等:


将此插件照常导入项目后,将以下内容添加到中的 AndroidManifest.xml 中< manifest>< / manifest> 标签:




  < uses-permission android:name = android.permission.RECEIVE_BOOT_COMPLETED /> 
< uses-permission android:name = android.permission.WAKE_LOCK />




接下来,在< application> < / application> 标记,添加:




 <服务
android:name = io.flutter.plugins.androidalarmmanager.AlarmService
android:permission = android.permission.BIND_JOB_SERVICE
android:exported = false />
<接收器
android:name = io.flutter.plugins.androidalarmmanager.AlarmBroadcastReceiver
android:exported = false />
<接收器
android:name = io.flutter.plugins.androidalarmmanager.RebootBroadcastReceiver
android:enabled = false>
<意图过滤器>
< action android:name = android.intent.action.BOOT_COMPLETED>< / action>
< / intent-filter>
< / receiver>

最后一部分是最让我困惑的部分。听起来好像他们很模糊,但是如果您早前完全正确地完成了所有其他操作,那么实际上正是您需要做的。


这必须反映在应用程序的AndroidManifest.xml中。例如:




 < application 
android:name =。Application
...

只需更改您的 android:name .Application ,这意味着它将利用我们先前添加的 Application.java



就是这样!希望您现在可以运行您的应用。


I have installed the Android Alarm Manager plugin in my New Flutter app. I use the example code of Plugin- but it gives error in console.

Please suggest how to make android alarm manager plugins works. How do I Integrate the Dart's android_alarm_manager to the app so that users get alarm when the time they picked in the schedule reaches?

I use the code from this link: 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): 

This is my Updated Code:

/////////////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): 

解决方案

I've finally fixed this issue myself, after struggling for a couple of hours (but felt a lot longer!). The breakthrough came when I actually cloned the Flutter Plugins Github repository that contains android_alarm_manager and poked around the example code and looked at how it was laid out in an IDE, rather than looking at isolated files online.

The Readme is not very clear on what exactly to do, if you're not versed in Android Java development, but it becomes clear when you look at the working example code.

You need to drop in the Application.java file they give you in the example directory into your actual project, in the same folder as your existing MainActivity.java file. The contents should look like this:

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;
import io.flutter.plugins.androidalarmmanager.AlarmService;

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

As for where you need to put this file, their example it looks like this, but yours is probably in something like <your project dir>/android/app/src/main/java/com/example/<your project name>:

After doing this, you must update the package name on the first line of Application.java from package io.flutter.plugins.androidalarmmanagerexample; to match whatever package your project uses (see the first line of your existing MainActivity.java). If you don't do this, gradle doesn't find it and nothing works!

You can now follow the advice in the Readme, adding in the permissions and etc:

After importing this plugin to your project as usual, add the following to your AndroidManifest.xml within the <manifest></manifest> tags:

<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

Next, within the <application></application> tags, add:

<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>

The last part is the part that most confused me. It sounds like they're being very vague, but if you did everything else exactly right earlier, it's actually precisely what you need to do.

Which must be reflected in the application's AndroidManifest.xml. E.g.:

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

Simply change your android:name to .Application, which means it will now utilise that Application.java we added previously.

That's it! Hopefully you can now run your app.

这篇关于Android警报管理器不适用于Flutter Project App的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆