最近研究源码程序,改了改手机短信铃声的源码,最近总结了下铃声的代码,写个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类中的代码:

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

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

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

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

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

四、在AndroidManifest.xml中的代码:

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

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

Android中铃声总结【安卓源码解析一】相关推荐

  1. Android多线程之ArrayBlockingQueue源码解析

    阻塞队列系列 Android多线程之LinkedBlockingQueue源码解析 Android多线程之SynchronousQueue源码解析 Andorid多线程之DelayQueue源码分析 ...

  2. Android多线程之IntentService源码解析

    想要了解 IntentService 的工作原理需要先对 Android 系统中以 Handler.Looper.MessageQueue 组成的异步消息处理机制以及 HandlerThread 有所 ...

  3. Android Glide 3.7.0 源码解析(八) , RecyclableBufferedInputStream 的 mark/reset 实现

    个人博客传送门 一.mark / reset 的作用 Android Glide 3.7.0 源码解析(七) , 细说图形变换和解码有提到过RecyclableBufferedInputStream ...

  4. 【Android应用开发】EasyDialog 源码解析

    示例源码下载 : http://download.csdn.net/detail/han1202012/9115227 EasyDialog 简介 : -- 作用 : 用于在界面进行一些介绍, 说明; ...

  5. 【源码篇】安卓源码解析(持续总结)

    前言 在Linux中,一切皆为文件.安卓底层也是基于Linux开发的. 在我们了解源码目录的时候,我们要先知道这些源码的后缀名代表的是什么文件,什么意思,才能更好的去了解其中的原理. 文章目录 前言 ...

  6. 【Android 控件使用及源码解析】 GridView规则显示图片仿微信朋友圈发图片

    今天闲下来想用心写一点东西,发现没什么可写的,就写一下最近项目上用到的一些东西吧.最近项目要求上传多图并且多图显示,而且要规则的显示,就像微信朋友圈的图片显示一样. 想了一下用GridView再适合不 ...

  7. java if中的continue_java中break和continue源码解析

    在自己学习java语言的过程中,很容易把break和continue的用法混淆.为了便于以后快速查阅及温习,在此特留学习笔记一份. 简述 在任何迭代语句的主体部分,都可以用break和continue ...

  8. Android海外应用和安卓源码的多国语言翻译

    前言:目前翻译只有baidu能直接进行调用,但是baidu支持的语言太少,无法支持对google play下发布的海外应用要求的多个翻译, 而且网上说的Android Studio的翻译插件,经过体验 ...

  9. Android 网络框架之Retrofit源码解析,flutter边框特效

    Retrofit的构建使用了建造者模式,这个模式的优点就是可以构造复杂的对象,方便扩展,并且看起来代码比较简洁,美观: 在开始之前,我们先来看一下Retrofit的成员变量: 这里的变量并不是很多,我 ...

最新文章

  1. Redis6安装配置集群cluster以及集群宕机注意事项
  2. CXF wsdl2java 生成java代码供客户端使用
  3. python教程app下载地址_python教程
  4. Linux Kernel TCP/IP Stack — L3 Layer — netfilter/iptables 防火墙
  5. Angular 一个简单的指令实现 阻止事件扩散
  6. sql-C#-类型对应
  7. python os path isfile_Python path.isfile方法代码示例
  8. 制作不同方向的三角形(border属性)
  9. HTML5实现扫描识别二维码/生成二维码
  10. “陆奇争夺战”:江湖传言,得陆奇者得AI天下。
  11. grub.conf解析
  12. 来自TMDB的5000部电影数据集
  13. 数据库安全关键技术之数据库脱敏技术详解
  14. 工作流:一文让你学会使用flowable工作流
  15. 【电脑使用】Windows 10账户那些事儿
  16. 入手评测华为nova10和vivoS15pro选哪个
  17. 计算机培训考试内容,计算机等级考试的科目和内容解析
  18. 布袋除尘器过滤风速多少_袋式除尘器过滤风速一般多大
  19. IPEmotion数据处理-分类元件的灵活运用——车辆制动次数的多样计算
  20. win11找不到恢复环境怎么恢复出厂设置

热门文章

  1. python 3 4j不是合法的_3 4j 是合法Python数字类型。
  2. AI医学影像千亿长坡,“医疗AI第一股”鹰瞳科技为何能滚起雪球?
  3. UL1007 UL1015与AWG
  4. 【个人记录 | 研二预答辩】
  5. 组卷与考试系统_题库添加选择题模块
  6. OpenCV开发笔记(四十八):红胖子8分钟带你深入了解直方图均衡化(图文并茂+浅显易懂+程序源码)
  7. 三次握手的过程、四次挥手、为什么要进行第三次握手、为什么要进行四次挥手
  8. 精进之路-day01
  9. ubuntu下网易云音乐无法打开
  10. Pro Android学习笔记(一五五) 传感器(5) 磁场传感器和方位(上)