Java tutorial
//package com.java2s; //License from project: Apache License import android.content.Context; import android.media.RingtoneManager; import android.net.Uri; public class Main { public static Uri getRandomRingtone(Context context) { Uri alert = null; RingtoneManager ringtoneManager = new RingtoneManager(context); ringtoneManager.setType(RingtoneManager.TYPE_ALARM); int count = ringtoneManager.getCursor().getCount(); int attempts = 0; do { int random = (int) Math.random() * (count + 1); alert = ringtoneManager.getRingtoneUri(random); } while (alert == null && ++attempts < 5); return alert; } }