1
public void runSound(){

    Intent it = new Intent(Values.TAG_EXECUTE_ALARM);
    PendingIntent pendIntent = PendingIntent.getBroadcast(this, 0, it, 0);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND,0);

    AlarmManager alarm = (AlarmManager) getSystemService(ALARM_SERVICE);
    long time = calendar.getTimeInMillis();
    alarm.set(AlarmManager.RTC_WAKEUP,time,pendIntent);
}

This method is called from a service. My service is supposed to play a sound to notify the user even if the device is on sleep mode.

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    MediaPlayer player = MediaPlayer.create(arg0, R.raw.order_alarm);
    player.setAudioStreamType(AudioManager.STREAM_ALARM);

    player.start();

}

This is the class that will actually play the sound. Unfortunately the devices doesn't wake up and the sound is not played. When runSound is called before the device enters in sleep mode the sound works perfecly... Any ideas?

Edit:

-> Removed static references (yes, they were dumb, since runSound is inside a service).

-> Yes, the tag value is defined in receiver intent-filter

-> Improved the question.

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
Rafael Ito
  • 37
  • 5
  • Have you registered your AlarmReceiver to listen for `Values.TAG_EXECUTE_ALARM` IntentFilters? – Sam Dec 06 '12 at 19:44
  • Understand that `Calendar calendar = Calendar.getInstance();` is already set to the current time so: `calendar.setTimeInMillis(System.currentTimeMillis());` and `calendar.add(Calendar.SECOND,0);` do nothing. – Sam Dec 06 '12 at 19:50
  • What is the following... `MainMenuActivity.mainActivityContext` ? If `MainMenuActivity` is a class which extends `Activity` then I'm assuming `mainActivityContext` is a `public static` reference to the `Activity` `Context`. If that's the case you will have serious problems. – Squonk Dec 06 '12 at 20:09
  • Edited considering your advices. – Rafael Ito Dec 07 '12 at 15:44
  • @Rafaelito were you able to resolve this? I'm facing a similar issue – Santiago Jan 29 '13 at 14:50

1 Answers1

0

Well, I cant test now, but looking at your code I can see some problems.

At getBroadcast(), the last parameter has to be a flag, like "FLAG_CANCEL_CURRENT", and I am not sure if 0 is a valid flag.

And at "long time = calendar.getTimeInMillis();" it looks like you are putting a time in the past, well if it is in the past, it will not run, try add 5000 (5s).

All others things looks fine to me.

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
  • It was running, the problem is that the device doesn't wake up =( – Rafael Ito Dec 07 '12 at 15:43
  • Sorry for delay, well it usually does not need anything else to wake up... I am not sure, but as you are trying some thing heavy (play sound), try to use a wake lock, acquiring at start of the receiver and realeasing after done.... (it will require a WAKE_LOCK permission at mainifest as well). – Pozzo Apps Dec 11 '12 at 10:56