2

I'm using AlarmManager to display alarms in my android app, I want to display a sound from the system available sounds as an alarm but the only availability for me is to choose between the already set sounds like ringtone, alarm, notification :

Uri alert = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); // OR TYPE_RINGTONE OR TYPE_NOTIFICATION
Ringtone r = RingtoneManager.getRingtone(mContext, alert);

but I want to get all the system available tones and choose a one between them.

Muhammed Refaat
  • 8,914
  • 14
  • 83
  • 118
  • You can use Ringtone Picker here is an example : http://stackoverflow.com/questions/12393016/ringtone-picker-radio-button-set – Ronish Jan 07 '16 at 18:10
  • @Ronish great, I never knew about RingtonePicker, that totally solved my problem, you can add a complete answer for it – Muhammed Refaat Jan 07 '16 at 19:11

1 Answers1

3

This worked for me :

Uri ringtone= RingtoneManager.getActualDefaultRingtoneUri(YourActivity.this, RingtoneManager.TYPE_ALARM);


And as stated by this answer, you have to do the following:

Intent intent=new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, ringtone);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, ringtone);
startActivityForResult(intent , 1);

And finally you get the selected tone "uri" which you can store in ringtone as shown below in onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        switch (requestCode) {
        case 1:
            ringtone = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
            break;    
        default:
            break;
        }
    }
}
Community
  • 1
  • 1
Ronish
  • 404
  • 3
  • 10
  • you have to always give the credit to the one that solved a similar problem before :) that's why I included the link into your answer – Muhammed Refaat Jan 14 '16 at 15:23