我试图在Flutter中创建后台计时器,每n秒调用一次。调用AndroidAlarmManager.periodic应该每2秒运行一次print("Hello")
函数,但它看起来像是以更大的间隔随机调用的。我做错了什么?
import 'package:android_alarm_manager/android_alarm_manager.dart';
void runTimer() async{
await AndroidAlarmManager.periodic(const Duration(seconds: 2), 0, printHello, exact: true);
}
void printHello(){
print("Hello");
}
main() async {
await AndroidAlarmManager.initialize();
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
home: Scaffold(
body: Center(
child: InkWell(
onTap: runTimer,
child: Container(child: Text('Run Timer'))
),
),
),
);
}
}
2条答案
按热度按时间v6ylcynt1#
您不能使用
AlarmManager
频繁地安排闹钟:注:从API 19(Build.VERSION_CODES.KITKAT)开始,报警输送不准确:操作系统将转移警报以减少唤醒和电池使用。有新的API来支持需要严格交付保证的应用程序;请参阅setWindow(int,long,long,android.app.PendingIntent)和setExact(int,long,android.app.PendingIntent)。targetSdkVersion早于API 19的应用程序将继续看到之前的行为,即所有报警都在请求时准确发送。
参见:https://developer.android.com/reference/android/app/AlarmManager
nzk0hqpo2#
@kashlo,将以下代码添加到main()中
问题1:funTimerMain中的编号1、2、3何时运行?当应用程序处于前台时(即用户使用应用程序),或当应用程序处于后台时(即用户按下主页按钮或切换到另一个应用程序)。
问题2:1、2号机组何时停止运行?答案2:当应用程序被用户或Android系统终止时(例如,被节电程序终止)
问题3:3号何时停止运行?回答3:与Ans 2相同,另外,当用户关闭屏幕时。从Android?开始(我忘记了确切的版本),当用户关闭屏幕时不允许网络访问。
如您所见,funTimerMain每1秒(或x秒)运行一次,但是funTimerMain中的每个任务可以每y秒运行一次(其中y〉= x,或者最好是y = Nx,N为整数)。
当我第一次使用这个技术的时候,我觉得效率会很糟糕,但是当我在真实的移动的上运行这个应用程序的时候,我注意不到手机上有任何延迟。所以我认为通过设置一个适当的x和y值,你可以有一个响应速度快的应用程序。
您也可以在funTimerMain中使用'await',但请记住,如果'function after await'没有返回,则计时器将暂停!