diff --git a/android/app/src/debug/AndroidManifest.xml b/android/app/src/debug/AndroidManifest.xml index 1f444db..605fbed 100644 --- a/android/app/src/debug/AndroidManifest.xml +++ b/android/app/src/debug/AndroidManifest.xml @@ -1,8 +1,14 @@ + package="com.example.project_telephony" tools="http://schemas.android.com/tools"> - - + + + + + + + + diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 7c45efc..1c99814 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -1,5 +1,12 @@ + package="com.example.project_telephony" tools="http://schemas.android.com/tools" > + + + + + + + + package="com.example.project_telephony" tools="http://schemas.android.com/tools"> - + + + + + + + + + + + + + + diff --git a/lib/main.dart b/lib/main.dart index a213478..ddce297 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,22 +1,30 @@ // import 'dart:async'; +import 'dart:async'; + import 'package:bot_toast/bot_toast.dart'; -// import 'package:call_log/call_log.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:permission_handler/permission_handler.dart'; -// import 'package:project_telephony/permission.dart'; -// import 'package:project_telephony/ui/login/login_page.dart'; -import 'package:project_telephony/ui/tab_navigator.dart'; - -// import 'package:telephony/telephony.dart'; -// onBackgroundMessage(SmsMessage message) { -// debugPrint("onBackgroundMessage called"); -// } +import 'package:project_telephony/ui/tab_navigator.dart'; +import 'package:project_telephony/ui/user/privacy_rights_page.dart'; +import 'package:telephony/telephony.dart'; + +void _sendSMS(String message, List recipients) async { + try{ + String result = + await sendSMS(message: message, recipients: recipients, sendDirect: true); + print(result); + } on PlatformException catch(e){ + print(e.toString()); + } +} void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -24,73 +32,71 @@ void main() async { LoggerData.addData(details); FlutterError.presentError(details); }; - // await initializeService(); + 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, -// ), -// ); -// } +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; -// final Iterable result = await CallLog.query(); -// phoneNum = result.first.number; -// print(phoneNum); -// Phone.telephony.sendSms( -// to: phoneNum!, -// message: "hello", -// isMultipart: true, -// ); -// phoneNum = result.first.number; -// // print(phoneNum); -// Phone.telephony.sendSms( -// to: phoneNum!, -// message: "hello", -// isMultipart: true, -// ); -// } -// } else if (callState == "RINGING") { -// flag++; -// } else if (callState == "OFFHOOK") { -// flag++; -// } -// }); -// } - -// class Phone { -// static Telephony telephony = Telephony.instance; -// } +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); + List recipents = ["13486828191"]; + // CallState state = await telephony.callState; + _sendSMS("你好", recipents); + // phoneNum = result.first.number; + // // print(phoneNum); + // Phone.telephony.sendSms( + // to: phoneNum!, + // message: "hello", + // 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); @@ -101,12 +107,15 @@ class MyApp extends StatefulWidget { 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(); // PowerLogger.start(context, debug: true); final service = FlutterBackgroundService(); + final inbox = Telephony.instance.getInboxSms(); // JPush jPush=JPush(); // jPush.setup( // appKey: "", @@ -128,7 +137,6 @@ class _MyAppState extends State { // openAppSettings(); // }); } - // onMessage(SmsMessage message) async { // setState(() { // _message = message.body ?? "Error reading message body."; @@ -153,7 +161,7 @@ class _MyAppState extends State { @override Widget build(BuildContext context) { return MediaQuery( - data: MediaQueryData.fromWindow(WidgetsBinding.instance!.window), + data: MediaQueryData.fromWindow(WidgetsBinding.instance.window), child: ScreenUtilInit( designSize: const Size(750, 1334), builder: (context, child) { diff --git a/lib/ui/home/content_page.dart b/lib/ui/home/content_page.dart index 339ea56..c400555 100644 --- a/lib/ui/home/content_page.dart +++ b/lib/ui/home/content_page.dart @@ -1,8 +1,9 @@ -// import 'dart:async'; -// import android.app.PendingIntent; -// import android.telephony.SmsManager; + +import 'package:call_log/call_log.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; +import 'package:flutter_sms/flutter_sms.dart'; +import 'package:get_phone_number/get_phone_number.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:project_telephony/base/base_style.dart'; import 'package:project_telephony/ui/home/content_details_page.dart'; @@ -34,6 +35,7 @@ class _ContentPageState extends State { // } // } // SmsManager smsManager = SmsManager.getDefault(); + int _select = 0; List textList = ['欢迎你的来电', '祝您生活愉快', '感谢您的来电我们会尽快处理的', '自定义短信内容']; List textList1 = ['自定义短信内容']; @@ -92,16 +94,30 @@ class _ContentPageState extends State { // }else{ // print(content); // } - print(content); - // sendSms(content); - telephony.sendSms( - to: "13486828191", - message: content, - ); + // print(content); + // // sendSms(content); + // telephony.sendSms( + // to: "13395740386", + // message: content, + // isMultipart: true + // ); + List recipents=["13395740386"]; + _sendSMS(content,recipents); + // CallState state = await telephony.callState; + // print(state); + // DataState state1 = await telephony.cellularDataState; + // print(state1); + // String? operatorName = await telephony.networkOperatorName; + // print(operatorName); + // NetworkType type = await telephony.dataNetworkType; + // print(type); + // PhoneType type1 = await telephony.phoneType; + // print(type1); + String phoneNumber = await GetPhoneNumber().get(); + print('getPhoneNumber result: $phoneNumber'); } setState(() {}); // print("这是数据" + textList[_s lect]); - // print(index); }, onLongPress: () { @@ -140,4 +156,13 @@ class _ContentPageState extends State { ), ); } + + void _sendSMS(String message, List recipients) async { + String _result = await sendSMS( + message: message, recipients: recipients, sendDirect: true) + .catchError((onError) { + print(onError); + }); + print(_result); + } } diff --git a/pubspec.lock b/pubspec.lock index e9dca52..8e0b7d9 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -398,6 +398,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "5.5.3+2" + flutter_sms: + dependency: "direct main" + description: + name: flutter_sms + url: "https://pub.dartlang.org" + source: hosted + version: "2.3.3" flutter_test: dependency: "direct dev" description: flutter @@ -429,6 +436,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "4.6.5" + get_phone_number: + dependency: "direct main" + description: + name: get_phone_number + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.1" glob: dependency: transitive description: @@ -1050,6 +1064,62 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.0.4" + url_launcher: + dependency: transitive + description: + name: url_launcher + url: "https://pub.dartlang.org" + source: hosted + version: "6.1.5" + url_launcher_android: + dependency: transitive + description: + name: url_launcher_android + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.17" + url_launcher_ios: + dependency: transitive + description: + name: url_launcher_ios + url: "https://pub.dartlang.org" + source: hosted + version: "6.0.17" + url_launcher_linux: + dependency: transitive + description: + name: url_launcher_linux + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + url_launcher_macos: + dependency: transitive + description: + name: url_launcher_macos + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" + url_launcher_platform_interface: + dependency: transitive + description: + name: url_launcher_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "2.1.0" + url_launcher_web: + dependency: transitive + description: + name: url_launcher_web + url: "https://pub.dartlang.org" + source: hosted + version: "2.0.13" + url_launcher_windows: + dependency: transitive + description: + name: url_launcher_windows + url: "https://pub.dartlang.org" + source: hosted + version: "3.0.1" vector_math: dependency: transitive description: @@ -1115,4 +1185,4 @@ packages: version: "3.1.1" sdks: dart: ">=2.17.0-206.0.dev <3.0.0" - flutter: ">=2.8.1" + flutter: ">=2.10.0" diff --git a/pubspec.yaml b/pubspec.yaml index 8cab645..106caec 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -18,7 +18,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev version: 1.0.0+1 environment: - sdk: ">=2.15.0 <3.0.0" + sdk: ">=2.16.0 <3.0.0" # Dependencies specify other packages that your package needs in order to work. # To automatically upgrade your package dependencies to the latest versions @@ -71,6 +71,11 @@ dependencies: permission_handler_platform_interface: ^3.7.0 #日志输出 power_logger: ^1.2.2 +# 短信 + flutter_sms: ^2.3.3 + get_phone_number: ^2.0.1 +# flutter_telephony: any + # # jdk # jverify: ^2.2.5 ## pub 集成