Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import android.content.Context;

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

public class Main {
    /**
     * Returns the system default ringtone URI {@link Uri}, or
     * the first available ringtone when the system default
     * does not exist (usually tablets).
     *
     * @param ctx the {@link Context}
     * @return Uri ringtone URI
     */
    public static Uri getDefaultRingtoneUri(Context ctx, int type) {
        Uri uri;

        uri = RingtoneManager.getActualDefaultRingtoneUri(ctx, type);

        if (uri == null) {
            // The default ringtone doesn't exist - probably a tablet
            // Return the first available
            RingtoneManager rm = new RingtoneManager(ctx);
            rm.setType(type);

            Cursor cursor = rm.getCursor();
            cursor.moveToFirst();

            String idString = cursor.getString(RingtoneManager.ID_COLUMN_INDEX);
            String uriString = cursor.getString(RingtoneManager.URI_COLUMN_INDEX);

            uri = Uri.parse(uriString + '/' + idString);

            cursor.close();

            return uri;
        } else {
            // Return system default ringtone
            return uri;
        }
    }

    public static Uri getDefaultRingtoneUri(Context ctx) {
        return getDefaultRingtoneUri(ctx, RingtoneManager.TYPE_RINGTONE);
    }
}