List of usage examples for android.media RingtoneManager setActualDefaultRingtoneUri
public static void setActualDefaultRingtoneUri(Context context, int type, Uri ringtoneUri)
From source file:Main.java
/** * Set the Default Notification Ringtone to the given URI. * @param ctx/* www.ja va 2s.co m*/ * @param uri URI of a row in MediaStore */ public static void setDefaultNotificationRingtone(Context ctx, Uri uri) { RingtoneManager.setActualDefaultRingtoneUri(ctx, RingtoneManager.TYPE_NOTIFICATION, uri); }
From source file:Main.java
public static void changeRingtone(Context context) { SharedPreferences preferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE); if (!preferences.getBoolean("active", false)) return;//from w ww. j a v a 2s .co m RingtoneManager mgr = new RingtoneManager(context); Random random = new Random(System.currentTimeMillis()); int n = random.nextInt(mgr.getCursor().getCount()); RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, mgr.getRingtoneUri(n)); }
From source file:Main.java
public static void changeRingtone(Context context) { SharedPreferences sharedPreferences = context.getSharedPreferences("randomizer", Context.MODE_PRIVATE); if (!sharedPreferences.getBoolean("active", false)) { return;//from w w w.j a v a 2 s . com } // END if RingtoneManager ringtoneManager = new RingtoneManager(context); Random random = new Random(System.currentTimeMillis()); int count = random.nextInt(ringtoneManager.getCursor().getCount()); RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, ringtoneManager.getRingtoneUri(count)); }
From source file:com.rks.musicx.misc.utils.Helper.java
/** * Set Ringtone/*from ww w . j a va 2s .c o m*/ * * @param context * @param path */ private static void setRingtone(Context context, String path) { if (path == null) { return; } File file = new File(path); ContentValues contentValues = new ContentValues(); contentValues.put(MediaColumns.DATA, file.getAbsolutePath()); String filterName = path.substring(path.lastIndexOf("/") + 1); contentValues.put(MediaColumns.TITLE, filterName); contentValues.put(MediaColumns.MIME_TYPE, "audio/mp3"); contentValues.put(MediaColumns.SIZE, file.length()); contentValues.put(Media.IS_RINGTONE, true); Uri uri = MediaStore.Audio.Media.getContentUriForPath(path); Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[] { path }, null); if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) { String id = cursor.getString(0); contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true); context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[] { path }); Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id)); try { RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri); } catch (Throwable t) { t.printStackTrace(); } cursor.close(); } }
From source file:saphion.fragments.alarm.AlarmFragment.java
private void saveRingtoneUri(Intent intent) { Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if (uri == null) { uri = Alarm.NO_RINGTONE_URI; }//from ww w . j a va 2 s. c o m mSelectedAlarm.alert = uri; // Save the last selected ringtone as the default for new alarms if (!Alarm.NO_RINGTONE_URI.equals(uri)) { RingtoneManager.setActualDefaultRingtoneUri(getActivity(), RingtoneManager.TYPE_ALARM, uri); } asyncUpdateAlarm(mSelectedAlarm, false); }
From source file:com.example.mydemos.view.RingtonePickerActivity.java
public void onClick(View v) { Log.e("lys", "onClick called"); if (toneType == -1) { finish();//ww w. j a v a 2 s .co m } Intent resultIntent = new Intent(); ContentResolver resolver = getContentResolver(); switch (v.getId()) { case R.id.ok: //if(mSelectedId >= 0 && mSelectedId != SILENT_ID && mSelectedId != DEFAULT_ID) //{ // Log.e("lys","onClick mSelectedId == "+mSelectedId); // ContentValues values = new ContentValues(2); // if(toneActivityType == ALARM_TYPE) // { // values.put(MediaStore.Audio.Media.IS_ALARM, "1"); // } // else if(toneActivityType==NOTIFICATION_TYPE) // { // values.put(MediaStore.Audio.Media.IS_NOTIFICATION, "1"); // } // else//! if(toneType==RINGTONE_TYPE) by duwenhua // { // values.put(MediaStore.Audio.Media.IS_RINGTONE, "1"); // } Log.e("lys", "onClick values == " + mSelectedUri); // resolver.update(mSelectedUri, values, null, null); // resultIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, mSelectedUri); // setResult(RESULT_OK, resultIntent); //Toast.makeText(this, "mSelectedUri==" + mSelectedUri, // Toast.LENGTH_LONG).show(); // finish(); //} //else if(mSelectedId == SILENT_ID) //{ //resultIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, mSelectedUri); //setResult(RESULT_OK, resultIntent); //Toast.makeText(this, "mSelectedUri==" + mSelectedUri, // Toast.LENGTH_LONG).show(); //finish(); //} if (mSelectedId == SILENT_ID) { mSelectedUri = null; } else if (mSelectedId == DEFAULT_ID) { mSelectedUri = mUriForDefaultItem; } else { Log.e("lys", "onClick values mSelectedId == " + mSelectedId); //wuqingliang modify begin20130307 for the first time entery the ringtonePickerActivity, and the ringtone is in EXTERNAL_CONTENT_URI //user click ok button in TAP ringtone. if (BaseUri != MediaStore.Audio.Media.INTERNAL_CONTENT_URI && BaseUri != MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) { mSelectedUri = mExistingUri; } else { mSelectedUri = ContentUris.withAppendedId(BaseUri, mSelectedId); } //wuqingliang modify end } resultIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI, mSelectedUri); RingtoneManager.setActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE, mSelectedUri); setResult(RESULT_OK, resultIntent); //Toast.makeText(this, "mSelectedUri==" + mSelectedUri, // Toast.LENGTH_LONG).show(); finish(); break; case R.id.cancel: setResult(RESULT_CANCELED); stopMediaPlayer(); finish(); break; } }
From source file:com.andrew.apollo.utils.MusicUtils.java
/** * @param context The {@link Context} to use * @param id The song ID.//from w w w. java 2 s. c o m */ public static void setRingtone(final Context context, final long id) { final ContentResolver resolver = context.getContentResolver(); final Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, id); try { final ContentValues values = new ContentValues(2); values.put(AudioColumns.IS_RINGTONE, "1"); values.put(AudioColumns.IS_ALARM, "1"); resolver.update(uri, values, null, null); } catch (final UnsupportedOperationException ignored) { return; } final String[] projection = new String[] { BaseColumns._ID, MediaColumns.DATA, MediaColumns.TITLE }; final String selection = BaseColumns._ID + "=" + id; Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null, null); try { if (cursor != null && cursor.getCount() == 1) { cursor.moveToFirst(); RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, uri); final String message = context.getString(R.string.set_as_ringtone, cursor.getString(2)); AppMsg.makeText(context, message, AppMsg.STYLE_CONFIRM).show(); } } catch (Throwable ignored) { UIUtils.showLongMessage(context, R.string.ringtone_not_set); } finally { if (cursor != null) { cursor.close(); } } }
From source file:com.ringdroid.RingdroidEditActivity.java
private void afterSavingRingtone(CharSequence title, String outPath, File outFile, int duration) { long length = outFile.length(); if (length <= 512) { outFile.delete();// www. j a v a 2 s . c o m new AlertDialog.Builder(this).setTitle(R.string.alert_title_failure) .setMessage(R.string.too_small_error).setPositiveButton(R.string.alert_ok_button, null) .setCancelable(false).show(); return; } // Create the database record, pointing to the existing file path long fileSize = outFile.length(); String mimeType = "audio/mpeg"; String artist = "" + getResources().getText(R.string.artist_name); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, outPath); values.put(MediaStore.MediaColumns.TITLE, title.toString()); values.put(MediaStore.MediaColumns.SIZE, fileSize); values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); values.put(MediaStore.Audio.Media.ARTIST, artist); values.put(MediaStore.Audio.Media.DURATION, duration); values.put(MediaStore.Audio.Media.IS_RINGTONE, mNewFileKind == FileSaveDialog.FILE_KIND_RINGTONE); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION); values.put(MediaStore.Audio.Media.IS_ALARM, mNewFileKind == FileSaveDialog.FILE_KIND_ALARM); values.put(MediaStore.Audio.Media.IS_MUSIC, mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC); // Insert it into the database Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath); final Uri newUri = getContentResolver().insert(uri, values); setResult(RESULT_OK, new Intent().setData(newUri)); // Update a preference that counts how many times we've // successfully saved a ringtone or other audio SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); int successCount = prefs.getInt(PREF_SUCCESS_COUNT, 0); SharedPreferences.Editor prefsEditor = prefs.edit(); prefsEditor.putInt(PREF_SUCCESS_COUNT, successCount + 1); prefsEditor.commit(); // If Ringdroid was launched to get content, just return if (mWasGetContentIntent) { // sendStatsToServerIfAllowedAndFinish(); return; } // There's nothing more to do with music or an alarm. Show a // success message and then quit. if (mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC || mNewFileKind == FileSaveDialog.FILE_KIND_ALARM) { Toast.makeText(this, R.string.save_success_message, Toast.LENGTH_SHORT).show(); // sendStatsToServerIfAllowedAndFinish(); return; } // If it's a notification, give the user the option of making // this their default notification. If they say no, we're finished. if (mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION) { new AlertDialog.Builder(RingdroidEditActivity.this).setTitle(R.string.alert_title_success) .setMessage(R.string.set_default_notification) .setPositiveButton(R.string.alert_yes_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { RingtoneManager.setActualDefaultRingtoneUri(RingdroidEditActivity.this, RingtoneManager.TYPE_NOTIFICATION, newUri); // sendStatsToServerIfAllowedAndFinish(); } }).setNegativeButton(R.string.alert_no_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { // sendStatsToServerIfAllowedAndFinish(); } }).setCancelable(false).show(); return; } // If we get here, that means the type is a ringtone. There are // three choices: make this your default ringtone, assign it to a // contact, or do nothing. final Handler handler = new Handler() { public void handleMessage(Message response) { int actionId = response.arg1; switch (actionId) { case R.id.button_make_default: RingtoneManager.setActualDefaultRingtoneUri(RingdroidEditActivity.this, RingtoneManager.TYPE_RINGTONE, newUri); Toast.makeText(RingdroidEditActivity.this, R.string.default_ringtone_success_message, Toast.LENGTH_SHORT).show(); // sendStatsToServerIfAllowedAndFinish(); break; case R.id.button_choose_contact: chooseContactForRingtone(newUri); break; default: case R.id.button_do_nothing: // sendStatsToServerIfAllowedAndFinish(); break; } } }; Message message = Message.obtain(handler); AfterSaveActionDialog dlog = new AfterSaveActionDialog(this, message); dlog.show(); }
From source file:mobi.omegacentauri.ptimer.PTimerEditActivity.java
private void afterSavingRingtone(CharSequence title, String outPath, File outFile, int duration) { long length = outFile.length(); if (length <= 512) { outFile.delete();/*from w w w .ja va 2s.co m*/ new AlertDialog.Builder(this).setTitle(R.string.alert_title_failure) .setMessage(R.string.too_small_error).setPositiveButton(R.string.alert_ok_button, null) .setCancelable(false).show(); return; } // Create the database record, pointing to the existing file path long fileSize = outFile.length(); String mimeType = "audio/mpeg"; String artist = "" + getResources().getText(R.string.artist_name); ContentValues values = new ContentValues(); values.put(MediaStore.MediaColumns.DATA, outPath); values.put(MediaStore.MediaColumns.TITLE, title.toString()); values.put(MediaStore.MediaColumns.SIZE, fileSize); values.put(MediaStore.MediaColumns.MIME_TYPE, mimeType); values.put(MediaStore.Audio.Media.ARTIST, artist); values.put(MediaStore.Audio.Media.DURATION, duration); values.put(MediaStore.Audio.Media.IS_RINGTONE, mNewFileKind == FileSaveDialog.FILE_KIND_RINGTONE); values.put(MediaStore.Audio.Media.IS_NOTIFICATION, mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION); values.put(MediaStore.Audio.Media.IS_ALARM, mNewFileKind == FileSaveDialog.FILE_KIND_ALARM); values.put(MediaStore.Audio.Media.IS_MUSIC, mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC); // Insert it into the database Uri uri = MediaStore.Audio.Media.getContentUriForPath(outPath); final Uri newUri = getContentResolver().insert(uri, values); setResult(RESULT_OK, new Intent().setData(newUri)); // Update a preference that counts how many times we've // successfully saved a ringtone or other audio SharedPreferences prefs = getPreferences(Context.MODE_PRIVATE); int successCount = prefs.getInt(PREF_SUCCESS_COUNT, 0); SharedPreferences.Editor prefsEditor = prefs.edit(); prefsEditor.putInt(PREF_SUCCESS_COUNT, successCount + 1); prefsEditor.commit(); // If Ringdroid was launched to get content, just return if (mWasGetContentIntent) { sendStatsToServerIfAllowedAndFinish(); return; } // There's nothing more to do with music or an alarm. Show a // success message and then quit. if (mNewFileKind == FileSaveDialog.FILE_KIND_MUSIC || mNewFileKind == FileSaveDialog.FILE_KIND_ALARM) { Toast.makeText(this, R.string.save_success_message, Toast.LENGTH_SHORT).show(); sendStatsToServerIfAllowedAndFinish(); return; } // If it's a notification, give the user the option of making // this their default notification. If they say no, we're finished. if (mNewFileKind == FileSaveDialog.FILE_KIND_NOTIFICATION) { new AlertDialog.Builder(PTimerEditActivity.this).setTitle(R.string.alert_title_success) .setMessage(R.string.set_default_notification) .setPositiveButton(R.string.alert_yes_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { RingtoneManager.setActualDefaultRingtoneUri(PTimerEditActivity.this, RingtoneManager.TYPE_NOTIFICATION, newUri); sendStatsToServerIfAllowedAndFinish(); } }).setNegativeButton(R.string.alert_no_button, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton) { sendStatsToServerIfAllowedAndFinish(); } }).setCancelable(false).show(); return; } // If we get here, that means the type is a ringtone. There are // three choices: make this your default ringtone, assign it to a // contact, or do nothing. final Handler handler = new Handler() { public void handleMessage(Message response) { int actionId = response.arg1; switch (actionId) { case R.id.button_make_default: RingtoneManager.setActualDefaultRingtoneUri(PTimerEditActivity.this, RingtoneManager.TYPE_RINGTONE, newUri); Toast.makeText(PTimerEditActivity.this, R.string.default_ringtone_success_message, Toast.LENGTH_SHORT).show(); sendStatsToServerIfAllowedAndFinish(); break; case R.id.button_choose_contact: chooseContactForRingtone(newUri); break; default: case R.id.button_do_nothing: sendStatsToServerIfAllowedAndFinish(); break; } } }; Message message = Message.obtain(handler); AfterSaveActionDialog dlog = new AfterSaveActionDialog(this, message); dlog.show(); }