get Alarm Random Ringtone - Android java.util

Android examples for java.util:Random

Description

get Alarm Random Ringtone

Demo Code


//package com.java2s;

import android.content.Context;

import android.media.RingtoneManager;
import android.net.Uri;

public class Main {
    public static Uri getRandomRingtone(Context context) {
        Uri alert = null;/*from w  w w .j  ava2 s .c o  m*/
        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;
    }
}

Related Tutorials