RingtonePreference 设置为默认铃声

一、RingtonePreference 正常都是默认为静音的。要想设置为“默认铃声”  ,xml中的defaultValue如下:

<RingtonePreference android:key="ring_choose_editor"
			 				android:title="@string/system_setup_notify_ring"
			 				android:defaultValue="content://settings/system/notification_sound"/>

二、如果想把默认铃声设置为自己的音乐,通过RingtonePreference偶没有找到解决方案(希望有解决方案的朋友能留言告知如何处理,多谢!),但可以通过RingtoneManager实现。

1、首先,将自己的声音文件保存到data/data/packagename/files下,如下:

public static final void saveDefaultRing(Context context) throws IOException {
		
		InputStream is = context.getAssets().open(PeConstants.DEFAULT_RING_NAME);
		FileOutputStream out = context.openFileOutput(PeConstants.DEFAULT_RING_NAME, Context.MODE_WORLD_READABLE);

		byte[] buf = new byte[1024];
		int len;
		while ((len = is.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		is.close();
		out.close();
	}

 2、读取声音文件的uri。

File f = context.getFileStreamPath(PeConstants.DEFAULT_RING_NAME);
		if (f.exists()) {
			uri = Uri.fromFile(f);
		}

 3、展示声音提示框,设置默认铃声为自己的声音文件uri。

Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);//声音类型
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, onRestoreRingtone(RingtoneManager.TYPE_NOTIFICATION));//声音对话框出现时的选中的铃声
			
			intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, Uri.parse(DEFAULT_RING_URI));//默认铃声的uri,既步骤2得到的uri
			startActivityForResult(intent, RINGTONE_PICKER);

 4、onActivityResult方法中实现代码如下。

 if (requestCode == RINGTONE_PICKER && resultCode == RESULT_OK) {
			// 得到我们选择的铃声并播放
			Uri uri = intent.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			Ringtone myRingTone = RingtoneManager.getRingtone(mAppContext, uri);
										myRingTone.play();
										Thread.sleep(3000);
										myRingTone.stop();}

 三、铃声Uri对应表

MediaStore.Audio.Media.EXTERNAL_CONTENT_URI---content://media/external/audio/media/ SD卡上面的
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI---content://media/internal/audio/media/ 手机
 
获取当前系统的不同类型的铃声uri:
RingtoneManager.getActualDefaultRingtoneUri(context,type)
type:
RingtoneManager.TYPE_NOTIFICATION(通知铃声), 
RingtoneManager.TYPE_RINGTONE, RingtoneManager.TYPE_ALARM,RingtoneManager.TYPE_ALL,这四个类型,会得到带有具体id的uri,比如:
-----content://media/internal/audio/media/34
 
默认铃声:
Settings.System.DEFAULT_NOTIFICATION_URI ----content://setting/system/notification_sound
Settings.System.DEFAULT_RINGTONE_URI------content://setting/system/ringtone
Settings.System.DEFAULT_ALARM_ALERT_URI-----content://setting/system/alarm_alert
静音 无
 
 
判断reminderRingtone铃声是具体是在sd,手机还是设置铃声。匹配在SD卡的话,进行处理。
UriMatcher  sMatcher = new UriMatcher(UriMatcher.NO_MATCH);
sMatcher.addURI("media", "external/audio/media/#", 1);//#通配符,匹配数字
sMatcher.addURI("media", "internal/audio/media/#", 2);
sMatcher.addURI("settings", "system/*", 3);//notification_sound,*通配符,匹配文字
switch(sMatcher.match(Uri.parse(reminderRingtone)))
{
case 1:
boolean sdCardExist = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(!sdCardExist)
{
Editor editor = prefs.edit();
editor.putString(CalendarPreferenceActivity.KEY_ALERTS_RINGTONE, "content://settings/system/notification_sound");
editor.commit();
notification.sound = Settings.System.DEFAULT_NOTIFICATION_URI;
return;
}
break;
default:
break;
}

猜你喜欢

转载自gjhappyyy.iteye.com/blog/1625590
今日推荐