How to set ringtone with RingtoneManager.ACTION_RINGTONE_PICKER?
Asked Answered
R

4

20

I try to find solution here, but there are only solution for own/selected file, not for code when I call picker. I use following code when user press button:

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
ActivityCurrent.this.startActivityForResult(intent,999);

This show ringtone picker, user can choose what ringtone wants, but I miss two things: - it doesn´t show current ringtone when it open - it not save ringtone when it is clicked on OK


I still can´t find way how to open RINGTONE_PICKER with already selected current ring tone. Any idea?

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select ringtone for notifications:");
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
ActivityCurrent.this.startActivityForResult(intent,999);
Rimini answered 6/10, 2011 at 8:4 Comment(0)
I
21

You must implement onActivityResult() to receive result from user's pick, then save it.

if (resultCode == RESULT_OK) {
    Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
    if (uri != null) {
    String ringTonePath = uri.toString();
}

Here an example: http://www.ceveni.com/2009/07/ringtone-picker-in-android-with-intent.html

EDIT: update

RingtoneManager.setActualDefaultRingtoneUri(
    myActivity,
    RingtoneManager.TYPE_RINGTONE,
    uri);

You must call this :)

Im answered 6/10, 2011 at 8:18 Comment(2)
Thank you, but this is only how to catch path. But I don´t see way: - how to save this settings? - how to show current ringtone when it is open?Rimini
Dear Xjaphx : ) Thank you very much, you saved me : ) This code is working as I needed.Rimini
S
13
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);

"ringtone" is the uri in which I am saving the picked tone 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);

            // Toast.makeText(getBaseContext(),RingtoneManager.URI_COLUMN_INDEX,
            // Toast.LENGTH_SHORT).show();
            break;

        default:
            break;
        }
    }
}

Hope it helps you. Hit answered if it does.

Stonework answered 15/8, 2012 at 6:44 Comment(2)
Thanks you It's working But if we added our raw file uri in RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI that time selection for default sound is not getting selected after reopen ringtone picker.Despicable
can you please tell meDespicable
C
4

The code is perfect and works for me. But you forgot to mention the permissions required..here it is. try this code..hope it helps

<uses-permission android:name="android.permission.WRITE_SETTINGS" ></uses-permission>
  <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" ></uses-permission>
  <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" ></uses-permission>
Charlinecharlock answered 13/6, 2013 at 14:31 Comment(2)
On rooted device, you only need android.permission.WRITE_SETTINGS permission. Don't know about non-rooted devices, though.Homochromous
Oh! I didnt know that!! Thx for the info..Mine is unrooted..So I had to put all three to get it workCharlinecharlock
W
4

This code will show default ringtone which the user sets earlier when ringtone picker is used.

Use below code in the button for ringtone intent.

public void pickRingtone(View view) {
        // TODO Auto-generated method.   stub

        Intent intent = new.       Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,
                RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, "Select Ringtone");

    // for existing ringtone
        Uri urie =     RingtoneManager.getActualDefaultRingtoneUri(
                getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
        intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, urie);

    startActivityForResult(intent, 5);
}
Warthog answered 11/4, 2014 at 8:43 Comment(1)
adding this line to code helped me intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(strUri)); this line pass the existing uri to selector `Woodworth

© 2022 - 2024 — McMap. All rights reserved.