Android中铃声总结【安卓源码解析一】

最近研究源码程序,改了改手机短信铃声的源码,最近总结了下铃声的代码,写个activity继承PreferenceActivity有:手机短信铃声,手机铃声,闹钟铃声,还有sdcard中的铃声,通过选择相应的铃声,然后读取到xml文件里面,通过读取preference.xml文件,intent传个参数进去intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, phoneUri);打开对话框的时候就默认选中上次被选中的音乐。程序流程:在onCreate()方法中加入addPreferencesFromResource(R.xml.preferences);加载xml文件。@Override重写onPreferenceTreeClick()方法,处理点击事件,在打开对话框铃声的时候,先读取xml文件,判断是否有值,如果有值,就传值intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, phoneUri);然后进行选择铃声。通过onActivityResult()接受传递过来的uri,系统默认的铃声是通过data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);方法来获取uri的,而sdcard中的铃声通过Uri pickedUri = data.getData();来获得选中的uri的,再然后通过editor.commit(); 来提交接受过来的uri和音乐的名字整个流程大概就是这样。想要代码的请留言留下邮箱!

大明原创,转载请标明出处:http://blog.****.net/wdaming1986/article/details/6919653

下面请看截图:

第一次打开程序的界面:点击“选择短信铃声”后的界面:

Android中铃声总结【安卓源码解析一】Android中铃声总结【安卓源码解析一】

选择铃声的dialog后的界面: 点击“选择手机铃声”后的界面:

Android中铃声总结【安卓源码解析一】Android中铃声总结【安卓源码解析一】

点击“选择手机铃声”后的界面:点击“选择闹钟铃声”后的dialog界面:

Android中铃声总结【安卓源码解析一】Android中铃声总结【安卓源码解析一】

点击“选择sdcard中的铃声”后的界面: 点击“选择曲目”后弹出sdcard的界面:

Android中铃声总结【安卓源码解析一】Android中铃声总结【安卓源码解析一】

下面代码附上:

在SoundSettingActivity这个工程下面:

