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?