FlutterBackgroundTask在Android 14(API 34)下适配问题求助
Android targetSdkVersion升级至34后FlutterForegroundTask屏幕共享后台服务异常修复方案
问题核心
将targetSdkVersion从33升级到34后,Android平台使用FlutterForegroundTask实现的屏幕共享后台服务出现异常,iOS平台无问题。这是因为Android 14(API 34)对前台服务新增了权限限制和类型要求,原代码未适配这些新规则。
修复步骤
1. 更新AndroidManifest.xml配置
在项目的android/app/src/main/AndroidManifest.xml中添加必要权限,并配置前台服务类型:
<!-- Android 13+ 通知权限 --> <uses-permission android:name="android.permission.POST_NOTIFICATIONS" /> <!-- 屏幕共享专属前台服务权限 --> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PROJECTION" /> <!-- 找到FlutterForegroundTask的服务注册项,添加前台服务类型 --> <service android:name="com.pravera.flutter_foreground_task.service.ForegroundService" android:foregroundServiceType="mediaProjection" android:stopWithTask="true" />
2. 动态申请通知权限
Android 13及以上版本要求动态申请通知权限,启动服务前必须完成权限申请:
import 'package:permission_handler/permission_handler.dart'; // 请求通知权限 Future<bool> _requestNotificationPermission() async { final status = await Permission.notification.request(); return status.isGranted; }
3. 适配FlutterForegroundTask初始化配置
调整通知渠道配置,适配Android 14的通知类别要求,同时确保使用最新版插件:
FlutterForegroundTask.init( androidNotificationOptions: AndroidNotificationOptions( channelId: 'screen_share_channel', channelName: '屏幕共享服务', channelDescription: '屏幕共享后台运行时显示此通知', channelImportance: NotificationChannelImportance.HIGH, priority: NotificationPriority.HIGH, // 指定通知类别为服务类,适配后台场景 category: NotificationCategory.SERVICE, iconData: const NotificationIconData( resType: ResourceType.mipmap, resPrefix: ResourcePrefix.ic, name: 'launcher', ), ), iosNotificationOptions: const IOSNotificationOptions( showNotification: true, playSound: false, ), foregroundTaskOptions: const ForegroundTaskOptions( interval: 5000, isOnceEvent: false, autoRunOnBoot: true, allowWakeLock: true, allowWifiLock: true, ), );
4. 确保屏幕共享核心权限(MediaProjection)已获取
屏幕共享依赖系统的MediaProjection权限,启动服务前必须先通过原生代码或插件获取该权限(通常通过MediaProjectionManager发起权限请求),否则前台服务无法正常运行。
5. 升级FlutterForegroundTask插件版本
确保使用支持Android 14的最新版插件,在pubspec.yaml中更新版本:
dependencies: flutter_foreground_task: ^6.1.0 # 以pub.dev最新版本为准
完整启动流程示例
import 'package:flutter_foreground_task/flutter_foreground_task.dart'; import 'package:permission_handler/permission_handler.dart'; // 初始化服务 void initScreenShareService() { FlutterForegroundTask.init( androidNotificationOptions: AndroidNotificationOptions( channelId: 'screen_share_channel', channelName: '屏幕共享服务', channelDescription: '屏幕共享后台运行时显示的通知', channelImportance: NotificationChannelImportance.HIGH, priority: NotificationPriority.HIGH, category: NotificationCategory.SERVICE, iconData: const NotificationIconData( resType: ResourceType.mipmap, resPrefix: ResourcePrefix.ic, name: 'launcher', ), ), iosNotificationOptions: const IOSNotificationOptions( showNotification: true, playSound: false, ), foregroundTaskOptions: const ForegroundTaskOptions( interval: 5000, isOnceEvent: false, autoRunOnBoot: true, allowWakeLock: true, allowWifiLock: true, ), ); } // 启动屏幕共享服务 Future<void> startScreenShare() async { // 先申请通知权限 final hasNotificationPerm = await _requestNotificationPermission(); if (!hasNotificationPerm) { throw Exception('通知权限未授予,无法启动服务'); } // 这里需补充MediaProjection权限的检查与申请逻辑 // final hasMediaProjectionPerm = await _requestMediaProjection(); // if (!hasMediaProjectionPerm) { // throw Exception('屏幕录制权限未授予,无法启动服务'); // } // 启动前台服务 await FlutterForegroundTask.startService( notificationTitle: '屏幕共享中', notificationText: '后台服务正在运行', ); }
内容的提问来源于stack exchange,提问作者Inquisitive Owl
相关产品推荐
相关产品推荐