一、在com.cn.android.daming包的SoundSettingMainActivity.java类中的代码:

  1. <spanstyle="font-size:16px;color:#000000;">packagecom.cn.android.daming;
  2. importandroid.content.Intent;
  3. importandroid.content.SharedPreferences;
  4. importandroid.media.Ringtone;
  5. importandroid.media.RingtoneManager;
  6. importandroid.net.Uri;
  7. importandroid.os.Bundle;
  8. importandroid.preference.Preference;
  9. importandroid.preference.PreferenceActivity;
  10. importandroid.preference.PreferenceManager;
  11. importandroid.preference.PreferenceScreen;
  12. publicclassSoundSettingMainActivityextendsPreferenceActivity{
  13. privatestaticfinalintSMS_RINGTONE_PICKED=1;
  14. privatestaticfinalintPHONE_RINGTONE_PICKED=2;
  15. privatestaticfinalintALARM_RINGTONE_PICKED=3;
  16. privatestaticfinalintSDCARD_RINGTONE_PICKED=4;
  17. publicstaticfinalStringNOTIFICATION_RINGTONE="pref_notification_ringtone";
  18. publicstaticfinalStringNOTIFICATION_RINGTONE_TITLE_NAME="pref_notification_ringtone_name";
  19. publicstaticfinalStringPHONE_RINGTONE="pref_phone_ringtone";
  20. publicstaticfinalStringPHONE_RINGTONE_TITLE_NAME="pref_phone_ringtone_title_name";
  21. publicstaticfinalStringALARM_RINGTONE="pref_alarm_ringtone";
  22. publicstaticfinalStringALARM_RINGTONE_TITLE_NAME="pref_alarm_ringtone_title_name";
  23. publicstaticfinalStringSDCARD_RINGTONE="pref_sdcard_ringtone";
  24. publicstaticfinalStringSDCARD_RINGTONE_TITLE_NAME="pref_sdcard_ringtone_title_name";
  25. privateStringnotificationStr;
  26. privateStringphoneStr;
  27. privateStringalarmStr;
  28. privateStringsdcardStr;
  29. privatePreferencemMmsSoundsPref;
  30. privatePreferencemPhoneSoundsPref;
  31. privatePreferencemAlarmSoundsPref;
  32. privatePreferencemSdcardSoundsPref;
  33. @Override
  34. publicvoidonCreate(BundlesavedInstanceState){
  35. super.onCreate(savedInstanceState);
  36. addPreferencesFromResource(R.xml.preferences);
  37. setMessagePreferences();
  38. setDefaultPreferences();
  39. }
  40. privatevoidsetMessagePreferences(){
  41. mMmsSoundsPref=findPreference("pref_sms_ringtone");
  42. mPhoneSoundsPref=findPreference("pref_phone_ringtone");
  43. mAlarmSoundsPref=findPreference("pref_alarm_ringtone");
  44. mSdcardSoundsPref=findPreference("pref_sdcard_ringtone");
  45. }
  46. privatevoidsetDefaultPreferences(){
  47. SharedPreferencesinnersharedPreferences=PreferenceManager.getDefaultSharedPreferences(SoundSettingMainActivity.this);
  48. StringnotificationRingtoneTitleName=innersharedPreferences.getString(NOTIFICATION_RINGTONE_TITLE_NAME,null);
  49. if(notificationRingtoneTitleName!=null){
  50. mMmsSoundsPref.setSummary(notificationRingtoneTitleName);
  51. }else{
  52. mMmsSoundsPref.setSummary(getString(R.string.pref_summary_notification_ringtone));
  53. }
  54. StringphoneRingtoneTitleName=innersharedPreferences.getString(PHONE_RINGTONE_TITLE_NAME,null);
  55. if(phoneRingtoneTitleName!=null){
  56. mPhoneSoundsPref.setSummary(phoneRingtoneTitleName);
  57. }else{
  58. mPhoneSoundsPref.setSummary(getString(R.string.pref_summary_phone_ringtone));
  59. }
  60. StringalarmRingtoneTitleName=innersharedPreferences.getString(ALARM_RINGTONE_TITLE_NAME,null);
  61. if(alarmRingtoneTitleName!=null){
  62. mAlarmSoundsPref.setSummary(alarmRingtoneTitleName);
  63. }else{
  64. mAlarmSoundsPref.setSummary(getString(R.string.pref_summary_alarm_ringtone));
  65. }
  66. StringsdcardRingtoneTitleName=innersharedPreferences.getString(SDCARD_RINGTONE_TITLE_NAME,null);
  67. if(sdcardRingtoneTitleName!=null){
  68. mSdcardSoundsPref.setSummary(sdcardRingtoneTitleName);
  69. }else{
  70. mSdcardSoundsPref.setSummary(getString(R.string.pref_summary_sdcard_ringtone));
  71. }
  72. }
  73. @Override
  74. publicbooleanonPreferenceTreeClick(PreferenceScreenpreferenceScreen,
  75. Preferencepreference){
  76. if(preference==mMmsSoundsPref){
  77. doPickSmsRingtone();
  78. }
  79. elseif(preference==mPhoneSoundsPref){
  80. doPickPhoneRingtone();
  81. }
  82. elseif(preference==mAlarmSoundsPref){
  83. doPickAlarmRingtone();
  84. }
  85. elseif(preference==mSdcardSoundsPref){
  86. doPickSdcardRingtone();
  87. }
  88. returnsuper.onPreferenceTreeClick(preferenceScreen,preference);
  89. }
  90. privatevoiddoPickSmsRingtone(){
  91. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  92. notificationStr=sharedPreferences.getString(NOTIFICATION_RINGTONE,null);
  93. Intentintent=newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);
  94. //Allowusertopick'Default'
  95. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,true);
  96. //Showonlyringtones
  97. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_NOTIFICATION);
  98. //setthedefaultNotificationvalue
  99. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION));
  100. //Don'tshow'Silent'
  101. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT,true);
  102. UrinotificationUri;
  103. if(notificationStr!=null){
  104. notificationUri=Uri.parse(notificationStr);
  105. //Putcheckmarknexttothecurrentringtoneforthiscontact
  106. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,notificationUri);
  107. }else{
  108. //OtherwisepickdefaultringtoneUrisothatsomethingisselected.
  109. notificationUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
  110. //Putcheckmarknexttothecurrentringtoneforthiscontact
  111. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,notificationUri);
  112. }
  113. //Launch!
  114. startActivityForResult(intent,SMS_RINGTONE_PICKED);
  115. }
  116. privatevoiddoPickPhoneRingtone(){
  117. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  118. phoneStr=sharedPreferences.getString(PHONE_RINGTONE,null);
  119. Intentintent=newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);
  120. //Allowusertopick'Default'
  121. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,true);
  122. //Showonlyringtones
  123. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_RINGTONE);
  124. //setthedefaultNotificationvalue
  125. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE));
  126. //Don'tshow'Silent'
  127. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT,true);
  128. UriphoneUri;
  129. if(phoneStr!=null){
  130. phoneUri=Uri.parse(phoneStr);
  131. //Putcheckmarknexttothecurrentringtoneforthiscontact
  132. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,phoneUri);
  133. }else{
  134. //OtherwisepickdefaultringtoneUrisothatsomethingisselected.
  135. phoneUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE);
  136. //Putcheckmarknexttothecurrentringtoneforthiscontact
  137. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,phoneUri);
  138. }
  139. startActivityForResult(intent,PHONE_RINGTONE_PICKED);
  140. }
  141. privatevoiddoPickAlarmRingtone(){
  142. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  143. alarmStr=sharedPreferences.getString(ALARM_RINGTONE,null);
  144. Intentintent=newIntent(RingtoneManager.ACTION_RINGTONE_PICKER);
  145. //Allowusertopick'Default'
  146. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT,true);
  147. //Showonlyringtones
  148. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE,RingtoneManager.TYPE_ALARM);
  149. //setthedefaultNotificationvalue
  150. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI,RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM));
  151. //Don'tshow'Silent'
  152. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT,true);
  153. UrialarmUri;
  154. if(alarmStr!=null){
  155. alarmUri=Uri.parse(alarmStr);
  156. //Putcheckmarknexttothecurrentringtoneforthiscontact
  157. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,alarmUri);
  158. }else{
  159. //OtherwisepickdefaultringtoneUrisothatsomethingisselected.
  160. alarmUri=RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
  161. //Putcheckmarknexttothecurrentringtoneforthiscontact
  162. intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,alarmUri);
  163. }
  164. startActivityForResult(intent,ALARM_RINGTONE_PICKED);
  165. }
  166. privatevoiddoPickSdcardRingtone(){
  167. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  168. sdcardStr=sharedPreferences.getString(SDCARD_RINGTONE,null);
  169. UrisdcardUri=null;
  170. if(sdcardStr!=null){
  171. sdcardUri=Uri.parse(sdcardStr);
  172. }
  173. IntentinnerIntent=newIntent(Intent.ACTION_GET_CONTENT);
  174. innerIntent.setType("audio/*");
  175. //youcouldlookuptheframeworkthetypeofaudio,ifyoudon`twantusetheRecorderusethenotecode
  176. //innerIntent.setType("audio/aac");
  177. //innerIntent.setType("audio/mp3");
  178. //innerIntent.setType("audio/midi");
  179. //Putcheckmarknexttothecurrentringtoneforthiscontact
  180. innerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI,sdcardUri);
  181. IntentwrapperIntent=Intent.createChooser(innerIntent,null);
  182. startActivityForResult(wrapperIntent,SDCARD_RINGTONE_PICKED);
  183. }
  184. @Override
  185. protectedvoidonResume(){
  186. setDefaultPreferences();
  187. super.onResume();
  188. }
  189. @Override
  190. protectedvoidonActivityResult(intrequestCode,intresultCode,Intentdata){
  191. SharedPreferencessharedPreferences=PreferenceManager.getDefaultSharedPreferences(this);
  192. SharedPreferences.Editoreditor=sharedPreferences.edit();
  193. if(resultCode!=RESULT_OK){
  194. return;
  195. }
  196. switch(requestCode){
  197. caseSMS_RINGTONE_PICKED:{
  198. UripickedUri=data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
  199. if(null==pickedUri){
  200. editor.putString(NOTIFICATION_RINGTONE_TITLE_NAME,getString(R.string.select_ringtone_slient));
  201. editor.putString(NOTIFICATION_RINGTONE,null);
  202. editor.commit();
  203. }else{
  204. Ringtoneringtone=RingtoneManager.getRingtone(SoundSettingMainActivity.this,pickedUri);
  205. StringstrRingtone=ringtone.getTitle(SoundSettingMainActivity.this);
  206. editor.putString(NOTIFICATION_RINGTONE_TITLE_NAME,strRingtone);
  207. editor.putString(NOTIFICATION_RINGTONE,pickedUri.toString());
  208. editor.commit();
  209. }
  210. break;
  211. }
  212. casePHONE_RINGTONE_PICKED:{
  213. UripickedUri=data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
  214. if(null==pickedUri){
  215. editor.putString(PHONE_RINGTONE_TITLE_NAME,getString(R.string.select_ringtone_slient));
  216. editor.putString(PHONE_RINGTONE,null);
  217. editor.commit();
  218. }else{
  219. phoneStr=pickedUri.toString();
  220. Ringtoneringtone=RingtoneManager.getRingtone(SoundSettingMainActivity.this,pickedUri);
  221. StringstrRingtone=ringtone.getTitle(SoundSettingMainActivity.this);
  222. editor.putString(PHONE_RINGTONE_TITLE_NAME,strRingtone);
  223. editor.putString(PHONE_RINGTONE,pickedUri.toString());
  224. editor.commit();
  225. }
  226. break;
  227. }
  228. caseALARM_RINGTONE_PICKED:{
  229. UripickedUri=data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
  230. if(null==pickedUri){
  231. editor.putString(ALARM_RINGTONE_TITLE_NAME,getString(R.string.select_ringtone_slient));
  232. editor.putString(ALARM_RINGTONE,null);
  233. editor.commit();
  234. }else{
  235. Ringtoneringtone=RingtoneManager.getRingtone(SoundSettingMainActivity.this,pickedUri);
  236. StringstrRingtone=ringtone.getTitle(SoundSettingMainActivity.this);
  237. editor.putString(ALARM_RINGTONE_TITLE_NAME,strRingtone);
  238. editor.putString(ALARM_RINGTONE,pickedUri.toString());
  239. editor.commit();
  240. }
  241. break;
  242. }
  243. caseSDCARD_RINGTONE_PICKED:{
  244. UripickedUri=data.getData();
  245. if(null!=pickedUri){
  246. notificationStr=pickedUri.toString();
  247. Ringtoneringtone=RingtoneManager.getRingtone(SoundSettingMainActivity.this,pickedUri);
  248. StringstrRingtone=ringtone.getTitle(SoundSettingMainActivity.this);
  249. editor.putString(SDCARD_RINGTONE_TITLE_NAME,strRingtone);
  250. editor.putString(SDCARD_RINGTONE,pickedUri.toString());
  251. editor.commit();
  252. }
  253. break;
  254. }
  255. default:break;
  256. }
  257. }
  258. }</span>
