androidringtoneringtonemanager

ringtone picker: check 'none' or 'default notification sound' when they're picked


I want the user to be able to select a notification ringtone for my app. I have a custom sound file (an .mp3 file placed in /raw) that I want to be the default notification ringtone. I save/get the chosen ringtones uris in/from the Shared Preferences this way:

public static void setNotificationSound(Activity activity, String uri) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(activity);
        sharedPref.edit().putString("notificationSound", uri).apply();
    }


    public static Uri getNotificationSound(Context context) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        return Uri.parse(sharedPref.getString("notificationSound", ""));
    }

And this is how I show the ringtone picker:

    Uri notificationSoundUri = SharedPrefMethods.getNotificationSound(mACA); //gets the saved uri 

    Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Notification Ringtone");
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Uri.parse("android.resource://" + mACA.getPackageName() + "/raw/notificationbrightside")); //my custom sound
    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, notificationSoundUri); //pass the saved URI to be 'checked'
    this.startActivityForResult(intent, 555);

My issue seems to be with the EXTRA_RINGTONE_EXISTING_URI line. When the user picks a system ringtone and comes back to the picker, the ringtone is correctly checked. But when the user picks 'none' or 'default notification sound' (my custom sound) and comes back to the picker, nothing's checked.

Everything else in my code (not shown here) is ok - when I pick any ringtone (including the custom one), the sound is correctly assigned to my notifications. When I pick 'none', no sound is played. That means the correct URIs are being saved to SharedPref.

What do I have to pass to the Intent so that 'none' or 'default notification sound' are checked when they're picked?


Solution

  • Pass null when 'none' is selected.

    Pass Settings.System.DEFAULT_NOTIFICATION_URI when 'default notification sound' is selected in RingtoneManager.EXTRA_RINGTONE_EXISTING_URI field.