A
A
Artem Kalachyan2014-09-04 11:36:11
Android
Artem Kalachyan, 2014-09-04 11:36:11

How to programmatically change ringtone on Android 4.0+?

There is a path to the file that must be put on the call from the BroadcastReceiver. I haven't been able to find a clear answer. Who can offer a working option? Best of all - a method where you passed the full path to the file and the file name, and all the dirty work was done in it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Artem Kalachyan, 2014-09-05
@Bringoff

In general, something like this:

private void changeRingtone(boolean isRingtone, boolean isNotification) {
         File ringtoneFile = new File(dir, newRing);

        ContentValues values = new ContentValues();
        values.put(MediaStore.MediaColumns.DATA, ringtoneFile.getAbsolutePath());
        values.put(MediaStore.MediaColumns.TITLE, ringtoneFile.getName());
        values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
        values.put(MediaStore.Audio.Media.ARTIST, " ");
        values.put(MediaStore.MediaColumns.SIZE, ringtoneFile.getUsableSpace());
        values.put(MediaStore.Audio.Media.IS_RINGTONE, isRingtone);
        values.put(MediaStore.Audio.Media.IS_NOTIFICATION, isNotification);
        values.put(MediaStore.Audio.Media.IS_ALARM, false);
        values.put(MediaStore.Audio.Media.IS_MUSIC, false);

        Uri uri = MediaStore.Audio.Media.getContentUriForPath(ringtoneFile
                .getAbsolutePath());
        mContext.getContentResolver().delete(
                uri,
                MediaStore.MediaColumns.DATA + "=\""
                        + ringtoneFile.getAbsolutePath() + "\"", null);
        Uri newUri = mContext.getContentResolver().insert(uri, values);
        try {
            if (isRingtone) {
                RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_RINGTONE, newUri);
            }
            if (isNotification) {
                RingtoneManager.setActualDefaultRingtoneUri(mContext, RingtoneManager.TYPE_NOTIFICATION, newUri);
            }
        } catch (Throwable t) {
            System.err.println("Can't set ringtone");
            t.printStackTrace();
        }
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question