audio_service icon indicating copy to clipboard operation
audio_service copied to clipboard

[Android] RuntimeException on closing app with another running foreground service

Open Dix206 opened this issue 4 years ago • 4 comments

Which API doesn't behave as documented, and how does it misbehave? Hello! I have the problem that I get a native Android exception when I completely close the App. This problem only occurs if another foreground service is running. In my example this is a foreground step counter service which also runs when the app is closed. I have already debugged this behavior. The exception is thrown inside the AudioServicePlugin.java in line 82 (flutterEngine.destroy();). When I remove this line, the bug is fixed and my step counter service keeps running. But now the AudioService doesn't stopped reasonable on an app close. It looks like this is only a problem if a media item has been initialized and not stopped. Otherwise I haven't seen a problem without flutterEngine.destroy();.

Minimal reproduction project The example with another foreground service

To Reproduce (i.e. user steps, not code) Steps to reproduce the behavior:

  1. Start the App and initialize the Audio Service
  2. Start another foreground service
  3. Completely close the app (task manager)

Error messages

/libEGL  ( 7519): call to OpenGL ES API with no current context (logged once per thread)
D/AndroidRuntime( 7519): Shutting down VM
E/AndroidRuntime( 7519): FATAL EXCEPTION: main
E/AndroidRuntime( 7519): Process: XXX PID: XXX
E/AndroidRuntime( 7519): java.lang.RuntimeException: Unable to stop service com.ryanheise.audioservice.AudioService@f9727ac: java.lang.RuntimeException: Cannot execute operation because FlutterJNI is not attached to native.
E/AndroidRuntime( 7519): 	at android.app.ActivityThread.handleStopService(ActivityThread.java:4682)
E/AndroidRuntime( 7519): 	at android.app.ActivityThread.access$2000(ActivityThread.java:247)
E/AndroidRuntime( 7519): 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2089)
E/AndroidRuntime( 7519): 	at android.os.Handler.dispatchMessage(Handler.java:106)
E/AndroidRuntime( 7519): 	at android.os.Looper.loopOnce(Looper.java:201)
E/AndroidRuntime( 7519): 	at android.os.Looper.loop(Looper.java:288)
E/AndroidRuntime( 7519): 	at android.app.ActivityThread.main(ActivityThread.java:7822)
E/AndroidRuntime( 7519): 	at java.lang.reflect.Method.invoke(Native Method)
E/AndroidRuntime( 7519): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
E/AndroidRuntime( 7519): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
E/AndroidRuntime( 7519): Caused by: java.lang.RuntimeException: Cannot execute operation because FlutterJNI is not attached to native.
E/AndroidRuntime( 7519): 	at io.flutter.embedding.engine.FlutterJNI.ensureAttachedToNative(FlutterJNI.java:386)
E/AndroidRuntime( 7519): 	at io.flutter.embedding.engine.FlutterJNI.detachFromNativeAndReleaseResources(FlutterJNI.java:370)
E/AndroidRuntime( 7519): 	at io.flutter.embedding.engine.FlutterEngine.destroy(FlutterEngine.java:421)
E/AndroidRuntime( 7519): 	at com.ryanheise.audioservice.AudioServicePlugin.disposeFlutterEngine(AudioServicePlugin.java:82)
E/AndroidRuntime( 7519): 	at com.ryanheise.audioservice.AudioServicePlugin$AudioHandlerInterface.onDestroy(AudioServicePlugin.java:765)
E/AndroidRuntime( 7519): 	at com.ryanheise.audioservice.AudioService.onDestroy(AudioService.java:296)
E/AndroidRuntime( 7519): 	at android.app.ActivityThread.handleStopService(ActivityThread.java:4662)

Expected behavior No native Android exception. The other foreground service keeps going on.

Runtime Environment (please complete the following information if relevant):

  • Device: Pixel 3a
  • OS: Android 12

Flutter SDK version

[✓] Flutter (Channel stable, 2.5.0, on macOS 11.4 20F71 darwin-x64, locale de-DE)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
[✓] Xcode - develop for iOS and macOS
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1)
[✓] Android Studio (version 2020.3)
[✓] IntelliJ IDEA Community Edition (version 2020.2.4)
[✓] VS Code (version 1.60.0)
[✓] Connected device (2 available)

Additional context This Exception only appeared with audio_service version 0.18.0. In version 0.17.0 this works fine.

Dix206 avatar Sep 13 '21 16:09 Dix206

Can you please provide the minimal reproduction project as requested?

ryanheise avatar Sep 13 '21 16:09 ryanheise

i have same problem my app not closed with another foreground task is running. using "flutter foreground task" with "audio service" causing this problem for me.

when main activity extends audio service activity and other foreground task is running audio service prevent app from closing completely .

mhorizon avatar Jun 21 '23 04:06 mhorizon