import 'dart:async'; import 'dart:io'; import 'dart:ui'; import 'package:call_log/call_log.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/services.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_sms/flutter_sms.dart'; import 'package:shared_preferences/shared_preferences.dart'; import 'package:telephony/telephony.dart'; import './call_sms.dart'; Future _sendSMS(String message, List recipients) async { try{ String result = await sendSMS(message: message, recipients: recipients, sendDirect: true); print(result); } catch (error){ print(error.toString()); } } 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: false, 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) { WidgetsFlutterBinding.ensureInitialized(); print('FLUTTER BACKGROUND FETCH'); return true; } void onStart(ServiceInstance service ) async { DartPluginRegistrant.ensureInitialized(); int flag = 0; String phoneNum=""; String callState; Timer.periodic(const Duration(seconds: 1), (timer) async { CallState state = await Telephony.instance.callState; callState = state.name; // print(callState!+" $flag"); if (callState == "IDLE") { if (flag != 0) { flag = 0; // print("object"); final Iterable result = await CallLog.query(); phoneNum = result.first.number!; if(phoneNum.isEmpty){ print("At Least 1 Person or Message Required"); }else{ print("你好$phoneNum"); // print("你好123123$phoneNum"); } } } else if (callState == "RINGING") { flag++; } else if (callState == "OFFHOOK") { flag++; } }); }