Search code examples
androiduripickerringtone

Android Default tone picker issue (default tone with notifications and alarm)


I don't know if it is a stupid question with easy solution. I have a ringtone picker that shows the default ringtone option (for Notifications). Like this:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
startActivityForResult(intent, RingtoneManager.TYPE_NOTIFICATION);

And then I catch the result:

protected void onActivityResult(int requestCode, int resultCode, Intent mRingtone) {
    switch (requestCode) {
        case RingtoneManager.TYPE_NOTIFICATION:
            if (resultCode == RESULT_OK) {
                notifToneUri = mRingtone.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
                Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
                Log.d(TAG,"Uri = " + notifToneUri.toString() + " and title = " + ringtone.getTitle(this));
            }
        break;
    } 
}

An weird thing happens, if I select the option "Default notification tone", the uri should be: *content://settings/system/notification_sound* but the uri is content://settings/system/ringtone (which is the default value of the ringtone, not the notification). It happens the same with the Default alarm tone.

I solved like this:

if (notifToneUri.equals(Settings.System.DEFAULT_RINGTONE_URI)){
    notifToneUri = Settings.System.DEFAULT_NOTIFICATION_URI;
}

Ok this works... But I would like to know if there is a better way or there is something I am doing wrong.

Thank you in advance.


Solution

  • I don't think you're retrieving the Uri correctly. Here's an example to follow:

    Launch the RingtoneManager

    final Intent ringtone = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    ringtone.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
                RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
    startActivityForResult(ringtone, 0);
    

    Retrieve the Uri and title in Activity.onActivityResult

    if (requestCode == 0 && resultCode == RESULT_OK) {
        final Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        final Ringtone ringtone = RingtoneManager.getRingtone(this, uri);
        // Get your title here `ringtone.getTitle(this)`
    }
    

    You can see here in the source for Ringtone, that the prefix is only added when the authhority of the Uri is equal to Settings.AUTHORITY and never MediaStore.AUTHORITY.