15

I've seen plenty of examples of how to set a default ringtone, but what I'm more interested in is being able populate a drop down box list filled with the available ringtones on the phone. So the list that people see when they change their ringtone in the android settings, I want to be able to list all of those.

The closest thing I've found is here, but again this is just for setting the default ringtone. Any ideas anyone? It can be in or out of ringtonemanager.

4

2 回答 2

26

这将为您返回所有可用铃声的标题和 uri。随他们做你想做的事!

public Map<String, String> getNotifications() {
    RingtoneManager manager = new RingtoneManager(this);
    manager.setType(RingtoneManager.TYPE_RINGTONE);
    Cursor cursor = manager.getCursor();

    Map<String, String> list = new HashMap<>();
    while (cursor.moveToNext()) {
        String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
        String notificationUri = cursor.getString(RingtoneManager.URI_COLUMN_INDEX) + "/" + cursor.getString(RingtoneManager.ID_COLUMN_INDEX);

        list.put(notificationTitle, notificationUri);
    }

    return list;
}
于 2015-04-17T11:21:43.750 回答
16

RingtoneManager是您正在寻找的。您只需要使用setType设置TYPE_RINGTONE ,然后遍历getCursor提供的 Cursor 。

这是一个返回 URI 数组的假设方法的工作示例,唯一的细微差别是它正在寻找警报而不是铃声:

RingtoneManager ringtoneMgr = new RingtoneManager(this);
ringtoneMgr.setType(RingtoneManager.TYPE_ALARM);
Cursor alarmsCursor = ringtoneMgr.getCursor();
int alarmsCount = alarmsCursor.getCount();
if (alarmsCount == 0 && !alarmsCursor.moveToFirst()) {
    return null;
}
Uri[] alarms = new Uri[alarmsCount];
while(!alarmsCursor.isAfterLast() && alarmsCursor.moveToNext()) {
    int currentPosition = alarmsCursor.getPosition();
    alarms[currentPosition] = ringtoneMgr.getRingtoneUri(currentPosition);
}
alarmsCursor.close();
return alarms;
于 2012-09-25T16:39:03.887 回答