admin管理员组

文章数量:1405164

When using flutter_background_service, I am attempting to create a foreground service to allow my app access to the microphone when the user is off of the app. However, I get the foregroundServiceType 0x00000012 is not a subset of foregroundServiceType attribute 0x00000080 in service element of manifest file error whenever I run my app. Now, when reading the error I assumed what it is saying is I didn't add the necessary background service types to my AndroidManifest.xml file. But... I literally did. I checked documentation and it appears right. Here is my code and errors:

AndroidManifest.xml:

<manifest xmlns:android=";
xmlns:tools=";>
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE"/>
    <uses-permission android:name="android.permission.READ_MEDIA_AUDIO" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.POST_NOTIFICATIONS" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_SPECIAL_USE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />

    <application
        android:label="client"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">

        <service
            android:name="id.flutter.flutter_background_service.BackgroundService"
            android:foregroundServiceType="microphone"
        />


        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:taskAffinity=""
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
    <!-- Required to query activities that can process text, see:
          and
         .

         In particular, this is used by the Flutter engine in io.flutter.plugin.text.ProcessTextPlugin. -->
    <queries>
        <intent>
            <action android:name="android.intent.action.PROCESS_TEXT"/>
            <data android:mimeType="text/plain"/>
        </intent>
    </queries>
</manifest>

back_services.dart:

import 'dart:async';
import 'dart:ui';

import 'package:flutter/cupertino.dart';
import 'package:flutter_background_service/flutter_background_service.dart';

Future<void> initializeService() async {
  final service = FlutterBackgroundService();
  await service.configure(
      iosConfiguration: IosConfiguration(
          autoStart: false,
          onForeground: onStart,
          onBackground: onIosBackground),
      androidConfiguration: AndroidConfiguration(
        onStart: onStart,
        isForegroundMode: true,
        autoStart: true,
        foregroundServiceTypes: [
          AndroidForegroundType.microphone,
          // AndroidForegroundType.connectedDevice
        ],
      ));
}

Future<void> startService() async {
  print('Service start');
  final service = FlutterBackgroundService();
  final isRunning = await service.isRunning();
  if (!isRunning) {
    await service.startService();
  }
}

@pragma('vm:entry-point')
Future<bool> onIosBackground(ServiceInstance service) async {
  WidgetsFlutterBinding.ensureInitialized();
  DartPluginRegistrant.ensureInitialized();
  return true;
}

@pragma('vm:entry-point')
void onStart(ServiceInstance service) {
  DartPluginRegistrant.ensureInitialized();
  if (service is AndroidServiceInstance) {
    service.on('setAsForeground').listen((event) {
      print('service is foreground');
      service.setAsForegroundService();
    });
    service.on('setAsBackground').listen((event) {
      service.setAsBackgroundService();
    });

    service.setForegroundNotificationInfo(
        title: 'You\'re using NoteMate.',
        content:
            'We may be listening for messages or using your microphone (if recording lesson).');

    service.invoke('update');
  }
  service.on('stopService').listen((event) {
    print('service stop');
    service.stopSelf();
  });
  Timer.periodic(const Duration(seconds: 1), (timer) async {
    if (service is AndroidServiceInstance) {
      if (await service.isForegroundService()) {
        print('setting notif info');
        service.setForegroundNotificationInfo(
            title: 'You\'re using NoteMate.',
            content:
                'We may be listening for messages or using your microphone (if recording lesson).');
      }
    }

    service.invoke('update');
  });
}

startForegroundService() async {
  FlutterBackgroundService().invoke('setAsForeground');
  startService();
}

stopForegroundService() {
  FlutterBackgroundService().invoke('stopService');
}

File where I use the foreground service code:

  @override
  void initState() {
    super.initState();
    _stopwatch = Stopwatch();
    _audioRecorder = RecordPlatform.instance;
    _initRecorder();
  }

  Whenever I attempt to start it, I call:     stopForegroundService();

main.dart:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Permission.notification.isDenied.then(
    (value) {
      if (value) {
        Permission.notification.request();
      }
    },
  );
  await initializeService();
  runApp(App());
}

