问题描述
我已按照以下说明在Flutter v1.0.0应用程序中安装了插件链接上的说明。但是,当我尝试使用 AndroidAlarmManager.oneShot(...)
时,没有任何反应。控制台中甚至没有错误。
I have installed the Android Alarm Manager plugin in my Flutter v1.0.0 app by following the instructions at the link. But when I try to use AndroidAlarmManager.oneShot(...)
nothing happens. There's not even an error in the console.
我正在使用 FutureBuilder
小部件等待 AndroidAlarmManager.initialize()
未来以及我的应用开始渲染前的另一个未来:
I am using a FutureBuilder
widget to wait on the AndroidAlarmManager.initialize()
future and one other future before my app begins rendering:
final combinedFutures = Future.wait<void>([
gameService.asyncLoad(),
AndroidAlarmManager.initialize(),
]);
FutureBuilder<void>(
future: combinedFutures,
builder: (context, snapshot) {
...
}
)
FutureBuilder
最终呈现了它的内容应该这样,我知道 AndroidAlarmManager.initialize()
将来会正确返回。
The FutureBuilder
does end up rendering what it should so I know that the AndroidAlarmManager.initialize()
future returns correctly.
然后在按钮的 onPressed
函数中,我这样做:
Then in a button's onPressed
function I do this:
AndroidAlarmManager.oneShot(
Duration(seconds: 5),
0,
() {
print("test");
},
wakeup: true,
);
上面应该调用 print(test)
5秒钟后,唤醒我的手机,但控制台上没有打印任何内容,并且手机上没有任何反应。
The above should be calling print(test)
after 5 seconds and waking up my phone but nothing is printed to the console and nothing happens on my phone. What is going wrong?
推荐答案
调试库代码后,我发现 AndroidAlarmManager.oneShot
函数调用方法 PluginUtilities.getCallbackHandle(callback)
,其中 callback
是传入的函数。我的案例回调
函数是:
After debugging the library code I found that the AndroidAlarmManager.oneShot
function calls the method PluginUtilities.getCallbackHandle(callback)
where callback
is the function passed in. In my case the callback
function was:
() {
print("test");
}
由于某些原因 PluginUtilities.getCallbackHandle
应该返回 null
时应返回 CallbackHandle
类型的值。
For some reason PluginUtilities.getCallbackHandle
was returning null
when it should be returning a value of type CallbackHandle
.
查看 PluginUtilities.getCallbackHandle
它说:
我传递的函数不是top-级别,静态或命名。因此,我在课堂上做了以下操作,现在一切正常:
The function I was passing is not top-level, static, or named. So I did the following inside my class and everything works fine now:
static void alarmTest() {
print("test");
}
void runAlarm() {
AndroidAlarmManager.oneShot(
Duration(seconds: 10),
0,
alarmTest,
wakeup: true,
).then((val) => print(val));
}
这篇关于Flutter Android Alarm Manager无法运作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!