Note that there are some explanatory texts on larger screens.

plurals
  1. POPlay sound while phone is asleep
    text
    copied!<p>I am wirting a service which reminds the user in random intervalls. This service is started by a Activity and runs in the background as a service. </p> <p>The challenge I am facing is, when the user puts the phone into sleep (blank screen), in some occasions the sound simply does not play. Time has run out, but the sound won't play either on time or it plays when the user wakes up the phone. </p> <p>Here is some code /** * Starts a new thread which is checking every seconds how much time has * elapsed and if the point of time has come to play the sound. * Once the time has come, it gets the next ring time and continues * until it is shut down by the user */ private void runCheck() { Log.i(tag, "starting runCheck");</p> <pre><code> Thread thread = new Thread() { public void run() { Log.i(tag, "starting LogoTimerThread"); while (vRunningFlag) { try { // update the notification makeNotification(); Log.v(tag, "Next Ring in : [" + helper.TimeCalculator .duration2_hh_MM_SS((vTimerNextRing - System .currentTimeMillis()) / 1000) + " sec.]"); // check if time has run out if (vTimerNextRing &lt; System.currentTimeMillis()) { // reset the timer setNextRingTime(); // update the screen makeNotification(); // play the sound playLTimerSound(); } sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } Log.i(tag, "finished LogoTimerThread"); } }; thread.start(); } </code></pre> <p>The whole service is running as remote service which is set to foreground, so a notification reminds the user of the service and he can stop it that way.</p> <p>Once I commentet out the playLTimerSound(), the timer would just count down. Somehow the thread gets stopped by playing of the sound.</p> <p>Here ist that function as well:</p> <pre><code>public void playLTimerSound() { Log.i(tag, "playLogoTimerSound - volume:" + vVolume); MediaPlayer mp = MediaPlayer.create(this, R.raw.enterprise); mp.setVolume(2.0f, 2.0f); mp.start(); mp.setOnCompletionListener(new OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { // TODO Auto-generated method stub mp.release(); } }); } </code></pre> <p>Sorry for the formatting here, you might have some tips on this as well ;-)</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