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

文章出处:http://blog.csdn.net/wdaming1986/article/details/6919653

  

         最近研究源码程序,改了改手机短信铃声的源码,最近总结了下铃声的代码,写个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.csdn.net/wdaming1986/article/details/6919653

 

                下面请看截图:             

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

                                                             

 

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

                                                    

 

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

                                                    

 

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

                                                            

 

下面代码附上:

在SoundSettingActivity这个工程下面:

 

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

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

 

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

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <PreferenceScreen  
  3.   xmlns:android="http://schemas.android.com/apk/res/android">  
  4.       <PreferenceCategory android: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>  

 

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

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


 

四、在AndroidManifest.xml中的代码:

[html]  view plain copy print ?
  1. <span style="font-size:16px;"><?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns: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-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".SoundSettingMainActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.     </application>  
  17. </manifest></span>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值