From ad97721b059db0f8686cbee4a8bef6b5bc6be8c5 Mon Sep 17 00:00:00 2001 From: datang Date: Fri, 5 Aug 2022 17:06:42 +0800 Subject: [PATCH] permission --- android/app/build.gradle | 4 +- lib/main.dart | 18 +++++- lib/permission.dart | 53 +++++++++++++++++ pubspec.lock | 124 +++++++++++---------------------------- pubspec.yaml | 7 +-- 5 files changed, 110 insertions(+), 96 deletions(-) create mode 100644 lib/permission.dart diff --git a/android/app/build.gradle b/android/app/build.gradle index c8c8611..20188d5 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 } diff --git a/lib/main.dart b/lib/main.dart index f059485..9d9a489 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -2,6 +2,8 @@ import 'dart:async'; import 'package:call_log/call_log.dart'; import 'package:flutter/material.dart'; import 'package:flutter_background_service/flutter_background_service.dart'; +import 'package:permission_handler/permission_handler.dart'; +import 'package:project_telephony/permission.dart'; import 'package:telephony/telephony.dart'; onBackgroundMessage(SmsMessage message) { @@ -16,6 +18,7 @@ Future main() async { Future initializeService() async { final service = FlutterBackgroundService(); + service.setNotificationInfo(title: '短信助手',content: '正在后台运行'); await service.configure( androidConfiguration: AndroidConfiguration( onStart: onStart, @@ -48,7 +51,7 @@ void onStart() { if (flag != 0) { flag = 0; final Iterable result = await CallLog.query(); - phoneNum = await result.first.number; + phoneNum = result.first.number; print(phoneNum); Phone.telephony.sendSms(to: phoneNum!, message: "hello",isMultipart:true,); } @@ -79,6 +82,19 @@ class _MyAppState extends State { super.initState(); final service = FlutterBackgroundService(); 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 2ba9110..e5e0ede 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -57,48 +57,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.0.5" - device_info_plus: - dependency: "direct main" - description: - name: device_info_plus - url: "https://pub.dartlang.org" - source: hosted - version: "4.0.0" - device_info_plus_linux: - dependency: transitive - description: - name: device_info_plus_linux - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" - device_info_plus_macos: - dependency: transitive - description: - name: device_info_plus_macos - url: "https://pub.dartlang.org" - source: hosted - version: "2.2.3" - device_info_plus_platform_interface: - dependency: transitive - description: - name: device_info_plus_platform_interface - url: "https://pub.dartlang.org" - source: hosted - version: "2.3.0+1" - device_info_plus_web: - dependency: transitive - description: - name: device_info_plus_web - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.0" - device_info_plus_windows: - dependency: transitive - description: - name: device_info_plus_windows - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.1" fake_async: dependency: transitive description: @@ -106,20 +64,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.2.0" - ffi: - dependency: transitive - description: - name: ffi - url: "https://pub.dartlang.org" - source: hosted - version: "1.2.1" - file: - dependency: transitive - description: - name: file - url: "https://pub.dartlang.org" - source: hosted - version: "6.1.2" flutter: dependency: "direct main" description: flutter @@ -144,18 +88,6 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_web_plugins: - dependency: transitive - description: flutter - source: sdk - version: "0.0.0" - js: - dependency: transitive - description: - name: js - url: "https://pub.dartlang.org" - source: hosted - version: "0.6.3" lints: dependency: transitive description: @@ -177,20 +109,48 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "1.7.0" - nested: + path: dependency: transitive description: - name: nested + name: path url: "https://pub.dartlang.org" source: hosted - version: "1.0.0" - path: + version: "1.8.0" + permission_handler: + dependency: "direct main" + description: + name: permission_handler + url: "https://pub.dartlang.org" + source: hosted + version: "10.0.0" + permission_handler_android: dependency: transitive description: - name: path + name: permission_handler_android url: "https://pub.dartlang.org" source: hosted - version: "1.8.0" + version: "10.0.0" + permission_handler_apple: + dependency: transitive + description: + name: permission_handler_apple + url: "https://pub.dartlang.org" + source: hosted + version: "9.0.4" + permission_handler_platform_interface: + dependency: transitive + description: + name: permission_handler_platform_interface + url: "https://pub.dartlang.org" + source: hosted + version: "3.7.0" + permission_handler_windows: + dependency: transitive + description: + name: permission_handler_windows + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.0" platform: dependency: transitive description: @@ -205,13 +165,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.2" - provider: - dependency: "direct main" - description: - name: provider - url: "https://pub.dartlang.org" - source: hosted - version: "6.0.3" sky_engine: dependency: transitive description: flutter @@ -280,13 +233,6 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "2.1.1" - win32: - dependency: transitive - description: - name: win32 - url: "https://pub.dartlang.org" - source: hosted - version: "2.5.2" sdks: dart: ">=2.15.0 <3.0.0" - flutter: ">=2.0.0" + flutter: ">=2.8.0" diff --git a/pubspec.yaml b/pubspec.yaml index 767fc80..16e705b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,11 +34,10 @@ 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: any - provider: ^6.0.3 + telephony: ^0.1.4 call_log: ^4.0.0 - flutter_background_service: any - device_info_plus: any + flutter_background_service: ^0.2.6 + permission_handler: ^10.0.0 dev_dependencies: flutter_test: