Alarm sound - doesn't always play
<p>I am in the process of writing an alarm program. Most of it appears to work fine. I can set a time for the alarm to go off. When this time comes around my broadcast receiver initiates an activity which vibrates, makes a sound and displays a button to allow the user to stop the sound. When I run the program I can set the alarm to ring in a minute or two. I can then quit the alarm program and go and run something else. Sure enough when the alarm time comes, I get interrupted by the alarm activity, the phone vibrates, the alarm sounds and I see the button. I hear the sound even if the media volume is turned right down. </p>

<p>The problem is that if the phone is sleeping, then at the alarm time the phone will vibrate, but I hear no sound. When I wake the phone up I see the button on screen correctly.</p>

<p>I have looked at a large number of different code snippets for how to make the alarm sound. They all seem inordinately complicated and surprisingly different from each other. I don't fully understand any of them. What I have used is a conglomerate of several snippets shown below:</p>

<pre><code>alarm_sound = new MediaPlayer();

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
if(alert == null){
    // alert is null, using backup
    alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    if(alert == null){  // I can't see this ever being null (as always have a default notification) but just incase
        // alert backup is null, using 2nd backup
        alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);               
    }
}

 try {
        alarm_sound.setDataSource(this, alert);
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalStateException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
 final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
 if (audioManager.getStreamVolume(AudioManager.STREAM_ALARM) != 0) 
 {

                alarm_sound.setAudioStreamType(AudioManager.STREAM_ALARM);
                alarm_sound.setLooping(true);
                try {
                    alarm_sound.prepare();
                } catch (IllegalStateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                alarm_sound.start();
  }
</code></pre>

<p>Can anyone tell me how to correct my code so that it rings properly... or perhaps give me a web link to a site which explains it <em>simply</em>.</p>

<p><strong>EDIT:</strong> I thought that perhaps the problem could be resolved by waking up the phone first. To do this I added the following code before any of the alarm of vibrate stuff:</p>

<pre><code>PowerManager pm = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock = pm.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock.acquire();

PowerManager pm2 = (PowerManager) getApplicationContext().getSystemService(Context.POWER_SERVICE);
WakeLock wakeLock2 = pm2.newWakeLock((PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP), "TAG");
wakeLock2.acquire();
</code></pre>

<p>I also added the following to my manifest:</p>

<pre><code>&lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;
&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
</code></pre>

<p>This code appeared to only half work - the phone did wake up in as much as the screen became lit up, but the lock was still on, i.e. I had to perform the slide-to-unlock operation myself. And just to add insult to injury, the alarm still didn't make a sound :-(</p>

<p><strong>EDIT:</strong>: I just added <code>getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);</code> 
for my main window. This bypassed the lock problem - but still no sound.</p>
