97 lines
3.0 KiB
Dart
97 lines
3.0 KiB
Dart
import 'dart:ffi';
|
|
|
|
import 'package:permission_handler/permission_handler.dart';
|
|
import 'dart:io';
|
|
|
|
import '../device_info.dart';
|
|
|
|
class AndroidPermissionHandler {
|
|
Future<void> requestAllPermissions() async {
|
|
await requestLocationPermission();
|
|
await requestBluetoothPermission();
|
|
await requestCameraPermission();
|
|
await requestStoragePermission();
|
|
await requestNotificationPermission();
|
|
await requestPhonePermission();
|
|
}
|
|
|
|
Future<void> requestLocationPermission() async {
|
|
PermissionStatus status;
|
|
if (Platform.isAndroid && await DeviceInfo.getAndroidSdkVersion() >= 29) {
|
|
status = await Permission.locationAlways.request();
|
|
} else {
|
|
status = await Permission.locationWhenInUse.request();
|
|
}
|
|
|
|
if (status.isGranted) {
|
|
print("Android: 位置权限已授予");
|
|
} else if (status.isPermanentlyDenied) {
|
|
print("Android: 位置权限被永久拒绝,请前往设置开启");
|
|
} else {
|
|
print("Android: 位置权限被拒绝");
|
|
}
|
|
}
|
|
|
|
Future<void> requestBluetoothPermission() async {
|
|
PermissionStatus status;
|
|
if (Platform.isAndroid && await DeviceInfo.getAndroidSdkVersion() >= 31) {
|
|
status = (await [
|
|
Permission.bluetoothScan,
|
|
Permission.bluetoothConnect,
|
|
Permission.bluetoothAdvertise,
|
|
].request()) as PermissionStatus;
|
|
} else {
|
|
status = await Permission.bluetooth.request();
|
|
}
|
|
|
|
if (status.isGranted) {
|
|
print("Android: 蓝牙权限已授予");
|
|
} else {
|
|
print("Android: 蓝牙权限被拒绝");
|
|
}
|
|
}
|
|
|
|
Future<void> requestCameraPermission() async {
|
|
PermissionStatus status = await Permission.camera.request();
|
|
if (status.isGranted) {
|
|
print("Android: 相机权限已授予");
|
|
} else if (status.isPermanentlyDenied) {
|
|
print("Android: 相机权限被永久拒绝,请前往设置开启");
|
|
} else {
|
|
print("Android: 相机权限被拒绝");
|
|
}
|
|
}
|
|
|
|
Future<void> requestStoragePermission() async {
|
|
PermissionStatus status = await Permission.storage.request();
|
|
if (status.isGranted) {
|
|
print("Android: 存储权限已授予");
|
|
} else if (status.isPermanentlyDenied) {
|
|
print("Android: 存储权限被永久拒绝,请前往设置开启");
|
|
} else {
|
|
print("Android: 存储权限被拒绝");
|
|
}
|
|
}
|
|
|
|
Future<void> requestNotificationPermission() async {
|
|
if (Platform.isAndroid && await DeviceInfo.getAndroidSdkVersion() >= 33) {
|
|
PermissionStatus status = await Permission.notification.request();
|
|
if (status.isGranted) {
|
|
print("Android: 通知权限已授予");
|
|
} else {
|
|
print("Android: 通知权限被拒绝");
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> requestPhonePermission() async {
|
|
PermissionStatus status = await Permission.phone.request();
|
|
if (status.isGranted) {
|
|
print("Android: 电话权限已授予");
|
|
} else if (status.isPermanentlyDenied) {
|
|
print("Android: 电话权限被永久拒绝,请前往设置开启");
|
|
} else {
|
|
print("Android: 电话权限被拒绝");
|
|
}
|
|
}
|
|
} |