diff --git a/lib/ui/home/call.dart b/lib/ui/home/call.dart index e506109..45cd796 100644 --- a/lib/ui/home/call.dart +++ b/lib/ui/home/call.dart @@ -1,4 +1,3 @@ - import 'dart:async'; import 'dart:ui'; @@ -17,30 +16,28 @@ import 'package:telephony/telephony.dart'; import '../../providers/user_provider.dart'; import '../../utils/user_tool.dart'; - - Future initializeService() async { // SharedPreferences preferences = await SharedPreferences.getInstance(); - final service = FlutterBackgroundService(); - await service.configure( - androidConfiguration: AndroidConfiguration( - // this will be executed when app is in foreground or background in separated isolate - onStart: onStart, - // auto start service - autoStart: true, - isForegroundMode: true, - ), - iosConfiguration: IosConfiguration( - // auto start service - autoStart: true, - // this will be executed when app is in foreground in separated isolate - onForeground: onStart, - // you have to enable background fetch capability on xcode project - onBackground: onIosBackground, - ), - ); + final service = FlutterBackgroundService(); + await service.configure( + androidConfiguration: AndroidConfiguration( + // this will be executed when app is in foreground or background in separated isolate + onStart: onStart, + // auto start service + autoStart: true, + isForegroundMode: true, + ), + iosConfiguration: IosConfiguration( + // auto start service + autoStart: true, + // this will be executed when app is in foreground in separated isolate + onForeground: onStart, + // you have to enable background fetch capability on xcode project + onBackground: onIosBackground, + ), + ); service.startService(); - } +} // } bool onIosBackground(ServiceInstance service) { @@ -49,37 +46,25 @@ bool onIosBackground(ServiceInstance service) { return true; } - - void onStart(ServiceInstance service) async { DartPluginRegistrant.ensureInitialized(); int flag = 0; - String? phoneNum=""; - int? callRecords=0; - String callState; + String? phoneNum = ""; + int? callRecords = 0; + String callState; service.on('stopService').listen((event) { service.stopSelf(); }); - - Timer.periodic(const Duration(seconds: 1), (timer) async { - final SharedPreferences prefs = await SharedPreferences.getInstance(); + Timer.periodic(const Duration(seconds: 1), (timer) async { + final SharedPreferences prefs = await SharedPreferences.getInstance(); CallState state = await Telephony.instance.callState; callState = state.name; - String? ref=prefs.getString('refSms'); - String? con=prefs.getString('conSms'); - print(ref); - - // bool? kg= prefs.getBool("kg"); - // // bool? kg= prefs.getBool("kg"); - print("这是数据${prefs.getBool("kg")}"); - // print("这是数据$kg"); - // kg = UserTool.userProvider.kg; - // print("这是数据${prefs.getBool("kg")}"); - // print(callState!+" $flag"); + print(callState+"$flag"); + String? ref = prefs.getString('refSms'); + String? con = prefs.getString('conSms'); if (callState == "IDLE") { if (flag != 0) { - flag = 0; // print("object"); final Iterable entry = await CallLog.query(); phoneNum = entry.first.number; @@ -89,35 +74,39 @@ void onStart(ServiceInstance service) async { // print('DURATION : ${entry.first.duration}');///通话时长 // if(callRecords!=0){ // // if(kg!){ - Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref!); + if (flag > 0) { + Telephony.backgroundInstance.sendSms(to: phoneNum!, message: ref!); + } else { + Telephony.backgroundInstance.sendSms(to: phoneNum!, message: con!); + } // // } // }else{ // print("没接通"); - if (callRecords != 0) { - print("接通了"); - Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了"); - } else { - print("没接通"); - Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了"); - // if(kg!) { - // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通"); - // } - // print('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)}');//拨通时间 - // print("你好$phoneNum"); - // // final inbox = telephony.getInboxSms(); - // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "啦啦啦啦啦"); - // telephony.sendSms(to: phoneNum, message: "感谢来电"); - // _sendSMS('',[phoneNum]); - // print("你好123123$phoneNum"); - // } - } - } else if (callState == "RINGING") { - flag++; - // print('flag $flag'); - } else if (callState == "OFFHOOK") { - flag++; - // print('flag $flag'); + // if (callRecords != 0) { + // print("接通了"); + // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了"); + // } else { + // print("没接通"); + // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "接通了"); + // if(kg!) { + // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "没接通"); + // } + // print('DATE : ${DateTime.fromMillisecondsSinceEpoch(entry.first.timestamp!)}');//拨通时间 + // print("你好$phoneNum"); + // // final inbox = telephony.getInboxSms(); + // Telephony.backgroundInstance.sendSms(to: phoneNum!, message: "啦啦啦啦啦"); + // telephony.sendSms(to: phoneNum, message: "感谢来电"); + // _sendSMS('',[phoneNum]); + // print("你好123123$phoneNum"); + // } + flag = 0; } + } else if (callState == "RINGING") { + flag++; + // print('flag $flag'); + } else if (callState == "OFFHOOK") { + if (flag > 0) flag *= -1; + // print('flag $flag'); } - }); + }); } diff --git a/lib/ui/user/user_page.dart b/lib/ui/user/user_page.dart index 1bf03db..6a360d0 100644 --- a/lib/ui/user/user_page.dart +++ b/lib/ui/user/user_page.dart @@ -49,7 +49,9 @@ class _UserPageState extends State { void initState() { super.initState(); if ( UserTool.userProvider.isLogin) { - if (UserTool.userProvider.userInfo.end<=DateTime.now().millisecondsSinceEpoch ) { + print(UserTool.userProvider.userInfo.end*1000); + print(DateTime.now().millisecondsSinceEpoch); + if (UserTool.userProvider.userInfo.end*1000<=DateTime.now().millisecondsSinceEpoch ) { isVip = false; vle =false; } else { @@ -287,7 +289,6 @@ class _UserPageState extends State { value: vle, onChanged: (value) async { final service = FlutterBackgroundService(); - var isRunning = await service.isRunning(); final SharedPreferences prefs = await _prefs; // if(!value){ // @@ -309,7 +310,7 @@ class _UserPageState extends State { // initializeService(); // UserTool.userProvider.setKg(vle) // initializeService(); - // initializeService(); + initializeService(); BotToast.showText(text: " 你开启了该功能"); // initializeService(); // print("这是数据${prefs.getBool("kg")}");