Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the ringtone in android programmatically?

Tags:

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?

like image 554
dckrooney Avatar asked May 31 '11 04:05

dckrooney


1 Answers

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;
        }
    }
like image 179
Hafiz Waleed Hussain Avatar answered Oct 12 '22 05:10

Hafiz Waleed Hussain