<span style="font-size:16px;color:#000000;">package com.cn.android.daming; import android.content.Intent; import android.content.SharedPreferences; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceManager; import android.preference.PreferenceScreen; public class SoundSettingMainActivity extends PreferenceActivity { private static final int SMS_RINGTONE_PICKED = 1; private static final int PHONE_RINGTONE_PICKED = 2; private static final int ALARM_RINGTONE_PICKED = 3; private static final int SDCARD_RINGTONE_PICKED = 4; public static final String NOTIFICATION_RINGTONE = "pref_notification_ringtone"; public static final String NOTIFICATION_RINGTONE_TITLE_NAME = "pref_notification_ringtone_name"; public static final String PHONE_RINGTONE = "pref_phone_ringtone"; public static final String PHONE_RINGTONE_TITLE_NAME = "pref_phone_ringtone_title_name"; public static final String ALARM_RINGTONE = "pref_alarm_ringtone"; public static final String ALARM_RINGTONE_TITLE_NAME = "pref_alarm_ringtone_title_name"; public static final String SDCARD_RINGTONE = "pref_sdcard_ringtone"; public static final String SDCARD_RINGTONE_TITLE_NAME = "pref_sdcard_ringtone_title_name"; private String notificationStr; private String phoneStr; private String alarmStr; private String sdcardStr; private Preference mMmsSoundsPref; private Preference mPhoneSoundsPref; private Preference mAlarmSoundsPref; private Preference mSdcardSoundsPref; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); setMessagePreferences(); setDefaultPreferences(); } private void setMessagePreferences() { mMmsSoundsPref = findPreference("pref_sms_ringtone"); mPhoneSoundsPref = findPreference("pref_phone_ringtone"); mAlarmSoundsPref = findPreference("pref_alarm_ringtone"); mSdcardSoundsPref = findPreference("pref_sdcard_ringtone"); } private void setDefaultPreferences(){ SharedPreferences innersharedPreferences = PreferenceManager.getDefaultSharedPreferences(SoundSettingMainActivity.this); String notificationRingtoneTitleName = innersharedPreferences.getString(NOTIFICATION_RINGTONE_TITLE_NAME, null); if(notificationRingtoneTitleName!=null){ mMmsSoundsPref.setSummary(notificationRingtoneTitleName); }else{ mMmsSoundsPref.setSummary(getString(R.string.pref_summary_notification_ringtone)); } String phoneRingtoneTitleName = innersharedPreferences.getString(PHONE_RINGTONE_TITLE_NAME, null); if(phoneRingtoneTitleName!=null){ mPhoneSoundsPref.setSummary(phoneRingtoneTitleName); }else{ mPhoneSoundsPref.setSummary(getString(R.string.pref_summary_phone_ringtone)); } String alarmRingtoneTitleName = innersharedPreferences.getString(ALARM_RINGTONE_TITLE_NAME, null); if(alarmRingtoneTitleName!=null){ mAlarmSoundsPref.setSummary(alarmRingtoneTitleName); }else{ mAlarmSoundsPref.setSummary(getString(R.string.pref_summary_alarm_ringtone)); } String sdcardRingtoneTitleName = innersharedPreferences.getString(SDCARD_RINGTONE_TITLE_NAME, null); if(sdcardRingtoneTitleName!=null){ mSdcardSoundsPref.setSummary(sdcardRingtoneTitleName); }else{ mSdcardSoundsPref.setSummary(getString(R.string.pref_summary_sdcard_ringtone)); } } @Override public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) { if (preference == mMmsSoundsPref){ doPickSmsRingtone(); } else if(preference == mPhoneSoundsPref){ doPickPhoneRingtone(); } else if(preference == mAlarmSoundsPref){ doPickAlarmRingtone(); } else if(preference == mSdcardSoundsPref){ doPickSdcardRingtone(); } return super.onPreferenceTreeClick(preferenceScreen, preference); } private void doPickSmsRingtone(){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); notificationStr = sharedPreferences.getString(NOTIFICATION_RINGTONE, null); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); // Allow user to pick 'Default' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); // Show only ringtones intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION); //set the default Notification value intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION)); // Don't show 'Silent' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); Uri notificationUri; if (notificationStr != null) { notificationUri = Uri.parse(notificationStr); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, notificationUri); } else { // Otherwise pick default ringtone Uri so that something is selected. notificationUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, notificationUri); } // Launch! startActivityForResult(intent, SMS_RINGTONE_PICKED); } private void doPickPhoneRingtone(){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); phoneStr = sharedPreferences.getString(PHONE_RINGTONE, null); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); // Allow user to pick 'Default' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); // Show only ringtones intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE); //set the default Notification value intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE)); // Don't show 'Silent' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); Uri phoneUri; if (phoneStr != null) { phoneUri = Uri.parse(phoneStr); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, phoneUri); } else { // Otherwise pick default ringtone Uri so that something is selected. phoneUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_RINGTONE); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, phoneUri); } startActivityForResult(intent, PHONE_RINGTONE_PICKED); } private void doPickAlarmRingtone(){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); alarmStr = sharedPreferences.getString(ALARM_RINGTONE, null); Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER); // Allow user to pick 'Default' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, true); // Show only ringtones intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM); //set the default Notification value intent.putExtra(RingtoneManager.EXTRA_RINGTONE_DEFAULT_URI, RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM)); // Don't show 'Silent' intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true); Uri alarmUri; if (alarmStr != null) { alarmUri = Uri.parse(alarmStr); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, alarmUri); } else { // Otherwise pick default ringtone Uri so that something is selected. alarmUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM); // Put checkmark next to the current ringtone for this contact intent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, alarmUri); } startActivityForResult(intent, ALARM_RINGTONE_PICKED); } private void doPickSdcardRingtone(){ SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); sdcardStr = sharedPreferences.getString(SDCARD_RINGTONE, null); Uri sdcardUri = null; if (sdcardStr != null) { sdcardUri = Uri.parse(sdcardStr); } Intent innerIntent = new Intent(Intent.ACTION_GET_CONTENT); innerIntent.setType("audio/*"); //you could lookup the framework the type of audio,if you don`t want use the Recorder use the note code // innerIntent.setType("audio/aac"); // innerIntent.setType("audio/mp3"); // innerIntent.setType("audio/midi"); // Put checkmark next to the current ringtone for this contact innerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, sdcardUri); Intent wrapperIntent = Intent.createChooser(innerIntent, null); startActivityForResult(wrapperIntent, SDCARD_RINGTONE_PICKED); } @Override protected void onResume() { setDefaultPreferences(); super.onResume(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this); SharedPreferences.Editor editor = sharedPreferences.edit(); if (resultCode != RESULT_OK) { return; } switch (requestCode) { case SMS_RINGTONE_PICKED:{ Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if(null == pickedUri){ editor.putString(NOTIFICATION_RINGTONE_TITLE_NAME, getString(R.string.select_ringtone_slient)); editor.putString(NOTIFICATION_RINGTONE, null); editor.commit(); }else{ Ringtone ringtone = RingtoneManager.getRingtone(SoundSettingMainActivity.this, pickedUri); String strRingtone = ringtone.getTitle(SoundSettingMainActivity.this); editor.putString(NOTIFICATION_RINGTONE_TITLE_NAME, strRingtone); editor.putString(NOTIFICATION_RINGTONE, pickedUri.toString()); editor.commit(); } break; } case PHONE_RINGTONE_PICKED:{ Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if(null == pickedUri){ editor.putString(PHONE_RINGTONE_TITLE_NAME, getString(R.string.select_ringtone_slient)); editor.putString(PHONE_RINGTONE, null); editor.commit(); }else{ phoneStr = pickedUri.toString(); Ringtone ringtone = RingtoneManager.getRingtone(SoundSettingMainActivity.this, pickedUri); String strRingtone = ringtone.getTitle(SoundSettingMainActivity.this); editor.putString(PHONE_RINGTONE_TITLE_NAME, strRingtone); editor.putString(PHONE_RINGTONE, pickedUri.toString()); editor.commit(); } break; } case ALARM_RINGTONE_PICKED:{ Uri pickedUri = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI); if(null == pickedUri){ editor.putString(ALARM_RINGTONE_TITLE_NAME, getString(R.string.select_ringtone_slient)); editor.putString(ALARM_RINGTONE, null); editor.commit(); }else{ Ringtone ringtone = RingtoneManager.getRingtone(SoundSettingMainActivity.this, pickedUri); String strRingtone = ringtone.getTitle(SoundSettingMainActivity.this); editor.putString(ALARM_RINGTONE_TITLE_NAME, strRingtone); editor.putString(ALARM_RINGTONE, pickedUri.toString()); editor.commit(); } break; } case SDCARD_RINGTONE_PICKED:{ Uri pickedUri = data.getData(); if(null != pickedUri){ notificationStr = pickedUri.toString(); Ringtone ringtone = RingtoneManager.getRingtone(SoundSettingMainActivity.this, pickedUri); String strRingtone = ringtone.getTitle(SoundSettingMainActivity.this); editor.putString(SDCARD_RINGTONE_TITLE_NAME, strRingtone); editor.putString(SDCARD_RINGTONE, pickedUri.toString()); editor.commit(); } break; } default:break; } } }</span>

