Java tutorial
//package com.java2s; import android.content.Context; import android.database.Cursor; import android.media.RingtoneManager; import android.net.Uri; import android.support.annotation.NonNull; import java.util.HashMap; import java.util.Map; public class Main { /** * Returns whether given ringtone uri is valid (will produce sound). Can be used to determine * validity of a ring tone uri which is retrieved from preferences. * * @param context {@link Context} used to access system data. * @param ringTone {@link Uri} for ring tone. * * @return true if there is a registered ring tone with given uri, false otherwise. */ public static boolean isRingtoneValid(@NonNull Context context, @NonNull Uri ringTone) { Map<String, String> ringTones = getRingtones(context); return (ringTones.values().contains(ringTone.toString())); } /** * Returns a map of ring tones registered on system. Map key is ring tone name, * value is ring tone uri. * * @param context {@link Context} used to access system data. * @return Map of ring tones. */ public static Map<String, String> getRingtones(@NonNull Context context, int type) { RingtoneManager manager = new RingtoneManager(context); manager.setType(type); Cursor cursor = manager.getCursor(); Map<String, String> map = new HashMap<>(); while (cursor.moveToNext()) { String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX); String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX); map.put(notificationTitle, notificationUri); } return map; } public static Map<String, String> getRingtones(@NonNull Context context) { return getRingtones(context, RingtoneManager.TYPE_RINGTONE); } }