2019独角兽企业重金招聘Python工程师标准>>>

  最近研究源码程序,改了改手机短信铃声的源码,总结了下铃声的代码,写个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和音乐的名字整个流程大概就是这样。
点击此处下载源码



  下面代码附上:

  在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;@Overridepublic 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));}}@Overridepublic 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 ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_NOTIFICATION);//set the default Notification valueintent.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 contactintent.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 contactintent.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 ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);//set the default Notification valueintent.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 contactintent.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 contactintent.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 ringtonesintent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_ALARM);//set the default Notification valueintent.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 contactintent.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 contactintent.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 contactinnerIntent.putExtra(RingtoneManager.EXTRA_RINGTONE_EXISTING_URI, sdcardUri);Intent wrapperIntent = Intent.createChooser(innerIntent, null);startActivityForResult(wrapperIntent, SDCARD_RINGTONE_PICKED);}@Overrideprotected void onResume() {setDefaultPreferences();super.onResume();}@Overrideprotected 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>

转载:http://www.adobex.com/android/source/details/00000215.htm

转载于:https://my.oschina.net/androidcode/blog/104140

Android中铃声总结源码相关推荐

  1. Android中ICS4.0源码Launcher启动流程分析【android源码Launcher系列一】

    最近研究ICS4.0的Launcher,发现4.0和2.3有稍微点区别,但是区别不是特别大,所以我就先整理一下Launcher启动的大致流程.Launcher其实是贯彻于手机的整个系统的,时时刻刻都在 ...

  2. java toast_详解Android中的Toast源码

    Toast源码实现 Toast入口    我们在应用中使用Toast提示的时候,一般都是一行简单的代码调用,如下所示: [java] view plaincopyprint?在CODE上查看代码片派生 ...

  3. 如何更改java源码_java – 如何在Android中更改ImageView源码

    这是我的xml,它位于我活动中出现的片段上. android:id="@+id/frame1" android:layout_width="wrap_content&qu ...

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

    最近研究源码程序,改了改手机短信铃声的源码,最近总结了下铃声的代码,写个activity继承PreferenceActivity有:手机短信铃声,手机铃声,闹钟铃声,还有sdcard中的铃声,通过选择 ...

  5. Android 在WebView中获取网页源码

    原文链接:http://www.cnblogs.com/hibraincol/archive/2011/10/26/2224866.html 1. 使能javascript: 1 webView.ge ...

  6. Android: 在WebView中获取网页源码

    1. 使能javascript: ? 1 webView.getSettings().setJavaScriptEnabled(true); 2. 编写本地接口 ? 1 2 3 4 5 finalcl ...

  7. 搭建直播平台过程中Android端直播APP源码是如何实现连麦功能的?

    直播平台强大的变现能力是大家有目共睹的,很多开发商在搭建直播平台时为了增加用户黏性,纷纷将直播中加入连麦功能. 目前市场上通用的有两种连麦方案:本地混流和云端混流.本地混流即主播和连麦观众分别推一路流 ...

  8. Android短视频系统源码功能测试(个人总结完整版)

    Android短视频系统源码功能测试包含短视频系统源码的安装卸载测试,界面测试,业务功能测试,短视频系统源码特性测试,交叉事件测试,兼容性测试,升级更新测试,消息通知测试,功能键测试,手势测试等 1- ...

  9. 【转】Ubuntu 14.04.3上配置并成功编译Android 6.0 r1源码

    http://www.linuxidc.com/Linux/2016-01/127292.htm 终于成功把Android 6.0 r1源码的源码编译.先上图,这是在Ubuntu中运行的Android ...

  10. android 修改编译内核源码 对抗反调试

    0×00  写在前面 攻防对立.程序调试与反调试之间的对抗是一个永恒的主题.在安卓逆向工程实践中,通过修改和编译安卓内核源码来对抗反调试是一种常见的方法.但网上关于此类的资料比较少,且都是基于AOSP ...

最新文章

  1. AttributeError: ‘Series‘ object has no attribute ‘as_matrix‘
  2. 宜春学院计算机专业好就业吗,宜春学院毕业生良好就业前景的背后
  3. vue.js根据数据循环生成表格_vue.js循环for(列表渲染)详解
  4. CSP - J 2020 T1 优秀的拆分
  5. C++|Linux工作笔记-C++获取Linux中shell命令结果
  6. 物联网专科专业必修课程_江西自考专科物联网技术专业的考试课程/科目
  7. 【MySQL】mysql中any,in,some,all的区别
  8. 信息安全工程师 学习笔记 完结
  9. 省钱兄(APP、h5版本)任务悬赏点赞平台uniapp前端源码模板
  10. ARKit玩起来 - AR预览唱片-史小川-专题视频课程
  11. 华三路由交换配置命令_h3c路由器配置命令
  12. 推荐系统中的前沿技术研究与落地:深度学习、AutoML与强化学习 | AI ProCon 2019
  13. 机器人领域 期刊与会议
  14. Pandas汇总不同excel工作簿中的表格并合并同类数据
  15. 少林寺公布武功秘籍 揭开绝技神秘面纱(组图)
  16. 怎么做硬件产品的需求分析?
  17. 通信网与计算机网络的区别,计算机网络和计算机通信网络之间的本质区别是什么?...
  18. linux ls和cd命令详解,Linux基础cd、pwd和ls命令
  19. 【路径规划-TSP问题】基于蚁群算法求解旅行商问题含Matlab代码
  20. android studio CreateProcess error=2, 系统找不到指定的文件。

热门文章

  1. android 弹窗banner,弹窗交互规范分析|UI|观点|DesigneR_D - 原创文章 - 站酷 (ZCOOL)
  2. 2021父亲节祝福话语、海报素材大全
  3. wps怎么关闭修改痕迹_WPS文字中如何保留修改痕迹
  4. java实训心得_大学java实训心得总结(共9篇).docx
  5. 【图像分割】基于直觉模糊C均值聚类实现图像分割IFCMmatlab代码
  6. Vue引入Froala-Editor富文本编辑器
  7. 使用iTunes 12.7 可以直接安装ipa安装包
  8. 基于jqUI的日期选择(‘yy-mm-dd’)
  9. C++ primer 5th 习题之10.13
  10. 思科CCNA认证视频 CCNA视频 ccna最新题库 专题 复习资料