二、在res目录下加入xml文件,加入preferences.xml中的代码:

  1. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android">
  4. <PreferenceCategoryandroid:title="@string/pref_sounds_storage_title"
  5. android:key="pref_key_storage_settings">
  6. <Preference
  7. android:layout="?android:attr/preferenceLayoutChild"
  8. android:key="pref_sms_ringtone"
  9. android:ringtoneType="notification"
  10. android:title="@string/pref_title_notification_ringtone"
  11. android:summary="@string/pref_summary_notification_ringtone"
  12. />
  13. <Preference
  14. android:layout="?android:attr/preferenceLayoutChild"
  15. android:key="pref_phone_ringtone"
  16. android:ringtoneType="notification"
  17. android:title="@string/pref_title_phone_ringtone"
  18. android:summary="@string/pref_summary_phone_ringtone"
  19. />
  20. <Preference
  21. android:layout="?android:attr/preferenceLayoutChild"
  22. android:key="pref_alarm_ringtone"
  23. android:ringtoneType="notification"
  24. android:title="@string/pref_title_alarm_ringtone"
  25. android:summary="@string/pref_summary_alarm_ringtone"
  26. />
  27. <Preference
  28. android:layout="?android:attr/preferenceLayoutChild"
  29. android:key="pref_sdcard_ringtone"
  30. android:ringtoneType="notification"
  31. android:title="@string/pref_title_sdcard_ringtone"
  32. android:summary="@string/pref_summary_sdcard_ringtone"
  33. />
  34. </PreferenceCategory>
  35. </PreferenceScreen>
  36. </span>
