question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

The ongoing parameter of onNotificationPosted does not indicate to put the service in foreground

See original GitHub issue

EDIT:

I suspect that onNotificationPosted is not posted because the phone app’s notification during the call is in place and thus, Exoplayer cannot post its notfication. Thus, onNotificationPosted is not called. Could that be the reason? If yes, where else would be an appropriate place to make a call to startForeground and stopForeground if not in onNotificationPosted?

**** EDIT END ****

I am streaming mp3s using Exoplayer. I have an AudioPlayerService which is started via Util.startForegroundService. The service creates the player instance and registers the notfications like this:

        PlayerNotificationManager.NotificationListener notificationListener = new PlayerNotificationManager.NotificationListener() {
            @Override
            public void onNotificationCancelled(int notificationId, boolean dismissedByUser) {
                if (dismissedByUser) {
                    stopService();
                    sendBroadcast(new Intent(Constants.FINISH_ACTION));
                } else {
                    // It seems the system killed the service
                    AudioUtils.saveCurrentMediaState(player.getCurrentWindowIndex(), player.getCurrentPosition(), AudioPlayerService.this);
                }
            }

            @Override
            public void onNotificationPosted(int notificationId, Notification notification, boolean ongoing) {
                if (!ongoing) {
                    stopForeground(false);
                } else {
                    startForeground(notificationId, notification);
                }
            }
        };

Now, this is the problem: When the user starts an mp3 during a phone call, Util.startForegroundService is called as expected but onNotificationPosted is never called (I verified this in the debugger). As a result, stopForeground is not called which leads to the crash RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground().

Steps to reproduce:

  1. Start a phone call.
  2. Start an mp3 within a service using Util.startForegroundService.
  3. Crash.

Of course, the scenario is hypothetical because nobody will start an mp3 during a phone call. However, this could happen accidentally and the app is not supposed to crash.

Do I have to check in my app if there is a phone call going on and - if yes - not call startForegroundService? Or what am I supposed to do to catch this error?

Please note that wihtout an active phone call, everything works as expected.

Here is my Logcat containing the crash (during a phone call):

