diff --git a/android/app/build.gradle b/android/app/build.gradle index 22d2e51..d89382c 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -47,7 +47,7 @@ android { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). applicationId "com.example.project_telephony" minSdkVersion 23 - targetSdkVersion 33 + targetSdkVersion 31 versionCode flutterVersionCode.toInteger() versionName flutterVersionName diff --git a/android/build.gradle b/android/build.gradle index 55968df..fe7a2e3 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -1,5 +1,6 @@ buildscript { ext.kotlin_version = '1.6.10' +// ext.kotlin_version = '1.4.10' repositories { // google() // mavenCentral() diff --git a/lib/main.dart b/lib/main.dart index 055ec51..f9b3577 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -11,15 +11,17 @@ import 'package:flutter_sms/flutter_sms.dart'; import 'package:fluwx/fluwx.dart'; import 'package:get/get_navigation/src/root/get_material_app.dart'; import 'package:permission_handler/permission_handler.dart'; -import 'package:permission_handler_platform_interface/permission_handler_platform_interface.dart'; + import 'package:power_logger/power_logger.dart'; import 'package:project_telephony/providers/user_provider.dart'; import 'package:project_telephony/ui/home/call.dart'; +import 'package:project_telephony/ui/home/call_sms.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'; +import 'package:workmanager/workmanager.dart'; // void _sendSMS(String message, List recipients) async { // try{ @@ -31,95 +33,25 @@ import 'package:project_telephony/utils/hive_store.dart'; // } // } + + void main() async { - // WidgetsFlutterBinding.ensureInitialized(); FlutterError.onError = (details) { LoggerData.addData(details); FlutterError.presentError(details); }; - // await initializeService(); + WidgetsFlutterBinding.ensureInitialized(); + await initializeService(); + // Workmanager().initialize( + // callbackDispatcher, // The top level function, aka callbackDispatcher + // isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks + // ); 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; -// String 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(); -// // 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!; -// // List recipents=[ -// // phoneNum -// // ]; -// if(phoneNum.isEmpty){ -// print("At Least 1 Person or Message Required"); -// }else{ -// print("你好"+phoneNum); -// _sendSMS("你好", [phoneNum]); -// 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); @@ -129,71 +61,27 @@ class MyApp extends StatefulWidget { } class _MyAppState extends State { - // This will not work as the instance will be replaced by - // the one in background. final telephony = Telephony.instance; - // final sendSMS=Telephony.instance; - final sendSMS=Telephony.instance; - // FlutterSmsPlatform + @override Future _Getpermission() async{ await [Permission.sms,Permission.phone,].request(); } + @override void initState() { super.initState(); // final service = FlutterBackgroundService(); - Future.delayed(const Duration(milliseconds: 0), () async { - //Hive.initFlutter; - - _Getpermission(); - await HiveStore.init(); - registerWxApi( - appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ " - ); - }); - // 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(); + // Future.delayed(const Duration(milliseconds: 0), () async { + // //Hive.initFlutter; // }); + _Getpermission(); + HiveStore.init(); + registerWxApi( + appId: "wxd930ea5d5a228f5f",universalLink:"https://your.univerallink.com/link/ " + ); } - // 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) { @@ -219,7 +107,7 @@ class _MyAppState extends State { return MediaQuery( //设置文字大小不随系统设置改变 data: MediaQueryData.fromWindow( - WidgetsBinding.instance!.window) + WidgetsBinding.instance.window) .copyWith(textScaleFactor: 1.0), child: BotToastInit().call(context, child), ); diff --git a/lib/ui/home/call.dart b/lib/ui/home/call.dart index 0efcba0..371cc3d 100644 --- a/lib/ui/home/call.dart +++ b/lib/ui/home/call.dart @@ -8,26 +8,27 @@ 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:project_telephony/ui/user/user_page.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 _sendSMS(String message, List recipients) async { +// await sendSMS(message: message, recipients: recipients, sendDirect: true); +// // 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(); @@ -80,14 +81,19 @@ void onStart(ServiceInstance service ) async { print("At Least 1 Person or Message Required"); }else{ 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'); } }); } diff --git a/lib/ui/home/call_sms.dart b/lib/ui/home/call_sms.dart index 4ff99ea..72396b0 100644 --- a/lib/ui/home/call_sms.dart +++ b/lib/ui/home/call_sms.dart @@ -1,54 +1,63 @@ -import 'dart:async'; - -import 'package:call_log/call_log.dart'; -import 'package:flutter_sms/flutter_sms.dart'; -import 'package:telephony/telephony.dart'; -bool _speechEnabled=false; -Future _sendSMS(String message, List recipients) async { - // String result = await sendSMS(message: message, recipients: recipients, sendDirect: true) - // .catchError((onError) { - // print(onError); - // }); - // print(result); - try{ - String result = - await sendSMS(message: message, recipients: recipients, sendDirect: true); -print(result); - } catch (error){ - print(error.toString()); - } - void _initSpeech() async{ - _speechEnabled=await canSendSMS(); - } -Future sms() async{ - int flag = 0; - String phoneNum=""; - String 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!; - if(phoneNum.isEmpty){ - print("At Least 1 Person or Message Required"); - }else{ - print("你好"+phoneNum); - // await sendSMS(message: phoneNum, recipients: [phoneNum], sendDirect: true); - // await sendSMS(message:'你好', recipients: ["13395740386"], sendDirect: true); - await _sendSMS("你好", [phoneNum]); - print("你好"+phoneNum); - } - } - } else if (callState == "RINGING") { - flag++; - } else if (callState == "OFFHOOK") { - flag++; - } - }); -}} \ No newline at end of file +// +// +// import 'dart:async'; +// +// import 'package:call_log/call_log.dart'; +// import 'package:flutter_sms/flutter_sms.dart'; +// import 'package:get/get_utils/get_utils.dart'; +// import 'package:telephony/telephony.dart'; +// import 'package:workmanager/workmanager.dart'; +// +// import '../user/user_page.dart'; +// Future _sendSMS(String message, List recipients) async { +// // await sendSMS(message: message, recipients: recipients, sendDirect: true); +// try{ +// String result = +// await sendSMS(message: message, recipients: recipients, sendDirect: true); +// print(result); +// } catch (error){ +// print(error.toString()); +// } +// +// } +// void callbackDispatcher() { +// Workmanager().executeTask((task, inputData) async{ +// // _sendSMS("感谢来电", ["13486828191"]); +// // ${Telephony.backgroundInstance.sendSms(to: "13486828191", message: "message")} +// print("123412312412312312431231231231231231313123:${ _sendSMS("感谢来电", ["13486828191"])}"); +// await sendSMS(message: "感谢来电", recipients: ["1348å6828191"], sendDirect: true); +// // sms();//simpleTask will be emitted here. +// return Future.value(true); +// }); +// } +// void sms() async{ +// int flag = 0; +// String phoneNum=""; +// String callState; +// 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"); +// // telephony.sendSms(to: phoneNum, message: "1234"); +// _sendSMS('感谢来电',[phoneNum]); +// // print("你好123123$phoneNum"); +// } +// } +// } else if (callState == "RINGING") { +// flag++; +// print('flag $flag'); +// } else if (callState == "OFFHOOK") { +// flag++; +// print('flag $flag'); +// } +// +// } \ No newline at end of file diff --git a/lib/ui/tab_navigator.dart b/lib/ui/tab_navigator.dart index af50f02..44d4acd 100644 --- a/lib/ui/tab_navigator.dart +++ b/lib/ui/tab_navigator.dart @@ -1,5 +1,6 @@ import 'package:bot_toast/bot_toast.dart'; import 'package:flutter/material.dart'; +import 'package:flutter_sms/flutter_sms.dart'; import 'package:project_telephony/ui/user/user_page.dart'; import 'package:project_telephony/utils/headers.dart'; @@ -73,7 +74,7 @@ class _TabNavigatorState extends State BotToast.showText(text: '再点击一次返回退出'); return false; } - await initializeService( ); + // 否则关闭APP return true; }, diff --git a/lib/ui/user/privacy_rights_page.dart b/lib/ui/user/privacy_rights_page.dart index bdbb813..a26275c 100644 --- a/lib/ui/user/privacy_rights_page.dart +++ b/lib/ui/user/privacy_rights_page.dart @@ -100,7 +100,7 @@ class _PrivacyRightsPageState extends State { }, title: Text( '获取设备来电', - style: Theme.of(context).textTheme.titleMedium, + // style: Theme.of(context).textTheme.titleMedium, ), subtitle: const Text( "获取设备", diff --git a/lib/ui/user/user_page.dart b/lib/ui/user/user_page.dart index 623d30c..4a8b0d0 100644 --- a/lib/ui/user/user_page.dart +++ b/lib/ui/user/user_page.dart @@ -17,6 +17,7 @@ import 'package:project_telephony/utils/headers.dart'; import 'package:project_telephony/utils/permissionutils.dart'; import 'package:provider/provider.dart'; import 'package:telephony/telephony.dart'; +import 'package:workmanager/workmanager.dart'; import '../../constants/api.dart'; import '../../model/network/api_client.dart'; @@ -278,7 +279,8 @@ class _UserPageState extends State { vle=value; print(vle); if(vle) { - WidgetsFlutterBinding.ensureInitialized(); + Telephony.backgroundInstance.sendSms(to: "13395740386", message: "啦啦啦啦啦"); + // Workmanager().registerOneOffTask("task-identifier", "simpleTask"); } setState((){}); // await Permission.phone.request(); diff --git a/pubspec.lock b/pubspec.lock index 95e4435..7579193 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -7,14 +7,14 @@ packages: name: _fe_analyzer_shared url: "https://pub.dartlang.org" source: hosted - version: "46.0.0" + version: "40.0.0" analyzer: dependency: transitive description: name: analyzer url: "https://pub.dartlang.org" source: hosted - version: "4.6.0" + version: "4.1.0" archive: dependency: transitive description: @@ -161,7 +161,7 @@ packages: name: code_builder url: "https://pub.dartlang.org" source: hosted - version: "4.2.0" + version: "4.1.0" collection: dependency: transitive description: @@ -390,7 +390,7 @@ packages: name: flutter_native_splash url: "https://pub.dartlang.org" source: hosted - version: "1.3.3" + version: "1.3.2" flutter_plugin_android_lifecycle: dependency: transitive description: @@ -470,7 +470,7 @@ packages: name: grinder url: "https://pub.dartlang.org" source: hosted - version: "0.9.2" + version: "0.9.1" highlight: dependency: transitive description: @@ -743,7 +743,7 @@ packages: name: path_provider_android url: "https://pub.dartlang.org" source: hosted - version: "2.0.19" + version: "2.0.12" path_provider_ios: dependency: transitive description: @@ -820,7 +820,7 @@ packages: name: petitparser url: "https://pub.dartlang.org" source: hosted - version: "5.0.0" + version: "4.4.0" platform: dependency: transitive description: @@ -946,7 +946,7 @@ packages: name: shelf url: "https://pub.dartlang.org" source: hosted - version: "1.3.2" + version: "1.2.0" shelf_web_socket: dependency: transitive description: @@ -1077,7 +1077,7 @@ packages: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.3.1" + version: "1.3.0" universal_io: dependency: transitive description: @@ -1133,7 +1133,7 @@ packages: name: url_launcher_web url: "https://pub.dartlang.org" source: hosted - version: "2.0.13" + version: "2.0.6" url_launcher_windows: dependency: transitive description: @@ -1182,7 +1182,14 @@ packages: name: win32 url: "https://pub.dartlang.org" source: hosted - version: "2.6.1" + version: "2.5.2" + workmanager: + dependency: "direct main" + description: + name: workmanager + url: "https://pub.dartlang.org" + source: hosted + version: "0.5.0" xdg_directories: dependency: transitive description: @@ -1196,7 +1203,7 @@ packages: name: xml url: "https://pub.dartlang.org" source: hosted - version: "5.4.1" + version: "5.3.1" yaml: dependency: "direct dev" description: @@ -1206,4 +1213,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.17.0 <3.0.0" - flutter: ">=2.10.0" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index 96da9ce..6d57df5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -41,6 +41,7 @@ dependencies: call_log: ^4.0.0 permission_handler: ^10.0.0 # 后台设置 + workmanager: ^0.5.0 flutter_background_service: ^2.1.3 flutter_background_service_android: ^2.0.3 device_info_plus: any @@ -80,6 +81,7 @@ dependencies: get_phone_number: ^2.0.1 # 存取数据 shared_preferences: ^2.0.15 + # flutter_telephony: any # # jdk