Android中鈴聲總結【安卓源碼解析一】 – Android移動開發技術文章_手機開發 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.csdn.net/wdaming1986/article/details/6919653

 

                下面請看截圖:            

                            第一次打開程序的界面:                                       點擊“選擇短信鈴聲”後的界面:

                                                                                                  

 

選擇鈴聲的dialog後的界面:                                 點擊“選擇手機鈴聲”後的界面:

                                                    

 

                             點擊“選擇手機鈴聲”後的界面:                      點擊“選擇鬧鐘鈴聲”後的dialog界面:

                                                    

 

                     點擊“選擇sdcard中的鈴聲”後的界面:             點擊“選擇曲目”後彈出sdcard的界面:

                                                       

 

 

 

     下面代碼附上:

 

 

在SoundSettingActivity這個工程下面:

 

一、在com.cn.android.daming包的SoundSettingMainActivity.java類中的代碼:

 

<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中的代碼:

 

<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中的代碼:

 

<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中的代碼:

view plainprint?

<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> 

 

 完整代碼下載地址:http://download.csdn.net/detail/wdaming1986/3736718

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。