Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. COI actually am returning START_STICKY during onStartCommand already. As far as onCreate, I do initiate the player, but I can't call prepare yet since the service is started even before music is requested to be played and therefore I don't know the path to which song should I pass. Also, I do add a status bar notification when a song is playing, I am not sure if you peaked at the gist I posted. I will however look into acquiring a wakelock which I didn't think I needed since it did play music in bg for like 30 min periods while I was not using the phone.
      singulars
    2. COIn the 'gist' link, I didn't see any 'onStart' or 'onStartCommand' implementation, that why I asked :) If you are calling 'startForeground' in the service's onCreate, the partial wake lock is only useful when you want to play even when the device is asleep. The 'startForeground' should've prevented the killing of the service when the device is not asleep. Did you check the implementation of your onReceive method(s) in your Service's broadcast-receivers? They must run quickly; if not, your process may be killed.
      singulars
    3. COHi, you are right, I did not include the onStartCommand, I added now as I updated the gist. Also, I call startForeground only when leaving the activity (i.e, when going to home, or another activity), and call stopForeground(true) when resuming. As far as the onReceive, not sure how i can test to see if it runs quickly or not. I added a few bits of the onReceive to the gist
      singulars
 

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