import 'dart:async'; import 'package:bot_toast/bot_toast.dart'; import 'package:call_log/call_log.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:flutter_sms/flutter_sms.dart'; import 'package:get/get_navigation/src/root/get_material_app.dart'; import 'package:power_logger/power_logger.dart'; import 'package:project_telephony/providers/user_provider.dart'; import 'package:project_telephony/ui/tab_navigator.dart'; import 'package:provider/provider.dart'; import 'package:telephony/telephony.dart'; import 'package:project_telephony/utils/hive_store.dart'; void _sendSMS(String message, List recipents) async { String _result = await sendSMS(message: message, recipients: recipents) .catchError((onError) { print(onError); }); print(_result); } void main() async { WidgetsFlutterBinding.ensureInitialized(); FlutterError.onError = (details) { LoggerData.addData(details); FlutterError.presentError(details); }; await initializeService(); WidgetsFlutterBinding.ensureInitialized(); runApp(const MyApp()); } Future initializeService() async { final service = FlutterBackgroundService(); service.setNotificationInfo(title: '短信助手', content: '正在后台运行'); await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, autoStart: true, isForegroundMode: true, ), iosConfiguration: IosConfiguration( autoStart: true, onForeground: onStart, onBackground: onIosBackground, ), ); } void onIosBackground() { WidgetsFlutterBinding.ensureInitialized(); // print('FLUTTER BACKGROUND FETCH'); } void onStart() { int flag = 0; String? phoneNum, callState; WidgetsFlutterBinding.ensureInitialized(); 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; // print(phoneNum); // String message = "This is a test message!"; // List recipents = ["10000", "10086"]; // String _result = await sendSMS(message: message, recipients: recipents, sendDirect: true) // .catchError((onError) { // print(onError); // }); // print(_result); phoneNum = result.first.number; print(phoneNum); final SmsSendStatusListener listener = (SendStatus status) { print(status); }; Phone.telephony.sendSms( to: phoneNum!, message: "hello", statusListener: listener, isMultipart: true, ); } } else if (callState == "RINGING") { flag++; } else if (callState == "OFFHOOK") { flag++; } }); } class Phone { static Telephony telephony = Telephony.instance; } class MyApp extends StatefulWidget { const MyApp({Key? key}) : super(key: key); @override _MyAppState createState() => _MyAppState(); } class _MyAppState extends State { String _message = ""; // This will not work as the instance will be replaced by // the one in background. final telephony = Telephony.instance; @override void initState() { super.initState(); final service = FlutterBackgroundService(); Future.delayed(const Duration(milliseconds: 0), () async { //Hive.initFlutter; await HiveStore.init(); }); final inbox = Telephony.instance.getInboxSms(); // JPush jPush=JPush(); // jPush.setup( // appKey: "", // channel: "theChannel", // production: false, // debug: true // ); // service.start(); // List permissions = [ // Permission.sms, // Permission.phone, // ]; // PermissionHelper.check(permissions, onSuccess: () { // print('onSuccess'); // }, onFailed: () { // print('onFailed'); // }, onOpenSetting: () { // print('onOpenSetting'); // openAppSettings(); // }); } // onMessage(SmsMessage message) async { // setState(() { // _message = message.body ?? "Error reading message body."; // }); // } // onSendStatus(SendStatus status) { // setState(() { // _message = status == SendStatus.SENT ? "sent" : "delivered"; // }); // } // Future initPlatformState() async { // final bool? result = await Phone.telephony.requestPhoneAndSmsPermissions; // if (result != null && result) { // Phone.telephony.listenIncomingSms( // onNewMessage: onMessage, onBackgroundMessage: onBackgroundMessage); // } // if (!mounted) return; // } @override Widget build(BuildContext context) { return MultiProvider( providers: [ ChangeNotifierProvider(create: (context) => UserProvider()), ], child: MediaQuery( data: MediaQueryData.fromWindow(WidgetsBinding.instance.window), child: ScreenUtilInit( designSize: const Size(750, 1334), builder: (context, child) { return AnnotatedRegion( value: const SystemUiOverlayStyle( statusBarColor: Colors.transparent, //状态栏背景色 statusBarIconBrightness: Brightness.dark), child: GetMaterialApp( // get.testmode=true, debugShowCheckedModeBanner: false, home: const TabNavigator(), builder: (context, child) { // ScreenUtil.setContext(context); return MediaQuery( //设置文字大小不随系统设置改变 data: MediaQueryData.fromWindow( WidgetsBinding.instance!.window) .copyWith(textScaleFactor: 1.0), child: BotToastInit().call(context, child), ); }, navigatorObservers: [BotToastNavigatorObserver()], )); }, )), ); } }