Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging service crashes

I have a music service that takes care of music playback in the background of course, and also a few other things such as notifying activities of UI changes that need to be made, storing and restoring queues, obtaining song records via a cursor, and every time the song changes it takes care of updating the bitmap on the ongoing notification.

What is the problem? The service crashes almost all the time the user tries to do..well pretty much anything else. For instance, playing a song, and then opening a game crashes the service, sometimes light things like browsing the web on chrome will crash it too.

A few details:

1 - The service is started via startService(new Intent(this, MyMusicService.class)); but when user leaves activity I call startForeground(mId, mNotification); to show an ongoing notification that provides easy controls, and also because startForeground() is intented for services that should be prioritized as the killing of these by the activityManager would be disruptive for the user experience (music is stopped if service is killed)

2 - I have attempted to reproduce the crash, and it certainly happens when doing not so heavy work, like browsing on chrome (this would reproduce the crash almost 50% of the time), and always when doing things like launching temple run 2. FYI, if not doing anything at all - just playing music on the background and idle, playback will not stop at all, and it is works just the way it's intended to.

3 - The phone I am using as test device is not by any means a slow or low-memory device, it is a Galaxy Nexus.

4 - I've tried catching a few longs and sometimes it simply says:

02-24 06:53:32.586: I/ActivityManager(387): Process com.deadpixels.light.player (pid 27720) has died.

without any other messages, and sometimes the same message is accompanied by a WINDEATH:

 02-24 06:53:32.586: W/ActivityManager(387): Scheduling restart of crashed service com.deadpixels.light.player/.service.MyMusicService in 5000ms
 02-24 06:53:32.602: W/InputDispatcher(387): channel '418d09c8    com.deadpixels.light.player/com.deadpixels.light.player.HomeActivity (server)' ~ Consumer closed input channel or an error occurred.  events=0x9
 02-24 06:53:32.602: E/InputDispatcher(387): channel '418d09c8 com.deadpixels.light.player/com.deadpixels.light.player.HomeActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
 02-24 06:53:32.602: W/InputDispatcher(387): Attempted to unregister already unregistered input channel '418d09c8 com.deadpixels.light.player/com.deadpixels.light.player.HomeActivity (server)'
 02-24 06:53:32.602: I/WindowState(387): WIN DEATH: Window{418d09c8 u0 com.deadpixels.light.player/com.deadpixels.light.player.HomeActivity}

What I've also noticed is that almost always when the crash happens, a lot of other services "die". I am attaching the full log here.

5 - I tried using ACRA to get more information about the crash but even though everything seems to be setup correctly, after crashing the log is not being sent, or just nothing happens. I know ACRA detects the crash and starts:

02-24 06:53:37.719: D/ACRA(28293): ACRA is enabled for com.deadpixels.light.player, intializing...
02-24 06:53:37.742: D/ACRA(28293): Using default Mail Report Fields
02-24 06:53:37.742: D/ACRA(28293): Looking for error files in /data/data/com.deadpixels.light.player/files
02-24 06:53:37.742: W/ACRA(28293): com.deadpixels.light.player reports will be sent by email (if accepted by user).

but nothing is being sent, neither to the mailto I set, or to the form in Google docs.

Not sure what else to try, and I was hoping to get some good ideas on what to look for. As I said, the service does do a few things, that to my knowledge are not that resource-extensive, and to be honest, I am not even sure if it is anything to do with the service that's crashing.

Here's a gist of several code bits that take place in the service, and also the ACRA code I set up. It's also worth noting that I tried to reproduce the service crashing with other music players and I did not run into the issue at all.

Thanks for the help!

like image 620
daniel_c05 Avatar asked Feb 24 '13 13:02

daniel_c05


1 Answers

It seems that the OS thinks your service is in the background as soon as the user leaves your activity, despite it being a 'foreground' service.

I would design your MyMusicService service a bit different though:

  1. In onCreate prepare the MediaPlayer.
  2. In the onStart or onStartCommand implementation of your service, start playing the selected/appropriate sound file. Acquire a partial wake_lock if you want music to keep playing in the background, even if device is asleep. Return START_STICKY.
  3. Add notification to status bar. 3 In onDestroy, clean everything up.

When the music stops, either by user interaction or when the song ends, be sure to remove notification and release wake lock.

A nice tutorial can be found here. http://www.youtube.com/watch?v=mcb99u8Nlgs&list=PL14AA2548E3C96B50

Update after OP added comments/updated 'gist':

I see that the onReceive of your BroadcastReceiver mReceiver in your service accesses a database. This could be the issue. Off-load this to an IntentService: In the onReceive, just start an IntentService using the same Intent that the onReceive received. Then the IntentService can do all the actual work and not worry about any possible slow database access causing the killing of the process.

If playing music still needs to work when the device is asleep, be sure to acquire a partial wake lock (and be sure to release it when necessary!).

like image 67
Streets Of Boston Avatar answered Oct 11 '22 23:10

Streets Of Boston