0

I created a music player, the music is played from a service and information like the song title and artist is displayed in a notification using startForeground(). This always worked fine, but recently I thought it would be nice to count down the number of songs and time left of the playlist and display that in the notification. When the service starts a thread is started that continuously updates this information:

private void updatePlaylistPosition() {
    if (updateThread == null) {
        Log.d(TAG, "Starting update thread");
        updateThread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    while (prepared) {
                        Log.d(TAG, "Updating playlist position");

                        // Set playlist position and time left.
                        String left = Util.formatDuration(
                                duration - player.getCurrentPosition());
                        if (songIndex < songs.size() - 1) {
                            left = (songs.size() - songIndex - 1) + " / " + left;
                        }
                        notificationViews.setTextViewText(R.id.tvPlaylistPosition, getString(
                                R.string.playlist_position, songIndex + 1, songs.size(), left));
                        startForeground(1, notification);

                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException ex) {
                        }
                    }

                    // Hide playlist position when playback completes.
                    notificationViews.setViewVisibility(R.id.tvPlaylistPosition, View.GONE);
                    startForeground(1, notification);
                } catch (Exception ex) {
                    Log.e(TAG, "Error in update thread", ex);
                } finally {
                    Log.d(TAG, "Update thread exiting");
                    updateThread = null;
                }
            }
        });
        updateThread.start();
    } else {
        Log.d(TAG, "Interrupting update thread");
        updateThread.interrupt();
    }
}

This is working, but after an hour or so, the notification is no longer updated, even though the music is still playing and when the song completes the next song is also played so the service is still running.

I managed to reproduce this in the emulator and found this in the logs:

07-04 10:20:37.338  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:37.861  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:38.391  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:38.914  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:39.437  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:39.962  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:40.488  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:41.011  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:41.070  1700  1712 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 124)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]: onNotificationPosted: Error receiving StatusBarNotification
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]: android.os.DeadObjectException: Transaction failed on small parcel; remote process probably died
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.os.BinderProxy.transactNative(Native Method)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.os.BinderProxy.transact(Binder.java:503)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.service.notification.IStatusBarNotificationHolder$Stub$Proxy.get(IStatusBarNotificationHolder.java:86)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.service.notification.NotificationListenerService$INotificationListenerWrapper.onNotificationPosted(NotificationListenerService.java:685)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.service.notification.INotificationListener$Stub.onTransact(INotificationListener.java:71)
07-04 10:20:41.070  1700  1712 W NotificationListenerService[]:     at android.os.Binder.execTransact(Binder.java:453)
07-04 10:20:41.537  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:41.550  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1039772)
07-04 10:20:42.050  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:42.063  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1039988)
07-04 10:20:42.564  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:42.577  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1040204)
07-04 10:20:43.077  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:43.090  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1040420)
07-04 10:20:43.590  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:43.604  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1040636)
07-04 10:20:44.104  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:44.120  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1040852)
07-04 10:20:44.620  2842  3149 D UPlayer : Updating playlist position
07-04 10:20:44.633  2842  3149 E JavaBinder: !!! FAILED BINDER TRANSACTION !!!  (parcel size = 1041068)

... and so on ...

"FAILED BINDER TRANSACTION" and lots of exclamation marks, it sounds serious but I have no idea what this means and I can't find anything about it that is related to Android, services and startForeground().

Could it have something to do with calling startForeground() from a thread? Anyone?

0ne_Up
  • 471
  • 3
  • 9
  • 24
  • Why you calling `startForeground(1, notification)` inside loop? ` – ADM Jul 04 '18 at 09:11
  • Because I want to continuously update the time left, which is displayed in the notification – 0ne_Up Jul 04 '18 at 09:14
  • Go through with this https://stackoverflow.com/questions/31131640/android-java-binder-failed-binder-transaction – Ankita Jul 04 '18 at 09:16
  • I also found that question, but I can't see how that is related, I'm not doing anything with images, except that my notification contains ImageButtons with previous/next and play/pause buttons. – 0ne_Up Jul 04 '18 at 09:19

0 Answers0