You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android14下Flutter后台定位服务启动崩溃问题求助

问题:Android 14上使用flutter_background_service与geolocator启动后台服务崩溃

我使用flutter_background_service: ^5.0.7搭配geolocator: ^11.0.0实现后台位置追踪功能,但在Android 14系统启动服务时应用直接崩溃。已按文档要求添加权限和配置,问题仍未解决。

异常信息

E/AndroidRuntime(20806): FATAL EXCEPTION: main
E/AndroidRuntime(20806): Process: com.example.procss, PID: 20806
E/AndroidRuntime(20806): java.lang.RuntimeException: Unable to create service id.flutter.flutter_background_service.BackgroundService: java.lang.IllegalArgumentException: foregroundServiceType 0x00000008 is not a subset of foregroundServiceType attribute 0x00000000 in service element of manifest file
E/AndroidRuntime(20806):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:5111)
E/AndroidRuntime(20806):    at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(20806):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2506)
E/AndroidRuntime(20806):    at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime(20806):    at android.os.Looper.loopOnce(Looper.java:230)
E/AndroidRuntime(20806):    at android.os.Looper.loop(Looper.java:319)
E/AndroidRuntime(20806):    at android.app.ActivityThread.main(ActivityThread.java:8893)
E/AndroidRuntime(20806):    at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(20806):    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:608)
E/AndroidRuntime(20806):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1103)
E/AndroidRuntime(20806): Caused by: java.lang.IllegalArgumentException: foregroundServiceType 0x00000008 is not a subset of foregroundServiceType attribute 0x00000000 in service element of manifest file
E/AndroidRuntime(20806):    at android.os.Parcel.createExceptionOrNull(Parcel.java:3073)
E/AndroidRuntime(20806):    at android.os.Parcel.createException(Parcel.java:3053)
E/AndroidRuntime(20806):    at android.os.Parcel.readException(Parcel.java:3036)
E/AndroidRuntime(20806):    at android.os.Parcel.readException(Parcel.java:2978)
E/AndroidRuntime(20806):    at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7214)
E/AndroidRuntime(20806):    at android.app.Service.startForeground(Service.java:862)
E/AndroidRuntime(20806):    at androidx.core.app.ServiceCompat$Api34Impl.startForeground(ServiceCompat.java:241)
E/AndroidRuntime(20806):    at androidx.core.app.ServiceCompat.startForeground(ServiceCompat.java:172)
E/AndroidRuntime(20806):    at id.flutter.flutter_background_service.BackgroundService.updateNotificationInfo(BackgroundService.java:175)
E/AndroidRuntime(20806):    at id.flutter.flutter_background_service.BackgroundService.onCreate(BackgroundService.java:107)
E/AndroidRuntime(20806):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:5098)
E/AndroidRuntime(20806):    ... 9 more
E/AndroidRuntime(20806): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime(20806):    at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2171)
E/AndroidRuntime(20806):    at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1785)
E/AndroidRuntime(20806):    at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:15850)
E/AndroidRuntime(20806):    at android.app.IActivityManager$Stub.onTransact$setServiceForeground$(IActivityManager.java:11873)
E/AndroidRuntime(20806):    at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3499)
E/AndroidRuntime(20806): 

当前配置

AndroidManifest.xml权限与服务配置

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />


    <service
        android:name=".flutter_background_service.BackgroundService"
        android:foregroundServiceType="location"
        />

注:添加FOREGROUND_SERVICE_LOCATION权限时,IDE提示无法识别,建议替换为FOREGROUND_SERVICE

android/build.gradle配置

ext.kotlin_version = '1.8.10'
    repositories {
        google()
        mavenCentral()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:7.4.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}allprojects {
    repositories {
        google()
        mavenCentral()
    }
}
rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}subprojects {
    project.evaluationDependsOn(':app')
}
tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

gradle-wrapper.properties配置

distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https://services.gradle.org/distributions/gradle-7.5-all.zip

main.dart中的服务配置

androidConfiguration: AndroidConfiguration(
      // 应用前后台时都会在独立isolate执行
      onStart: onStart,
      // 自动启动服务
      autoStart: false,
      isForegroundMode: true,
      initialNotificationTitle: 'Background Service',
      initialNotificationContent: 'Location Service Running',
                            /// 指定服务类型
        foregroundServiceType: AndroidForegroundType.location,
        foregroundServiceNotificationId: 888
    ),
    iosConfiguration: IosConfiguration(
      // 自动启动服务
      autoStart: false,
      // 应用前台时在独立isolate执行
      onForeground: onStart,

      ///Todo
      // 需要在Xcode项目中开启后台获取权限
      //onBackground: onIosBackground,
    ),
  );

解决方案

问题核心是AndroidManifest.xml中声明的服务路径错误,导致系统找不到正确的服务配置,异常提示的foregroundServiceType不匹配本质是因为配置的服务并非插件实际使用的服务。

1. 修正AndroidManifest.xml中的服务路径

插件的后台服务类路径是id.flutter.flutter_background_service.BackgroundService,而非你当前写的.flutter_background_service.BackgroundService(前缀点表示当前应用包路径,插件服务不在此路径下)。修改后:

<service
    android:name="id.flutter.flutter_background_service.BackgroundService"
    android:foregroundServiceType="location"
    android:exported="false" />

2. 解决IDE无法识别FOREGROUND_SERVICE_LOCATION的问题

FOREGROUND_SERVICE_LOCATION是Android 12(API 31)新增权限,需确保项目compileSdkVersion至少为31,建议直接适配Android 14:
在app/build.gradle中修改:

android {
    compileSdkVersion 34

    defaultConfig {
        targetSdkVersion 34
    }
}

3. 确保动态申请权限

Android 10及以上版本,后台位置权限需用户手动授权,需在代码中动态申请ACCESS_FINE_LOCATION、ACCESS_COARSE_LOCATION以及ACCESS_BACKGROUND_LOCATION权限。

内容的提问来源于stack exchange,提问作者MRS

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 19:25:53