<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> <PreferenceCategory android:title="@string/pref_sounds_storage_title" android:key="pref_key_storage_settings"> <Preference android:layout="?android:attr/preferenceLayoutChild" android:key="pref_sms_ringtone" android:ringtoneType="notification" android:title="@string/pref_title_notification_ringtone" android:summary="@string/pref_summary_notification_ringtone" /> <Preference android:layout="?android:attr/preferenceLayoutChild" android:key="pref_phone_ringtone" android:ringtoneType="notification" android:title="@string/pref_title_phone_ringtone" android:summary="@string/pref_summary_phone_ringtone" /> <Preference android:layout="?android:attr/preferenceLayoutChild" android:key="pref_alarm_ringtone" android:ringtoneType="notification" android:title="@string/pref_title_alarm_ringtone" android:summary="@string/pref_summary_alarm_ringtone" /> <Preference android:layout="?android:attr/preferenceLayoutChild" android:key="pref_sdcard_ringtone" android:ringtoneType="notification" android:title="@string/pref_title_sdcard_ringtone" android:summary="@string/pref_summary_sdcard_ringtone" /> </PreferenceCategory> </PreferenceScreen> </span>

三、在values目录下的string中的代码:

  1. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <stringname="hello">HelloWorld,SoundSettingMainActivity!</string>
  4. <stringname="app_name">SoundSettingApp</string>
  5. <stringname="pref_sounds_storage_title">大明原创SoundSetting</string>
  6. <stringname="pref_title_notification_ringtone">选择短信铃声</string>
  7. <stringname="pref_summary_notification_ringtone">默认短信铃声</string>
  8. <stringname="pref_title_phone_ringtone">选择手机铃声</string>
  9. <stringname="pref_summary_phone_ringtone">默认手机铃声</string>
  10. <stringname="pref_title_alarm_ringtone">选择闹钟铃声</string>
  11. <stringname="pref_summary_alarm_ringtone">默认闹钟铃声</string>
  12. <stringname="pref_title_sdcard_ringtone">选择Sdcard中的铃声</string>
  13. <stringname="pref_summary_sdcard_ringtone">默认Sdcard铃声</string>
  14. <stringname="select_ringtone_slient">静音</string>
  15. </resources>
  16. </span>
