Androidアラームマネージャープラグインを新しいFlutterアプリにインストールしました。プラグインのサンプルコードを使用していますが、コンソールでエラーが発生します。
Androidアラームマネージャープラグインが機能するようにする方法を提案してください。DartのAndroid_alarm_managerをアプリに統合して、スケジュールで選択した時刻に達したときにユーザーがアラームを受け取るようにするにはどうすればよいですか?
私はこのリンクからのコードを使用します: 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):
これは私の更新されたコードです:
/////////////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):
このアプリをAndroid=でのみ開発している場合は、指定された時間間隔ごとに実行される_native foreground/background service
_を作成できます。次に、_platform channel
_を使用して、特定の条件で毎回実行する場合はbutton
をダートします。または、_shared prefrence
_を使用してホームページのinitState()
からネイティブサービスを呼び出します。サービスが開始されると、指定された時間間隔ごとにそれ自体が呼び出されます(分または24時間など)。
AndroidManifest.xmlでApplication
を参照していますか?ところで、AndroidのApplication
と混同しないように、別の名前を使用します(たとえば、MyFlutterApplication
)。
プラグインのドキュメント に注意してください:
これは、アプリケーションのAndroidManifest.xmlに反映する必要があります。例えば。:
<application
Android:name=".Application"
...
詳細については、 Android Developers要素のドキュメント を参照してください。