Search code examples
androidalarmmanagerringtone

Why is alarm being called but not ringing?


This is me calling the alarmmanager,

     Intent intent = new Intent(getBaseContext(), AlarmReceiver.class);
    pendingIntent = PendingIntent.getBroadcast(getBaseContext(), RQS_1, intent, 0);
    alarmManager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

and this is my receiver:

public class AlarmReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context arg0, Intent arg1) {
    Toast.makeText(arg0, "Alarm received!", Toast.LENGTH_LONG).show();
    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    Ringtone ringtone = RingtoneManager.getRingtone(arg0, uri);
    ringtone.play();
    if (ringtone.isPlaying()) {
        Log.e("Yep","Its playin");
    }else {
        Log.e("NOp","just nop");
    }

when the receiver is called, i always get Yep,Its playing but the ringtone never rings... its not the volume i am sure of it, so what else could be the problem? Thank you in advance


Solution

  • Try using MediaPlayer instead:

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager. TYPE_ALARM);
    try{
        MediaPlayer mp = MediaPlayer.create(arg0, uri);
        mp.start();
    } catch(Exception e){}
    

    Also try using RingtoneManager.TYPE_NOTIFICATION instead of RingtoneManager.TYPE_ALARM

    Are you working on the emulator? Because it does not have a ringtone by default. If you are, you can use your code as it is (because it should work on actual device, but can fail if no ringtone is et e.g. silent mode.) by doing the following:

    Upload a MP3 file in your /sdcard folder using DDMS, restart the emulator, then open the Media application, browse to your MP3 file, long press on it and select "Use as phone ringtone".