From 77c57f06db84ec27f9dfe1a698d1860d1cd97fd5 Mon Sep 17 00:00:00 2001 From: WinUser01 Date: Sat, 23 Apr 2022 20:28:44 +0800 Subject: [PATCH] =?UTF-8?q?hyzp=5Fybqx-Commit125=EF=BC=9A=E5=B7=B2?= =?UTF-8?q?=E4=BD=BF=E7=94=A8flutter=5Fbackground=5Fservice=200.0.1+21?= =?UTF-8?q?=E6=8F=92=E4=BB=B6=EF=BC=8C=E5=88=9D=E6=AD=A5=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E5=90=8E=E5=8F=B0=E6=8F=90=E9=86=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/components/UserInfo.dart | 6 +- lib/components/commonFun.dart | 73 ++++++------ lib/pages/tabs/Tabs.dart | 41 ++----- lib/pages/tabs/page7_setRemind.dart | 178 ++++++++++++++-------------- 4 files changed, 138 insertions(+), 160 deletions(-) diff --git a/lib/components/UserInfo.dart b/lib/components/UserInfo.dart index d39a785..3f546a2 100644 --- a/lib/components/UserInfo.dart +++ b/lib/components/UserInfo.dart @@ -1,6 +1,6 @@ import 'package:hyzp_ybqx/components/commonFun.dart'; import 'package:hyzp_ybqx/pages/Works/DWDT/dwInfo_data.dart'; - +import 'package:hyzp_ybqx/services/Storage.dart'; import 'EncryptUtil.dart'; import 'commonFun.dart'; @@ -25,8 +25,10 @@ class UserInfo { setQxInfo(mapUserInfo["qx_code"], mapUserInfo["qx_name"]); } - setQxInfo(String _qx_code, String _qx_name) { + setQxInfo(String _qx_code, String _qx_name) async { qx_code = int.parse(_qx_code.trim()); + await Storage.setInt('qx_code', qx_code); // 为启动前台服务,保存 qx_code 到用户配置中 + qx_name = _qx_name; //区县用户登录接口增加返回字段“sfsy”,0表示正式账号,1表示试用账号 qx_trial_tag = mapUserInfo['sfsy']; diff --git a/lib/components/commonFun.dart b/lib/components/commonFun.dart index 58ab3f2..47798a4 100644 --- a/lib/components/commonFun.dart +++ b/lib/components/commonFun.dart @@ -119,43 +119,44 @@ const int g_sip = 1; // 新增sip参数,0表示市级平台,1表示区县平 List listReviewed = []; bool g_bVoiceRemind = false; // 用户设置是否开启语音提醒 int g_remindGap = 60; // 提醒间隔默认为60S,0表示取消定时提醒 -Timer g_remindTimer; //定时提醒变量 -// 设置定时提醒 -setRemindTimer({bool enable = true}) { - // 先取消旧的定时任务 - if (null != g_remindTimer) { - g_remindTimer.cancel(); //取消计时器 - } - - // 设置新的定时任务 - if (enable && g_remindGap > 0) { - ///循环执行定时任务,间隔 g_remindGap 秒 - g_remindTimer = Timer.periodic(Duration(seconds: g_remindGap), (timer) { - ///定时任务 - doRemind(); - }); - } -} - -// 执行语音提醒 -Future doRemind() async { - // 获取待审核黑烟车记录 - if (g_bVoiceRemind) { - listReviewed.clear(); - print("listReviewed = $listReviewed"); - await getReviewedList(); //注意:访问区县后台接口,需要统一添加区县代码参数 - print("listReviewed = $listReviewed"); - // if (g_bVoiceRemind) { - // import 'dart:io'; - // import 'package:audioplayers/audio_cache.dart'; - // print("播放:语音提醒.mp3"); - if (listReviewed.length > 0) { - // AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒 - myPlayVoiceRemind(); - } - } -} +// Timer g_remindTimer; //定时提醒变量 +// +// // 设置定时提醒 +// setRemindTimer({bool enable = true}) { +// // 先取消旧的定时任务 +// if (null != g_remindTimer) { +// g_remindTimer.cancel(); //取消计时器 +// } +// +// // 设置新的定时任务 +// if (enable && g_remindGap > 0) { +// ///循环执行定时任务,间隔 g_remindGap 秒 +// g_remindTimer = Timer.periodic(Duration(seconds: g_remindGap), (timer) { +// ///定时任务 +// doRemind(); +// }); +// } +// } +// +// // 执行语音提醒 +// Future doRemind() async { +// // 获取待审核黑烟车记录 +// if (g_bVoiceRemind) { +// listReviewed.clear(); +// print("listReviewed = $listReviewed"); +// await getReviewedList(); //注意:访问区县后台接口,需要统一添加区县代码参数 +// print("listReviewed = $listReviewed"); +// // if (g_bVoiceRemind) { +// // import 'dart:io'; +// // import 'package:audioplayers/audio_cache.dart'; +// // print("播放:语音提醒.mp3"); +// if (listReviewed.length > 0) { +// // AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒 +// myPlayVoiceRemind(); +// } +// } +// } ///用于定时提醒的变量和函数 diff --git a/lib/pages/tabs/Tabs.dart b/lib/pages/tabs/Tabs.dart index 4df8e72..ba15e3e 100644 --- a/lib/pages/tabs/Tabs.dart +++ b/lib/pages/tabs/Tabs.dart @@ -17,6 +17,8 @@ import '../../services/ServiceLocator.dart'; import '../../services/Storage.dart'; import 'page1_work.dart'; // import 'package:audioplayers/audio_cache.dart'; +import 'package:flutter_background_service/flutter_background_service.dart'; +import 'package:hyzp_ybqx/pages/tabs/flutter_background_service_my.dart'; class Tabs extends StatefulWidget { Tabs({Key key, this.arguments = 0}) : super(key: key); @@ -65,6 +67,7 @@ class _TabsState extends State { @override void initState() { super.initState(); + getlistItems().then((value) => try_setState()); print('widget.arguments = ${widget.arguments}'); //_currentIndex = 3 == widget.arguments ? 0 : widget.arguments; //解决"我的"页面根据用户所属组及时刷新问题 @@ -85,47 +88,17 @@ class _TabsState extends State { } init() async { - // // 从磁盘读取变量 - // g_bVoiceRemind = await Storage.getBool('bVoiceRemind'); - // g_bVoiceRemind = (null == g_bVoiceRemind) ? false : g_bVoiceRemind; - // // print('g_bVoiceRemind = $g_bVoiceRemind'); - // eventBus.fire(VoiceRemindUpdate('g_bVoiceRemind 数据已更新')); - // 执行语音提醒 - doRemind(); - + // doRemind(); //设置定时提醒,循环执行定时任务,间隔 g_remindGap 秒 - setRemindTimer(); + // setRemindTimer(); - // g_remindTimer = Timer.periodic(Duration(seconds: g_remindGap), (timer) { - // ///定时任务 - // doRemind(); - // }); + WidgetsFlutterBinding.ensureInitialized(); + FlutterBackgroundService.initialize(onStart); } - // // 执行语音提醒 - // Future doRemind() async { - // // 获取待审核黑烟车记录 - // listReviewed.clear(); - // print("listReviewed = $listReviewed"); - // await getReviewedList(); //注意:访问区县后台接口,需要统一添加区县代码参数 - // print("listReviewed = $listReviewed"); - // if (g_bVoiceRemind && listReviewed.length > 0) { - // // if (g_bVoiceRemind) { - // // import 'dart:io'; - // // import 'package:audioplayers/audio_cache.dart'; - // // print("播放:语音提醒.mp3"); - // AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒 - // } - // } - @override void dispose() { - // 取消定时任务 - if (null != g_remindTimer) { - g_remindTimer.cancel(); //取消计时器 - } - super.dispose(); } diff --git a/lib/pages/tabs/page7_setRemind.dart b/lib/pages/tabs/page7_setRemind.dart index 830fcf6..5c20d9c 100644 --- a/lib/pages/tabs/page7_setRemind.dart +++ b/lib/pages/tabs/page7_setRemind.dart @@ -11,6 +11,8 @@ import 'package:hyzp_ybqx/services/Storage.dart'; import '../../components/commonFun.dart'; import '../../widget/JdButton.dart'; +import 'package:flutter_background_service/flutter_background_service.dart'; +import 'package:hyzp_ybqx/pages/tabs/flutter_background_service_my.dart'; class page7_setRemind extends StatefulWidget { page7_setRemind({Key key}) : super(key: key); @@ -19,6 +21,9 @@ class page7_setRemind extends StatefulWidget { } class _LoginPageState extends State { + bool bVoiceRemind; + int remindGap; + dispose() { super.dispose(); } @@ -27,16 +32,7 @@ class _LoginPageState extends State { void initState() { super.initState(); bVoiceRemind = g_bVoiceRemind; - - getListView(); - - // //监听 g_bVoiceRemind 更新事件 - // eventBus.on().listen((event) async { - // print(event.str + ", g_bVoiceRemind = $g_bVoiceRemind"); - // setState(() { - // getListView(); - // }); - // }); + remindGap = g_remindGap; } //////// @@ -106,85 +102,15 @@ class _LoginPageState extends State { setState(() { bVoiceRemind = !bVoiceRemind; print("bVoiceRemind = $bVoiceRemind"); - // Storage.setBool('bVoiceRemind', bVoiceRemind); + print("播放语音提醒"); if (bVoiceRemind) { - print("播放语音提醒"); - // AudioCache().play(File('audio/语音提醒.mp3').path); //语音提醒 myPlayVoiceRemind(); + } else { + myPlayClick(); } - getListView(); }); } - bool bVoiceRemind; - - getListView() { - _listViewUser.clear(); - _listViewUser = [ - Center( - child: Container( - margin: EdgeInsets.only(top: 30), - height: ScreenUtil().setWidth(160), - width: ScreenUtil().setWidth(160), - //child: Image.asset('assets/images/user.png', fit: BoxFit.cover), - child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight), - ), - ), - SizedBox(height: 50), - _getListTile('待审提醒', - leadPath: 'assets/images/语音提醒.png', - leadColor: _deepGreyColor, - textColor: bVoiceRemind ? Colors.blue : null, - onTapFun: OnTap_Remind, - trailWidget: bVoiceRemind - ? Icon(Icons.check_box, color: Colors.blue) - : Icon(Icons.check_box_outline_blank, color: Colors.blueAccent)), - _getListTile('提醒间隔(秒)', - leadPath: 'assets/images/时间间隔.png', - leadColor: _deepBlueColor, - trailWidget: getTextField()), - SizedBox(height: 60), - Row( - mainAxisAlignment: MainAxisAlignment.spaceAround, - children: [ - JdButton( - height: 128, - width: 282, - //height: 126, - text: "确认", - color: Colors.blueAccent, - onTop: () { - g_bVoiceRemind = bVoiceRemind; - print("g_bVoiceRemind = $g_bVoiceRemind"); - Storage.setBool('bVoiceRemind', g_bVoiceRemind); - - g_remindGap = _gap; - print("g_remindGap = $g_remindGap"); - Storage.setInt('nRemindGap', g_remindGap); - setRemindTimer(enable: g_bVoiceRemind); - - // AudioCache().play(File('audio/yinxiao1064.mp3').path); //音效提醒 - myPlayClacks(); //音效提醒 - Navigator.pop(context); - }, - ), - JdButton( - height: 128, - width: 282, - //height: 126, - text: "取消", - color: Colors.blueAccent, - onTop: () { - Navigator.pop(context); - }, - ) - ], - ), - ]; - } - - int _gap = g_remindGap; - Widget getTextField() { return Container( width: ScreenUtil().setWidth(150), @@ -193,10 +119,10 @@ class _LoginPageState extends State { focusNode: FocusNode(), textAlignVertical: TextAlignVertical(y: 1.0), controller: TextEditingController.fromValue(TextEditingValue( - text: _gap.toString(), + text: remindGap.toString(), // 保持光标在最后 - selection: TextSelection.fromPosition( - TextPosition(affinity: TextAffinity.downstream, offset: _gap.toString().length)))), + selection: TextSelection.fromPosition(TextPosition( + affinity: TextAffinity.downstream, offset: remindGap.toString().length)))), maxLines: 1, keyboardType: TextInputType.number, decoration: InputDecoration( @@ -209,7 +135,7 @@ class _LoginPageState extends State { enabledBorder: UnderlineInputBorder(borderSide: BorderSide(color: Colors.lightBlue)), ), onChanged: (v) { - _gap = int.parse(v); + remindGap = int.parse(v); }, ), ); @@ -279,7 +205,83 @@ class _LoginPageState extends State { body: Container( padding: EdgeInsets.only(top: 30, bottom: 20, left: 20, right: 20), child: ListView( - children: _listViewUser, + children: [ + Center( + child: Container( + margin: EdgeInsets.only(top: 30), + height: ScreenUtil().setWidth(160), + width: ScreenUtil().setWidth(160), + //child: Image.asset('assets/images/user.png', fit: BoxFit.cover), + child: Image.asset('assets/images/ybsthbj.png', fit: BoxFit.fitHeight), + ), + ), + SizedBox(height: 50), + _getListTile('待审提醒', + leadPath: 'assets/images/语音提醒.png', + leadColor: _deepGreyColor, + textColor: bVoiceRemind ? Colors.blue : null, + onTapFun: OnTap_Remind, + trailWidget: bVoiceRemind + ? Icon(Icons.check_box, color: Colors.blue) + : Icon(Icons.check_box_outline_blank, color: Colors.blueAccent)), + _getListTile('提醒间隔(秒)', + leadPath: 'assets/images/时间间隔.png', + leadColor: _deepBlueColor, + trailWidget: getTextField()), + SizedBox(height: 60), + Row( + mainAxisAlignment: MainAxisAlignment.spaceAround, + children: [ + JdButton( + height: 128, + width: 282, + //height: 126, + text: "确认", + color: Colors.blueAccent, + onTop: () async { + if (g_bVoiceRemind != bVoiceRemind && g_remindGap != remindGap) { + if (await FlutterBackgroundService().isServiceRunning()) { + FlutterBackgroundService().sendData({"action": "stopService"}); + } + + g_bVoiceRemind = bVoiceRemind; + print("g_bVoiceRemind = $g_bVoiceRemind"); + await Storage.setBool('bVoiceRemind', g_bVoiceRemind); + + g_remindGap = remindGap; + print("g_remindGap = $g_remindGap"); + await Storage.setInt('nRemindGap', g_remindGap); + + if (bVoiceRemind) { + while (true) { + var isRunning = await FlutterBackgroundService().isServiceRunning(); + if (!isRunning) { + await FlutterBackgroundService.initialize(onStart); + myPlayVoiceRemind(); + break; + } + } + } else { + myPlayClick(); + } + } + + Navigator.pop(context); + }, + ), + JdButton( + height: 128, + width: 282, + //height: 126, + text: "取消", + color: Colors.blueAccent, + onTop: () { + Navigator.pop(context); + }, + ) + ], + ), + ], ), ), );