Full logs for crash:

√ Built build\app\outputs\flutter-apk\app-debug.apk
Installing build\app\outputs\flutter-apk\app-debug.apk...        1,553ms
I/flutter (18359): [IMPORTANT:flutter/shell/platform/android/android_context_gl_impeller(94)] Using the Impeller rendering backend (OpenGLES).
E/AndroidRuntime(18359): FATAL EXCEPTION: main
E/AndroidRuntime(18359): Process: com.example.client, PID: 18359
E/AndroidRuntime(18359): java.lang.RuntimeException: Unable to create service id.flutter.flutter_background_service.BackgroundService: java.lang.IllegalArgumentException: 
foregroundServiceType 0x00000012 is not a subset of foregroundServiceType attribute 0x00000080 in service element of manifest file
E/AndroidRuntime(18359):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4990)
E/AndroidRuntime(18359):        at android.app.ActivityThread.-$$Nest$mhandleCreateService(Unknown Source:0)
E/AndroidRuntime(18359):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2444)
E/AndroidRuntime(18359):        at android.os.Handler.dispatchMessage(Handler.java:107)
E/AndroidRuntime(18359):        at android.os.Looper.loopOnce(Looper.java:232)
E/AndroidRuntime(18359):        at android.os.Looper.loop(Looper.java:317)
E/AndroidRuntime(18359):        at android.app.ActivityThread.main(ActivityThread.java:8705)
E/AndroidRuntime(18359):        at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime(18359):        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
E/AndroidRuntime(18359):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
E/AndroidRuntime(18359): Caused by: java.lang.IllegalArgumentException: foregroundServiceType 0x00000012 is not a subset of foregroundServiceType attribute 0x00000080 in service element of manifest file
E/AndroidRuntime(18359):        at android.os.Parcel.createExceptionOrNull(Parcel.java:3246)
E/AndroidRuntime(18359):        at android.os.Parcel.createException(Parcel.java:3226)
E/AndroidRuntime(18359):        at android.os.Parcel.readException(Parcel.java:3209)
E/AndroidRuntime(18359):        at android.os.Parcel.readException(Parcel.java:3151)
E/AndroidRuntime(18359):        at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7193)
E/AndroidRuntime(18359):        at android.app.Service.startForeground(Service.java:863)
E/AndroidRuntime(18359):        at androidx.core.app.ServiceCompat$Api34Impl.startForeground(ServiceCompat.java:241)
E/AndroidRuntime(18359):        at androidx.core.app.ServiceCompat.startForeground(ServiceCompat.java:172)
E/AndroidRuntime(18359):        at id.flutter.flutter_background_service.BackgroundService.updateNotificationInfo(BackgroundService.java:173)
E/AndroidRuntime(18359):        at id.flutter.flutter_background_service.BackgroundService.onCreate(BackgroundService.java:101)
E/AndroidRuntime(18359):        at android.app.ActivityThread.handleCreateService(ActivityThread.java:4977)
E/AndroidRuntime(18359):        ... 9 more
E/AndroidRuntime(18359): Caused by: android.os.RemoteException: Remote stack trace:
E/AndroidRuntime(18359):        at com.android.server.am.ActiveServices.setServiceForegroundInnerLocked(ActiveServices.java:2219)
E/AndroidRuntime(18359):        at com.android.server.am.ActiveServices.setServiceForegroundLocked(ActiveServices.java:1843)
E/AndroidRuntime(18359):        at com.android.server.am.ActivityManagerService.setServiceForeground(ActivityManagerService.java:14092)
E/AndroidRuntime(18359):        at android.app.IActivityManager$Stub.onTransact(IActivityManager.java:3594)
E/AndroidRuntime(18359):        at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2812)
E/AndroidRuntime(18359):
Error connecting to the service protocol: failed to connect to http://127.0.0.1:62275/ToAoWRIyFG0=/ DartDevelopmentServiceException: Failed to start Dart Development
Service

本文标签: