21

I'm trying to write an app that (among other things) will change the user's ringtone based on their location.

However, I'm having difficulty setting the ringtone of my phone from within my app. I've been able to display a list of the phone's ringtones, and have been using the following code to try and set the ringtone:

RingtoneManager.setActualDefaultRingtoneUri(applicationContext, 
      RingtoneManager.TYPE_RINGTONE,
      MediaStore.Audio.Media.getContentUriForPath(settings.getRingtoneURI()));

Settings.System.putString(c.getContentResolver(), Settings.System.RINGTONE, 
      settings.getRingtoneURI());

where settings.getRingtoneURI() returns a string with the URI of the desired ringtone.

When I run this, I receive no errors but the ringtone does not change.

Any advice?

dckrooney
  • 3,041
  • 3
  • 22
  • 28
  • Duplicate of http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – John Leehey Jun 01 '11 at 21:06
  • 1
    I actually saw that question prior to posting my own. However, I'm not trying to apply a ringtone by creating a URI from an audio source. I want to use one of the phone's preexisting ringtones. I'm able to display a dialog which lists the available ringtones, but for some reason the above code does not set the default ringtone to the user's selection. – dckrooney Jun 04 '11 at 00:11
  • please refer the following question http://stackoverflow.com/questions/1271777/how-to-set-ringtone-in-android-from-my-activity – dharmendra Jul 12 '11 at 12:26
  • @dhams: That's the same question that John pointed out. – dckrooney Jul 19 '11 at 20:17

1 Answers1

2

The below code choose any random tone from the mobile for Incoming call.

            RingtoneManager rm = new RingtoneManager(context);
    Random random = new Random();

    int i = rm.getRingtonePosition(RingtoneManager
            .getActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE));
    MyApplication.APPLICATION_SHARED_PREFERENCE.edit()
            .putInt(MyConstants.PHONE_RINGTONE_NUMBER, i).commit();
    int chanegToneNumber;
    Cursor cursor = rm.getCursor();

    while (true) {
        chanegToneNumber = random.nextInt(cursor.getCount());
        if (chanegToneNumber != i)
            break;
    }

    Log.d(TAG, "Tone: " + i);

    Log.d(TAG, "Tone total: " + cursor.getCount());

    while (cursor.moveToNext()) {

        if (i == cursor.getInt(cursor
                .getColumnIndex(MediaStore.MediaColumns._ID))) {
            RingtoneManager.setActualDefaultRingtoneUri(context,
                    RingtoneManager.TYPE_RINGTONE,
                    rm.getRingtoneUri(chanegToneNumber));
            break;
        }
    }
Hafiz Waleed Hussain
  • 1,062
  • 12
  • 25
  • MyApplication.APPLICATION_SHARED_PREFERENCE.edit() .putInt(MyConstants.PHONE_RINGTONE_NUMBER, i).commit(); Please tell me the importance of this line ? – Sritam Jagadev Apr 15 '15 at 11:43
  • 1
    Sritam Jagadev. Basically I am using this tone number in one more place. That's why I am saving in file. But here for you that may be a redundant. Thanks for a good question. – Hafiz Waleed Hussain Apr 17 '15 at 00:36