--------- beginning of main
09-23 16:59:38.535  7513  7513 D ViewRootImpl@c873f2d[PlayerActivity]: ViewPostIme key 0
09-23 16:59:38.597  7513  7513 D ViewRootImpl@c873f2d[PlayerActivity]: ViewPostIme key 1
09-23 16:59:38.625  7513  7717 V FA      : Recording user engagement, ms: 75624
09-23 16:59:38.631  7513  7513 D ViewRootImpl@c873f2d[PlayerActivity]: MSG_WINDOW_FOCUS_CHANGED 0
09-23 16:59:38.634  7513  7717 V FA      : Connecting to remote service
09-23 16:59:38.640  7513  7717 V FA      : Activity paused, time: 96545547
09-23 16:59:38.645  7513  7717 D FA      : Event not sent since app measurement is disabled
09-23 16:59:38.646  7513  7717 V FA      : Activity resumed, time: 96545570
09-23 16:59:38.661  7513  7717 D FA      : Event not sent since app measurement is disabled
09-23 16:59:38.662  7513  7717 V FA      : Connection attempt already in progress
09-23 16:59:38.680  7513  7513 W StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
09-23 16:59:38.697  7513  7513 I chatty  : uid=10325(u0_a325) identical 2 lines
09-23 16:59:38.705  7513  7513 W StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
09-23 16:59:38.739  7513  7513 W StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
09-23 16:59:38.771  7513  7513 I chatty  : uid=10325(u0_a325) identical 2 lines
09-23 16:59:38.771  7513  7513 W StaticLayout: maxLineHeight should not be -1.  maxLines:1 lineCount:1
09-23 16:59:38.794  7513  7513 V Surface : sf_framedrop debug : 0x4f4c, game : false, logging : 0
09-23 16:59:38.794  7513  7513 D ViewRootImpl@e1e5a9[AudioListActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x7 surface={valid=true 506447368192} changed=true
09-23 16:59:38.800  7513  7570 D mali_winsys: EGLint new_window_surface(egl_winsys_display *, void *, EGLSurface, EGLConfig, egl_winsys_surface **, egl_color_buffer_format *, EGLBoolean) returns 0x3000,  [1080x1920]-format:1
09-23 16:59:38.800  7513  7570 D OpenGLRenderer: eglCreateWindowSurface = 0x75f2c296e0
09-23 16:59:38.804  7513  7513 D ViewRootImpl@e1e5a9[AudioListActivity]: MSG_WINDOW_FOCUS_CHANGED 1
09-23 16:59:38.807  7513  7513 V InputMethodManager: Starting input: tba=android.view.inputmethod.EditorInfo@a658586 nm : de.widmer.offerings.activities ic=null
09-23 16:59:38.807  7513  7513 I InputMethodManager: startInputInner - mService.startInputOrWindowGainedFocus
09-23 16:59:38.839  7513  7717 D FA      : Connected to remote service
09-23 16:59:38.841  7513  7717 V FA      : Processing queued up service tasks: 2
09-23 16:59:38.848  7513  7570 D OpenGLRenderer: eglDestroySurface = 0x75dae6f550
09-23 16:59:38.854  7513  7513 D ViewRootImpl@c873f2d[PlayerActivity]: Relayout returned: old=[0,0][1080,1920] new=[0,0][1080,1920] result=0x5 surface={valid=false 0} changed=true
09-23 16:59:39.303  7513  7513 D ViewRootImpl@c873f2d[PlayerActivity]: dispatchDetachedFromWindow
09-23 16:59:39.303  7513  7513 D InputEventReceiver: channel 'c6a9fa7 de.widmer.offerings.activities/de.widmer.offerings.activities.PlayerActivity (client)' ~ Disposing input event receiver.
09-23 16:59:39.303  7513  7513 D InputEventReceiver: channel 'c6a9fa7 de.widmer.offerings.activities/de.widmer.offerings.activities.PlayerActivity (client)' ~NativeInputEventReceiver.
09-23 16:59:39.321  7513  7513 I ExoPlayerImpl: Release 4c5a129 [ExoPlayerLib/2.12.0] [herolte, SM-G930F, samsung, 26] [goog.exo.core, goog.exo.ui, goog.exo.mediasession, goog.exo.cronet]
09-23 16:59:39.323  7513  7617 I ACodec  : [OMX.google.mp3.decoder] signalFlush
09-23 16:59:39.325  7513  7719 D AudioTrack: stop() called with 0 frames delivered
09-23 16:59:39.325  7513  7617 I ACodec  : [OMX.google.mp3.decoder] ExecutingState flushing now (codec owns 4/4 input, 0/4 output).
09-23 16:59:39.325  7513  7719 I AudioTrack: updateAudioTranstionLength FadeIn[0] FadeOut[0] FadeInRing[0]
09-23 16:59:39.326  7513  7617 I ACodec  : [OMX.google.mp3.decoder] Now Flushing
09-23 16:59:39.326  7513  7617 I ACodec  : [OMX.google.mp3.decoder] FlushingState onOMXEvent(0,1,0)
09-23 16:59:39.326  7513  7617 I ACodec  : [OMX.google.mp3.decoder] FlushingState onOMXEvent(0,1,1)
09-23 16:59:39.327  7513  7617 I ACodec  : [OMX.google.mp3.decoder] Now Executing
09-23 16:59:39.329  7513  7617 I ACodec  : [OMX.google.mp3.decoder] Now Executing->Idle
09-23 16:59:39.334  7513  7617 I ACodec  : [OMX.google.mp3.decoder] Now Idle->Loaded
09-23 16:59:39.334  7513  7617 I ACodec  : [OMX.google.mp3.decoder] Now Loaded
09-23 16:59:39.335  7513  7617 I ACodec  :  [OMX.google.mp3.decoder] Now uninitialized
09-23 16:59:39.335  7513  7617 I ACodec  :  [] Now kWhatShutdownCompleted event : 8483
09-23 16:59:39.336  7513  7617 I MediaCodec: Codec shutdown complete
09-23 16:59:39.403  7513  7518 I zygote64: Do full code cache collection, code=503KB, data=338KB
09-23 16:59:39.405  7513  7518 I zygote64: After code cache collection, code=468KB, data=269KB
09-23 16:59:39.406  7513  7513 D AndroidRuntime: Shutting down VM
--------- beginning of crash
09-23 16:59:39.411  7513  7513 E AndroidRuntime: FATAL EXCEPTION: main
09-23 16:59:39.411  7513  7513 E AndroidRuntime: Process: de.widmer.offerings.activities, PID: 7513
09-23 16:59:39.411  7513  7513 E AndroidRuntime: android.app.RemoteServiceException: Context.startForegroundService() did not then call Service.startForeground()
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1881)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at android.os.Handler.dispatchMessage(Handler.java:105)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at android.os.Looper.loop(Looper.java:164)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at android.app.ActivityThread.main(ActivityThread.java:6944)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at java.lang.reflect.Method.invoke(Native Method)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
09-23 16:59:39.411  7513  7513 E AndroidRuntime: 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
09-23 17:00:06.123  7742  7742 W SELinux : SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:runas:s0 RAM:SEPF_SM-G930F_8.0.0_0018, [-1 -1 -1 -1 0 1]
09-23 17:00:06.126  7742  7742 I SELinux : SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=de.widmer.offerings.activities 
09-23 17:00:06.464  7758  7758 W SELinux : SELinux selinux_android_compute_policy_index : Policy Index[2],  Con:u:r:zygote:s0 RAM:SEPF_SM-G930F_8.0.0_0018, [-1 -1 -1 -1 0 1]
09-23 17:00:06.465  7758  7758 I SELinux : SELinux: seapp_context_lookup: seinfo=untrusted, level=s0:c512,c768, pkgname=de.widmer.offerings.activities 
09-23 17:00:06.467  7758  7758 I zygote64: Late-enabling -Xcheck:jni
09-23 17:00:06.515  7758  7758 D TimaKeyStoreProvider: TimaKeyStore is not enabled: cannot add TimaSignature Service and generateKeyPair Service
09-23 17:00:06.516  7758  7758 D ActivityThread: Added TimaKeyStore provider

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:18 (10 by maintainers)

