下面列出了android.media.RingtoneManager#getActualDefaultRingtoneUri ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static Ringtone getRingtone(Context context) {
Uri alert =
RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_ALARM);
if (alert == null)
alert = RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_NOTIFICATION);
if (alert == null)
alert = RingtoneManager
.getActualDefaultRingtoneUri(context.getApplicationContext(), RingtoneManager.TYPE_RINGTONE);
final Ringtone ringtone = RingtoneManager.getRingtone(context, alert);
final AudioManager audioManager = (AudioManager) context.getSystemService(AUDIO_SERVICE);
if (audioManager != null) {//who knows lol - btw don't delete user's may lower the alarm sounds by mistake
final int alarmVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_ALARM) * (BPrefs.get(context).getInt(BPrefs.ALARM_VOLUME_KEY, BPrefs.ALARM_VOLUME_DEFAULT_VALUE) + 6) / 10;
audioManager.setStreamVolume(AudioManager.STREAM_ALARM, alarmVolume, 0);
}
ringtone.setAudioAttributes(alarmAttributes);
return ringtone;
}
private Uri getSelectedRingtoneUri() {
// If showing an item for "Default" (@see EXTRA_RINGTONE_SHOW_DEFAULT), this can be one
// of DEFAULT_RINGTONE_URI, DEFAULT_NOTIFICATION_URI, or DEFAULT_ALARM_ALERT_URI to have the
// "Default" item checked.
//
// Otherwise, use RingtoneManager.getActualDefaultRingtoneUri() to get the "actual sound URI".
//
// Do not use RingtoneManager.getDefaultUri(), because that just returns one of
// DEFAULT_RINGTONE_URI, DEFAULT_NOTIFICATION_URI, or DEFAULT_ALARM_ALERT_URI
// depending on the type requested (i.e. what the docs calls "symbolic URI
// which will resolved to the actual sound when played").
String ringtone = getAlarm().ringtone();
return ringtone.isEmpty() ?
RingtoneManager.getActualDefaultRingtoneUri(getContext(), RingtoneManager.TYPE_ALARM)
: Uri.parse(ringtone);
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
/*
* When a client attempts to openFile the default ringtone or
* notification setting Uri, we will proxy the call to the current
* default ringtone's Uri (if it is in the media provider).
*/
int ringtoneType = RingtoneManager.getDefaultType(uri);
// Above call returns -1 if the Uri doesn't match a default type
if (ringtoneType != -1) {
Context context = getContext();
// Get the current value for the default sound
Uri soundUri = RingtoneManager.getActualDefaultRingtoneUri(context, ringtoneType);
if (soundUri != null) {
// Proxy the openFile call to media provider
String authority = soundUri.getAuthority();
if (authority.equals(MediaStore.AUTHORITY)) {
return context.getContentResolver().openFileDescriptor(soundUri, mode);
}
}
}
return super.openFile(uri, mode);
}
private void requestRingtone(String tag) {
if (!mSettingsHelper.getBoolean(tag + "_ringtone_enable", false))
return;
int request_code = -1;
switch (tag) {
case "custom":
request_code = REQUEST_TONE_PICKER_CUSTOM;
break;
case "new_comment":
request_code = REQUEST_TONE_PICKER_NEW_COMMENT;
break;
case "msg_recall":
request_code = REQUEST_TONE_PICKER_MSG_RECALL;
break;
case "comment_recall":
request_code = REQUEST_TONE_PICKER_COMMENT_RECALL;
}
if (request_code == -1)
return;
String uri = mSettingsHelper.getString(tag + "_ringtone", "");
final Uri currentTone = TextUtils.isEmpty(uri) ? RingtoneManager
.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_NOTIFICATION) :
Uri.parse(uri);
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TITLE, getString(R.string.ringtone_selection));
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, currentTone);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true);
startActivityForResult(intent, request_code);
}
public static Uri getDefaultRingtoneUri(Context context, AlarmClockItem.AlarmType type)
{
switch (type)
{
case ALARM:
return RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM);
case NOTIFICATION:
default:
return RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION);
}
}
/**
* This method is used to start ringing the phone.
*/
@TargetApi(21)
private void startRing() {
if (audio != null) {
audio.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audio.setStreamVolume(AudioManager.STREAM_RING, audio.getStreamMaxVolume(AudioManager.STREAM_RING),
AudioManager.FLAG_PLAY_SOUND);
defaultRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(this, RingtoneManager.TYPE_RINGTONE);
if (defaultRingtoneUri != null) {
defaultRingtone = RingtoneManager.getRingtone(this, defaultRingtoneUri);
if (defaultRingtone != null) {
if (deviceInfo.getSdkVersion() >= Build.VERSION_CODES.LOLLIPOP) {
AudioAttributes attributes = new AudioAttributes.Builder().
setUsage(AudioAttributes.USAGE_NOTIFICATION).
setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).
build();
defaultRingtone.setAudioAttributes(attributes);
} else {
defaultRingtone.setStreamType(AudioManager.STREAM_NOTIFICATION);
}
defaultRingtone.play();
}
}
}
}
private static void setRingHasPermission(Activity context, int type, String path) {
Uri oldRingtoneUri = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE); //系统当前 通知铃声
Uri oldNotification = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION); //系统当前 通知铃声
Uri oldAlarm = RingtoneManager.getActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM); //系统当前 闹钟铃声
File sdfile = new File(path);
ContentValues values = new ContentValues();
values.put(MediaStore.MediaColumns.DATA, sdfile.getAbsolutePath());
values.put(MediaStore.MediaColumns.TITLE, sdfile.getName());
values.put(MediaStore.MediaColumns.MIME_TYPE, "audio/mp3");
values.put(MediaStore.Audio.Media.IS_RINGTONE, true);
values.put(MediaStore.Audio.Media.IS_NOTIFICATION, true);
values.put(MediaStore.Audio.Media.IS_ALARM, true);
values.put(MediaStore.Audio.Media.IS_MUSIC, true);
Uri uri = MediaStore.Audio.Media.getContentUriForPath(sdfile.getAbsolutePath());
Uri newUri = null;
String deleteId = "";
try {
Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null);
if (cursor.moveToFirst()) {
deleteId = cursor.getString(cursor.getColumnIndex("_id"));
}
Logger.d(" + deleteId" + deleteId);
context.getContentResolver().delete(uri,
MediaStore.MediaColumns.DATA + "=\"" + sdfile.getAbsolutePath() + "\"", null);
newUri = context.getContentResolver().insert(uri, values);
} catch (Exception e) {
e.printStackTrace();
}
if (newUri != null) {
String ringStoneId = "";
String notificationId = "";
String alarmId = "";
if (null != oldRingtoneUri) {
ringStoneId = oldRingtoneUri.getLastPathSegment();
}
if (null != oldNotification) {
notificationId = oldNotification.getLastPathSegment();
}
if (null != oldAlarm) {
alarmId = oldAlarm.getLastPathSegment();
}
Uri setRingStoneUri;
Uri setNotificationUri;
Uri setAlarmUri;
if (type == RingtoneManager.TYPE_RINGTONE || ringStoneId.equals(deleteId)) {
setRingStoneUri = newUri;
} else {
setRingStoneUri = oldRingtoneUri;
}
if (type == RingtoneManager.TYPE_NOTIFICATION || notificationId.equals(deleteId)) {
setNotificationUri = newUri;
} else {
setNotificationUri = oldNotification;
}
if (type == RingtoneManager.TYPE_ALARM || alarmId.equals(deleteId)) {
setAlarmUri = newUri;
} else {
setAlarmUri = oldAlarm;
}
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, setRingStoneUri);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_NOTIFICATION, setNotificationUri);
RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_ALARM, setAlarmUri);
switch (type) {
case RingtoneManager.TYPE_RINGTONE:
Toast.makeText(context.getApplicationContext(), "设置来电铃声成功!", Toast.LENGTH_SHORT).show();
break;
case RingtoneManager.TYPE_NOTIFICATION:
Toast.makeText(context.getApplicationContext(), "设置通知铃声成功!", Toast.LENGTH_SHORT).show();
break;
case RingtoneManager.TYPE_ALARM:
Toast.makeText(context.getApplicationContext(), "设置闹钟铃声成功!", Toast.LENGTH_SHORT).show();
break;
}
}
}