Example usage for android.media RingtoneManager getRingtone

List of usage examples for android.media RingtoneManager getRingtone

Introduction

In this page you can find the example usage for android.media RingtoneManager getRingtone.

Prototype

public static Ringtone getRingtone(final Context context, Uri ringtoneUri) 

Source Link

Document

Returns a Ringtone for a given sound URI.

Usage

From source file:Main.java

private static Ringtone getDefaultRingtone(Context context, int type) {
    return RingtoneManager.getRingtone(context, getDefaultRingtoneUri(context, type));
}

From source file:Main.java

public static String queryAudioName(Context context, Uri name) {
    String audioId;//w w w . j a v a 2 s .c o m
    String uriName = name.toString();
    uriName = uriName.substring(uriName.lastIndexOf("/") + 1);
    Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
            null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        int counter = cursor.getCount();
        for (int j = 0; j < counter; j++) {
            audioId = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

            if (uriName.equals(audioId)) {
                uriName = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
            } else {
                Ringtone ringtone = RingtoneManager.getRingtone(context, name);
                uriName = ringtone.getTitle(context);
                break;
            }
            cursor.moveToNext();

        }
        cursor.close();
    } else {
        try {
            Ringtone ringtone = RingtoneManager.getRingtone(context, name);
            uriName = ringtone.getTitle(context);
        } catch (Exception e) {
            return uriName;
        }
    }
    return uriName;
}

From source file:com.contactmanager.home.assignmentreminder.AlarmReceiver.java

@Override
public void onReceive(final Context context, Intent intent) {
    //this will update the UI with message
    TimeReminder inst = TimeReminder.instance();
    inst.setAlarmText("Alarm!!! Assignment Start");

    //this will sound the alarm tone
    //this will sound the alarm once, if you wish to
    //raise alarm in loop continuously then use MediaPlayer and setLooping(true)
    Uri alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
    if (alarmUri == null) {
        alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    }//  www  .j a  v  a 2  s.  c om
    mRingtone = RingtoneManager.getRingtone(context, alarmUri);
    mRingtone.play();

    //this will send a notification message
    ComponentName comp = new ComponentName(context.getPackageName(), AlarmService.class.getName());
    startWakefulService(context, (intent.setComponent(comp)));
    setResultCode(Activity.RESULT_OK);
}

From source file:MainActivity.java

public void clickSound(View view) {
    Uri notificationSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
    Ringtone ringtone = RingtoneManager.getRingtone(getApplicationContext(), notificationSoundUri);
    ringtone.play();/*  w  w w  . j a v  a2 s . c  om*/
}

From source file:com.taradov.alarmme.AlarmNotification.java

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);

    getWindow().addFlags(//from w  w  w  . j  a v a2s.com
            WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
                    | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);

    setContentView(R.layout.notification);

    mDateTime = new DateTime(this);
    mTextView = (TextView) findViewById(R.id.alarm_title_text);

    readPreferences();

    mRingtone = RingtoneManager.getRingtone(getApplicationContext(), mAlarmSound);
    if (mVibrate)
        mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

    start(getIntent());
}

From source file:com.memetro.android.notifications.GcmBroadcastReceiver.java

