Change Phone Ringtone while ringing
<p>I know that Android has a built-in system that lets the user choose a special ringtone for a single phonebook contact/group, but i have to change the ringtone dinamically while calling:</p>

<p>So, I wrote a Service and load it at phone startup, to set a phone listener that captures incoming call, that saves the default phone ringtone (in a Service variable) and change the original ringtone with another one if necessary, then restore the orignal ringtone when the phone state returns idle. Well, i can save the original tone, set the new one, but can't hear the new ringtone, only the old:</p>

<p>Take a look at my code and keep in mind that, when the phone listener chooses if to change the original ringtone or not, the phone is <strong>already ringing.</strong></p>

<p>So, how can i change the ringtone while the phone is already ringing?</p>

<p>I have <code>android.permission.WRITE_SETTINGS</code> and <code>android.permission.READ_PHONE_STATE</code> permissions in my Manifest.</p>

<p>The Service:</p>

<pre><code>public class CallFilterService extends Service {

    private Uri originalRingtoneUri;

    @Override
    public IBinder onBind(Intent arg0) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        telephonyManager.listen(new CallFilterListener(this), PhoneStateListener.LISTEN_CALL_STATE);
        return START_STICKY;
    }

    public void saveOriginalRingtone() {
        originalRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE);
    }

    public void restoreOriginalRingtone() {
        RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, originalRingtoneUri);
    }
}
</code></pre>

<p>The PhoneListener:</p>

<pre><code>public class MyPhoneListener extends PhoneListener
{
    private CallFilterService service;
    private Uri specialRingtoneUri;

    public CallFilterListener(CallFilterService service) {
        super();
        this.service = service;
        specialRingtoneUri = setSpecialRingtone();       
    }

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch (state) {
            case TelephonyManager.CALL_STATE_RINGING: {
                service.saveOriginalRingtone();
                if (canChangeRingtone(incomingNumber)) {
                    //When in this if block, the default phone ringtone is already playing.
                    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, specialRingtoneUri);
                }
                break;
            }
            case TelephonyManager.CALL_STATE_IDLE: 
            {
                service.restoreOriginalRingtone();
                break;
            }
            default:
                break;
        }
    }
}
</code></pre>