<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, SoundSettingMainActivity!</string> <string name="app_name">SoundSettingApp</string> <string name="pref_sounds_storage_title">大明原创SoundSetting</string> <string name="pref_title_notification_ringtone">选择短信铃声</string> <string name="pref_summary_notification_ringtone">默认短信铃声</string> <string name="pref_title_phone_ringtone">选择手机铃声</string> <string name="pref_summary_phone_ringtone">默认手机铃声</string> <string name="pref_title_alarm_ringtone">选择闹钟铃声</string> <string name="pref_summary_alarm_ringtone">默认闹钟铃声</string> <string name="pref_title_sdcard_ringtone">选择Sdcard中的铃声</string> <string name="pref_summary_sdcard_ringtone">默认Sdcard铃声</string> <string name="select_ringtone_slient">静音</string> </resources> </span>


四、在AndroidManifest.xml中的代码:

  1. <spanstyle="font-size:16px;"><?xmlversion="1.0"encoding="utf-8"?>
  2. <manifestxmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.cn.android.daming"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdkandroid:minSdkVersion="8"/>
  7. <applicationandroid:icon="@drawable/icon"android:label="@string/app_name">
  8. <activityandroid:name=".SoundSettingMainActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <actionandroid:name="android.intent.action.MAIN"/>
  12. <categoryandroid:name="android.intent.category.LAUNCHER"/>
  13. </intent-filter>
  14. </activity>
  15. </application>完整代码下载地址:http://download.****.net/detail/wdaming1986/3736718
<span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cn.android.daming" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".SoundSettingMainActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest></span>