AlarmManager does not play selected tone

Viewed 36

I have an Android app in which I want to create an alarm. It's a normal alarm that should wake you up at a specified time. As with any alarm, you can also specify which alarm sound it will use, by selecting a sound file when creating the alarm.

The alarm works well, it starts at the correct time, but it always plays only phone's default tone. Not the one I selected when creating the alarm.

Ringtone selection:

ringtoneURI = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);

Database storing:

SQLiteDatabase database=getWritableDatabase();
ContentValues contentValues=new ContentValues();
...
contentValues.put(DBFields.ALARM_TONE, ringtoneURI.toString());
...
database.insert(DBFields.TABLE_OF_ALARMS,null,contentValues);

Alarm creation:

Intent intent = new Intent(AddAlarm.this, AlarmReceiver.class);
intent.putExtra("AlarmTone", cursor.getString(cursor.getColumnIndex(DBFields.ALARM_TONE)));
intent.putExtra("AlarmId", cursor.getInt(cursor.getColumnIndex(DBFields.ALARM_ID)));
intent.putExtra("AlarmTime", cursor.getString(cursor.getColumnIndex(DBFields.ALARM_TIME)));
pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
        100,
        intent,
        FLAG_UPDATE_CURRENT);

alarmManager.setExact(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);

AlarmReceiver:

NotificationCompat.Builder builder = new 
NotificationCompat.Builder(context, CHANNEL_ID);
Uri soundUri = Uri.parse(intent.getStringExtra("AlarmTone"));
builder.setSound(soundUri);

I have stepped through the code with debugger and the correct AlarmName is selected and is part of the Intent. Therefore I assume that the correct name is being sent to the AlarmManager and I have no idea where the problem could be.

How can I make the alarm play the correct tone?

1 Answers

I found this in Geeks for Geeks site:

Use this and tweak to your database data:

Calendar calendar = Calendar.getInstance();
  
            // calendar is called to get current time in hour and minute
            calendar.set(Calendar.HOUR_OF_DAY, YourHour);
            calendar.set(Calendar.MINUTE, YourMinute());
  
            // using intent class AlarmReceiver which inherits
            // BroadcastReceiver
            Intent intent = new Intent(this, AlarmReceiver.class);
  
            // we call broadcast using pendingIntent
            pendingIntent = PendingIntent.getBroadcast(this, 0, intent, 0);
  
            time = yourTimeToWait

            // Alarm rings continuously until toggle button is turned off
            alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, time, 10000, pendingIntent);
             alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (time * 1000), pendingIntent);

Then create a class called AlarmReciever.java.

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Vibrator;
import android.widget.Toast;

import androidx.annotation.RequiresApi;

public class AlarmReceiver extends BroadcastReceiver {
    @RequiresApi(api = Build.VERSION_CODES.Q)
    @Override
    // implement onReceive() method
    public void onReceive(Context context, Intent intent) {

        Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.YOUR_TYPE);
        if (alarmUri == null) {
            alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.YOUR_TYPE);
        }
        
        // setting default ringtone
        Ringtone ringtone = RingtoneManager.getRingtone(context, alarmUri);

        // play ringtone
        ringtone.play();
    }
}

In short, what is happening is your timer Calender and alarmManager instances are calling to the AlarmReciever.java which is a BroadcastReciever. Then the AlamrReciever class handles actually playing timer sounds.

Related