diff --git a/android/app/build.gradle b/android/app/build.gradle index 7f964be..c497b2d 100644 --- a/android/app/build.gradle +++ b/android/app/build.gradle @@ -25,7 +25,7 @@ apply plugin: 'com.android.application' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion flutter.compileSdkVersion + compileSdkVersion 33 compileOptions { sourceCompatibility JavaVersion.VERSION_1_8 @@ -36,7 +36,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 32 + targetSdkVersion 33 versionCode flutterVersionCode.toInteger() versionName flutterVersionName // multiDexEnabled true diff --git a/lib/main.dart b/lib/main.dart index 331eb2e..10fdf4b 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,6 +6,8 @@ import 'package:flutter/services.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; import 'package:flutter_screenutil/flutter_screenutil.dart'; import 'package:get/get_navigation/src/root/get_material_app.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'; @@ -26,6 +28,7 @@ void main() async { Future initializeService() async { final service = FlutterBackgroundService(); + service.setNotificationInfo(title: '短信助手', content: '正在后台运行'); await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, @@ -59,6 +62,13 @@ void onStart() { 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!, @@ -100,6 +110,18 @@ class _MyAppState extends State { // 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 { diff --git a/lib/permission.dart b/lib/permission.dart new file mode 100644 index 0000000..6e81693 --- /dev/null +++ b/lib/permission.dart @@ -0,0 +1,53 @@ +import 'package:flutter/cupertino.dart'; +import 'package:permission_handler/permission_handler.dart'; + +class PermissionHelper { + static VoidCallback defaultCall = () {}; + + ///检查权限 + static void check(List permissionList, + {String? errMsg, + VoidCallback? onSuccess, + VoidCallback? onFailed, + VoidCallback? onOpenSetting}) async { + bool flag = true; + for (var value in permissionList) { + var status = await value.status; + if (!status.isGranted) { + flag = false; + break; + } + } + if (!flag) { + PermissionStatus permissionStatus = + await requestPermission(permissionList); + if (permissionStatus.isGranted) { + onSuccess != null ? onSuccess() : defaultCall(); + } else if (permissionStatus.isDenied) { + onFailed != null ? onFailed() : defaultCall(); + } else if (permissionStatus.isPermanentlyDenied) { + onOpenSetting != null ? onOpenSetting() : defaultCall(); + } else if (permissionStatus.isRestricted) { + //IOS单独处理 + onOpenSetting != null ? onOpenSetting() : defaultCall(); + } else if (permissionStatus.isLimited) { + //IOS单独处理 + onOpenSetting != null ? onOpenSetting() : defaultCall(); + } + } + } + + //申请权限 + static Future requestPermission( + List permissionList) async { + Map statuses = await permissionList.request(); + PermissionStatus currentPermissionStatus = PermissionStatus.granted; + statuses.forEach((key, value) { + if (!value.isGranted) { + currentPermissionStatus = value; + return; + } + }); + return currentPermissionStatus; + } +} diff --git a/pubspec.lock b/pubspec.lock index e9d5aa0..98ecfe4 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -320,28 +320,7 @@ packages: name: flutter_background_service url: "https://pub.dartlang.org" source: hosted - version: "0.2.8+5" - flutter_background_service_android: - dependency: transitive - description: - name: flutter_background_service_android - url: "https://pub.dartlang.org" - source: hosted - version: "0.0.2" - flutter_background_service_ios: - dependency: transitive - description: - name: flutter_background_service_ios - url: "https://pub.dartlang.org" - source: hosted - version: "0.0.2" - flutter_background_service_platform_interface: - dependency: transitive - description: - name: flutter_background_service_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "0.0.1+3" + version: "0.2.6" flutter_gen_core: dependency: transitive description: @@ -394,6 +373,7 @@ packages: description: flutter source: sdk version: "0.0.0" +<<<<<<< HEAD fluwx: dependency: "direct main" description: @@ -401,6 +381,8 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "3.9.1" +======= +>>>>>>> 155d2f3fbe4865345533a810a4489d63adad5e1d frontend_server_client: dependency: transitive description: @@ -533,7 +515,7 @@ packages: name: js url: "https://pub.dartlang.org" source: hosted - version: "0.6.4" + version: "0.6.3" json_annotation: dependency: "direct main" description: @@ -576,6 +558,7 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.11" +<<<<<<< HEAD material_color_utilities: dependency: transitive description: @@ -583,6 +566,8 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.4" +======= +>>>>>>> 155d2f3fbe4865345533a810a4489d63adad5e1d meta: dependency: transitive description: @@ -638,7 +623,7 @@ packages: name: path_provider_android url: "https://pub.dartlang.org" source: hosted - version: "2.0.17" + version: "2.0.12" path_provider_ios: dependency: transitive description: @@ -834,7 +819,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: @@ -916,7 +901,7 @@ packages: name: telephony url: "https://pub.dartlang.org" source: hosted - version: "0.2.0" + version: "0.1.4" term_glyph: dependency: transitive description: @@ -930,7 +915,11 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted +<<<<<<< HEAD version: "0.4.9" +======= + version: "0.4.3" +>>>>>>> 155d2f3fbe4865345533a810a4489d63adad5e1d time: dependency: transitive description: @@ -1030,5 +1019,10 @@ packages: source: hosted version: "3.1.1" sdks: +<<<<<<< HEAD dart: ">=2.17.0-0 <3.0.0" flutter: ">=2.8.1" +======= + dart: ">=2.15.0 <3.0.0" + flutter: ">=2.8.0" +>>>>>>> 155d2f3fbe4865345533a810a4489d63adad5e1d diff --git a/pubspec.yaml b/pubspec.yaml index ea8c62f..4f04080 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,12 +34,13 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.2 + telephony: ^0.1.4 # 获取来电状态 - telephony: any provider: ^6.0.3 permission_handler: ^10.0.0 # 获取通话记录 call_log: ^4.0.0 + permission_handler: ^10.0.0 # 后台设置 flutter_background_service: ^0.2.6 device_info_plus: any