Android set custom ringtone for each SIM

1.6k Views Asked by At

I am trying to set custom ringtone using below code:

private void registerRingtone(String ringtoneFilePath) {

    // Create File object for the specified ring tone path
    File ringtoneFile = new File(ringtoneFilePath);

    // Insert the ringtone to the content provider
    ContentValues value = new ContentValues();
    value.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
    value.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName());
    value.put(MediaStore.MediaColumns.SIZE, ringtoneFile.length());
    value.put(MediaStore.MediaColumns.MIME_TYPE, "audio/*");
    value.put(MediaStore.Audio.Media.ARTIST, "myapp");
    value.put(MediaStore.Audio.Media.IS_ALARM, false);
    value.put(MediaStore.Audio.Media.IS_MUSIC, false);
    value.put(MediaStore.Audio.Media.IS_NOTIFICATION, false);
    value.put(MediaStore.Audio.Media.IS_RINGTONE, true);

    ContentResolver cr = context.getContentResolver();
    Uri ringtoneUri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile.getAbsolutePath());
    System.out.println("Path to ringtone= " + ringtoneUri);

    Uri addedUri = cr.insert(ringtoneUri, value);
    System.out.println("addedUri= " + addedUri);

}

public static void setDefaultRingtone(Context context, String ringtoneFilePath) {

    Uri ringtoneUri = Uri.parse(ringtoneFilePath);
    // Set default ring tone
    RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, ringtoneUri);

}

I am calling registerRingtone method and passing my ringtone path (e.g "/storage/emulated/0/test/ringtones/14546548681.mp3").

Now I am facing weird issue. When I try to set the ringtone for mobile phones which have dual SIM, the code works sometimes (sets ringtone) and does not work other times (does not set ringtone). E.g. It works for MotoG which is dual sim and does not work for almost all other phones that have dual SIM (i.e. SONY Experia C, HTC Desire, Lenovo K4 Note)

I am on the conclusion that phones which gives option to set different ringtones for both SIMs i.e for SIM 1 - Ringtone1 and for SIM -2 - Ringtone 2 it gets fail in setting ringtone and also removes my previous selection.

How can I set custom ringtone for device which allow SIM specific ringtone selection?

Can anybody out there help?

0

There are 0 best solutions below