private void sendNotification(String msg, Intent intent, Context context) {
    mNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

    Intent newIntent = new Intent(context, SplashScreenActivity.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, newIntent, PendingIntent.FLAG_ONE_SHOT);

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
            .setSmallIcon(R.drawable.icon_push).setContentTitle("Memetro")
            .setStyle(new NotificationCompat.BigTextStyle().bigText(msg)).setAutoCancel(true)
            .setContentText(msg);//from   w w w . j  a v a2s.  c  o m

    mBuilder.setContentIntent(contentIntent);
    mNotificationManager.notify(NOTIFICATION_ID, mBuilder.build());
    try {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        Ringtone r = RingtoneManager.getRingtone(context, notification);
        r.play();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.microsoft.mimickeralarm.settings.RingtonePreference.java

public void setRingtone(Uri ringtone) {
    mRingtone = ringtone;//w w  w  . ja  v  a  2  s  . c  o  m
    if (mRingtone == null) {
        setSummary(getContext().getString(R.string.pref_no_ringtone));
    } else if (mRingtone.toString().compareToIgnoreCase(GeneralUtilities.defaultRingtone().toString()) == 0) {
        setSummary(getContext().getString(R.string.default_ringtone_name));
    } else {
        setSummary(RingtoneManager.getRingtone(getContext(), mRingtone).getTitle(getContext()));
    }
}

From source file:fr.bmartel.android.iotf.app.BaseActivity.java

protected void onCreate(Bundle savedInstance) {
    super.onCreate(savedInstance);

    sharedpreferences = getSharedPreferences(StorageConst.STORAGE_PROFILE, Context.MODE_PRIVATE);

    notifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

    try {// www.  j av  a  2  s .c om
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        ringtone = RingtoneManager.getRingtone(getApplicationContext(), notification);

    } catch (Exception e) {
        e.printStackTrace();
        ringtone = null;
    }

    screenLock = ((PowerManager) getSystemService(POWER_SERVICE))
            .newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP, "TAG");
}

From source file:org.proninyaroslav.libretorrent.settings.BehaviorSettingsFragment.java

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    final SettingsManager pref = new SettingsManager(getActivity().getApplicationContext());

    String keyTorrentFinishNotify = getString(R.string.pref_key_torrent_finish_notify);
    SwitchPreferenceCompat torrentFinishNotify = (SwitchPreferenceCompat) findPreference(
            keyTorrentFinishNotify);//from w  w w  . j av a 2  s.  c  om
    torrentFinishNotify.setChecked(pref.getBoolean(keyTorrentFinishNotify, true));
    bindOnPreferenceChangeListener(torrentFinishNotify);

    String keyPlaySound = getString(R.string.pref_key_play_sound_notify);
    SwitchPreferenceCompat playSound = (SwitchPreferenceCompat) findPreference(keyPlaySound);
    playSound.setChecked(pref.getBoolean(keyPlaySound, true));
    bindOnPreferenceChangeListener(playSound);

    final String keyNotifySound = getString(R.string.pref_key_notify_sound);
    Preference notifySound = findPreference(keyNotifySound);
    String ringtone = pref.getString(keyNotifySound,
            RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION).toString());
    notifySound
            .setSummary(RingtoneManager.getRingtone(getActivity().getApplicationContext(), Uri.parse(ringtone))
                    .getTitle(getActivity().getApplicationContext()));
    /* See https://code.google.com/p/android/issues/detail?id=183255 */
    notifySound.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
        @Override
        public boolean onPreferenceClick(Preference preference) {
            Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
            intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,
                    Settings.System.DEFAULT_NOTIFICATION_URI);

            String curRingtone = pref.getString(keyNotifySound, null);
            if (curRingtone != null) {
                if (curRingtone.length() == 0) {
                    // Select "Silent"
                    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, (Uri) null);
                } else {
                    intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, Uri.parse(curRingtone));
                }

            } else {
                intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,
                        Settings.System.DEFAULT_NOTIFICATION_URI);
            }

            startActivityForResult(intent, REQUEST_CODE_ALERT_RINGTONE);

            return true;
        }
    });

    String keyLedIndicator = getString(R.string.pref_key_led_indicator_notify);
    SwitchPreferenceCompat ledIndicator = (SwitchPreferenceCompat) findPreference(keyLedIndicator);
    ledIndicator.setChecked(pref.getBoolean(keyLedIndicator, true));
    bindOnPreferenceChangeListener(ledIndicator);

    String keyLedIndicatorColor = getString(R.string.pref_key_led_indicator_color_notify);
    LightPreference ledIndicatorColor = (LightPreference) findPreference(keyLedIndicatorColor);
    ledIndicatorColor.forceSetValue(pref.getInt(keyLedIndicatorColor,
            ContextCompat.getColor(getActivity().getApplicationContext(), R.color.primary)));
    bindOnPreferenceChangeListener(ledIndicatorColor);

    String keyVibration = getString(R.string.pref_key_vibration_notify);
    SwitchPreferenceCompat vibration = (SwitchPreferenceCompat) findPreference(keyVibration);
    vibration.setChecked(pref.getBoolean(keyVibration, true));
    bindOnPreferenceChangeListener(vibration);

    String keyAutostart = getString(R.string.pref_key_autostart);
    SwitchPreferenceCompat autostart = (SwitchPreferenceCompat) findPreference(keyAutostart);
    autostart.setChecked(pref.getBoolean(keyAutostart, false));
    bindOnPreferenceChangeListener(autostart);

    String keyShutdownComplete = getString(R.string.pref_key_shutdown_downloads_complete);
    SwitchPreferenceCompat shutdownComplete = (SwitchPreferenceCompat) findPreference(keyShutdownComplete);
    shutdownComplete.setChecked(pref.getBoolean(keyShutdownComplete, false));
    bindOnPreferenceChangeListener(shutdownComplete);

    String keyCpuSleep = getString(R.string.pref_key_cpu_do_not_sleep);
    SwitchPreferenceCompat cpuSleep = (SwitchPreferenceCompat) findPreference(keyCpuSleep);
    cpuSleep.setChecked(pref.getBoolean(keyCpuSleep, false));
    bindOnPreferenceChangeListener(cpuSleep);

    String keyOnlyCharging = getString(R.string.pref_key_download_and_upload_only_when_charging);
    SwitchPreferenceCompat onlyCharging = (SwitchPreferenceCompat) findPreference(keyOnlyCharging);
    onlyCharging.setChecked(pref.getBoolean(keyOnlyCharging, false));
    bindOnPreferenceChangeListener(onlyCharging);

    String keyBatteryControl = getString(R.string.pref_key_battery_control);
    SwitchPreferenceCompat batteryControl = (SwitchPreferenceCompat) findPreference(keyBatteryControl);
    batteryControl.setSummary(
            String.format(getString(R.string.pref_battery_control_summary), Utils.getDefaultBatteryLowLevel()));
    batteryControl.setChecked(pref.getBoolean(keyBatteryControl, false));
    bindOnPreferenceChangeListener(batteryControl);
}

From source file:org.apache.cordova.ringtonepicker.RingtonePicker.java

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.d("customPlugin", "Calling onActivityResult");
    if (resultCode == Activity.RESULT_OK && requestCode == 5) {
        Uri uri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
        Ringtone ringtone = RingtoneManager.getRingtone(this.cordova.getActivity(), uri);
        String title = ringtone.getTitle(this.cordova.getActivity());
        Log.d("customPlugin", "I picked this ringtone " + uri);
        Log.d("customPlugin", "I picked this ringtone title" + title);

        if (uri != null) {
            Log.d("customPlugin", "Setting ringtone to  " + notification_uri);
            String returnText = "{\"ringtone\": {\"title\": \"" + title + "\",\"uri\": \"" + uri + "\"}}";

            PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, returnText);
            pluginResult.setKeepCallback(true);
            this.callbackContext.sendPluginResult(pluginResult);
        }/*from   w ww. j a  v  a2s .  c o m*/
    }
}