github_iconTop GitHub Comments

1reaction
tosam144commented, Sep 24, 2020

Ok great, if your fix does the same thing, I won’t wait. Thank you so much for your prompt and excellent help, @marcbaechinger !

1reaction
tosam144commented, Sep 24, 2020

Yes, onNotificationPosted never gets called. That’s the problem. I have created a minimal Android project with which you can easily reproduce this behavior:

EPTest.zip

The steps are:

  1. Launch the app on the test device.
  2. Initiate a phone call to the test device.
  3. Set a breakpoint in onNotificationPosted.
  4. Press the “Button” to start the audio playing.

The breakpoint is never reached. If, however, you try without the phone call, it works.

Please also note that you must use a real device. It does not work on an Emulator. I am using a Samsung S7 with Android 8 for testing. Also, it has to be the phone app (not skype or so).

Let me know what you find. Thanks.

Read more comments on GitHub >

github_iconTop Results From Across the Web

android - How to cancel an ongoing notification of another app?
I've found an app that somehow hides heads-up notifications, including even ongoing notifications (used by foreground services), called NCleaner ...
Read more >
NotificationListenerService - Android Developers
A flag value indicating that this notification listener can see important ( > NotificationManager#IMPORTANCE_MIN ) ongoing type notifications.
Read more >
Can not Show notification on status bar - Google Groups
I have created a foreground service for an Application with UserHandle.USER_SYSTEM (user 0) under /product/priv-app/ folder on aosp 11 for ...
Read more >
Creating a never ending background service in Android
You use a background service to provide continuous data collection or processing while the app is no longer in the foreground (i.e. it...
Read more >
NotificationListenerService - Android Developers
A service that receives calls from the system when new notifications are ... hints constant - the primary device UI should disable notification...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found