Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>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.</p> <p>I would design your <code>MyMusicService</code> service a bit different though:</p> <ol> <li>In <code>onCreate</code> prepare the <code>MediaPlayer</code>. </li> <li>In the <code>onStart</code> or <code>onStartCommand</code> 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 <code>START_STICKY</code>.</li> <li>Add notification to status bar. 3 In <code>onDestroy</code>, clean everything up.</li> </ol> <p>When the music stops, either by user interaction or when the song ends, be sure to remove notification and release wake lock.</p> <p>A nice tutorial can be found here. <a href="http://www.youtube.com/watch?v=mcb99u8Nlgs&amp;list=PL14AA2548E3C96B50" rel="nofollow">http://www.youtube.com/watch?v=mcb99u8Nlgs&amp;list=PL14AA2548E3C96B50</a></p> <p><strong>Update after OP added comments/updated 'gist':</strong></p> <p>I see that the <code>onReceive</code> of your <code>BroadcastReceiver</code> <code>mReceiver</code> in your service accesses a database. This could be the issue. Off-load this to an <code>IntentService</code>: In the <code>onReceive</code>, just start an <code>IntentService</code> using the same <code>Intent</code> that the <code>onReceive</code> received. Then the <code>IntentService</code> can do all the actual work and not worry about any possible slow database access causing the killing of the process.</p> <p>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!).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload