androidandroid-4.1-jelly-bean

Are there known issues with Android Services on 4.1.x (Jelly Bean)?


My question is a shot in the dark: what is the deal with Android 4.x / Jelly Bean? Are there known problems with services, stickiness, foreground, etc.?

Backstory: I tested my music player application successfully on all sorts of Android devices and emulators and then just recently on a physical Jelly Bean device (Samsung Rugby Pro). I found that the MediaPlayer onCompletion function is not being fired consistently when the screen is turned off. Most often it isn't fired for several minutes. When the screen is on, whether or not my activity is shown, the application works just fine.

(There are no problems on Gingerbread, KitKat, Lollipop, or Marshmallow. I have physical devices for those versions and they all work flawlessly.)

Device Information:


Solution

  • I figured this out; I hope this helps you too ...

    The problem is not in firing the onCompletion event but rather that reset() blocks indefinitely. I had previously attributed this to Android issue #959: MediaPlayer "crash" (deadlocks the calling thread) when resetting or releasing an unused MediaPlayer which was incorrect for my case.

    My particular problem was aggressive sleep behavior on my test device. This bug would pop up when I ran in Airplane Mode with few other apps because the call to reset() (or release()) also releases the MediaPlayer instance's WakeLock. Once that occurred the device would sleep and the code would appear to block in that function. I was unable to reproduce this with a debugger attached and/or plugged in because either of those prevented the device from sleeping!

    My solution was to create a separately managed WakeLock which I acquire() basically when playback begins and release() after all songs have played through. Technically, I do this only when the activity has been paused and also release on resume because it's unnecessary to hold a WakeLock when the device is awake already.

    PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
    WakeLock wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyTag");
    wakeLock.acquire();