android – 在首选项中,选择我的声音就像铃声优先

前端之家收集整理的这篇文章主要介绍了android – 在首选项中,选择我的声音就像铃声优先前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的/ raw文件夹中有声音,我希望我的用户能够选择一个声音,就像“铃声优先”一样,但只有我的声音.

解决方法

这里我的铃声优先替换.
列出所有系统铃声和您的自定义铃声(以xml定义,存储在res / raw中)

ExtraRingtonePreference.java

  1. package de.almisoft.test;
  2.  
  3. import java.util.Arrays;
  4. import java.util.LinkedHashMap;
  5. import java.util.Map;
  6. import java.util.TreeMap;
  7.  
  8. import de.almisoft.test.R;
  9.  
  10. import android.app.AlertDialog.Builder;
  11. import android.content.Context;
  12. import android.content.DialogInterface;
  13. import android.content.res.TypedArray;
  14. import android.database.Cursor;
  15. import android.media.Ringtone;
  16. import android.media.RingtoneManager;
  17. import android.net.Uri;
  18. import android.preference.DialogPreference;
  19. import android.util.AttributeSet;
  20.  
  21. public class ExtraRingtonePreference extends DialogPreference {
  22.  
  23. private Context mContext;
  24. private String mValue;
  25. private Ringtone ringtone;
  26. private int mRingtoneType;
  27. private boolean mShowSilent;
  28. private boolean mShowDefault;
  29. private CharSequence[] mExtraRingtones;
  30. private CharSequence[] mExtraRingtoneTitles;
  31.  
  32. public ExtraRingtonePreference(Context context,AttributeSet attrs) {
  33.  
  34. super(context,attrs);
  35.  
  36. mContext = context;
  37.  
  38. final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ExtraRingtonePreference,0);
  39.  
  40. mRingtoneType = a.getInt(R.styleable.ExtraRingtonePreference_ringtoneType,RingtoneManager.TYPE_RINGTONE);
  41. mShowDefault = a.getBoolean(R.styleable.ExtraRingtonePreference_showDefault,true);
  42. mShowSilent = a.getBoolean(R.styleable.ExtraRingtonePreference_showSilent,true);
  43. mExtraRingtones = a.getTextArray(R.styleable.ExtraRingtonePreference_extraRingtones);
  44. mExtraRingtoneTitles = a.getTextArray(R.styleable.ExtraRingtonePreference_extraRingtoneTitles);
  45.  
  46. a.recycle();
  47. }
  48.  
  49. public ExtraRingtonePreference(Context context) {
  50. this(context,null);
  51. }
  52.  
  53. public String getValue() {
  54. return mValue;
  55. }
  56.  
  57. private Map<String,Uri> getSounds(int type) {
  58.  
  59. RingtoneManager ringtoneManager = new RingtoneManager(mContext);
  60. ringtoneManager.setType(type);
  61. Cursor cursor = ringtoneManager.getCursor();
  62.  
  63. Map<String,Uri> list = new TreeMap<String,Uri>();
  64. while (cursor.moveToNext()) {
  65. String notificationTitle = cursor.getString(RingtoneManager.TITLE_COLUMN_INDEX);
  66. Uri notificationUri = ringtoneManager.getRingtoneUri(cursor.getPosition());
  67.  
  68. list.put(notificationTitle,notificationUri);
  69. }
  70.  
  71. return list;
  72. }
  73.  
  74. private Uri uriFromRaw(String name) {
  75. int resId = mContext.getResources().getIdentifier(name,"raw",mContext.getPackageName());
  76. return Uri.parse("android.resource://" + mContext.getPackageName() + "/" + resId);
  77. }
  78.  
  79. private String getExtraRingtoneTitle(CharSequence name) {
  80. if (mExtraRingtones != null && mExtraRingtoneTitles != null) {
  81. int index = Arrays.asList(mExtraRingtones).indexOf(name);
  82. return mExtraRingtoneTitles[index].toString();
  83. }
  84.  
  85. return null;
  86. }
  87.  
  88. @Override
  89. public CharSequence getSummary() {
  90.  
  91. String ringtoneTitle = null;
  92.  
  93. if (mValue != null) {
  94.  
  95. if (mValue.length() == 0)
  96. ringtoneTitle = mContext.getString(R.string.silent);
  97.  
  98. if (ringtoneTitle == null && mExtraRingtones != null && mExtraRingtoneTitles != null) {
  99.  
  100. for (int i = 0; i < mExtraRingtones.length; i++) {
  101.  
  102. Uri uriExtra = uriFromRaw(mExtraRingtones[i].toString());
  103.  
  104. if (uriExtra.equals(Uri.parse(mValue))) {
  105. ringtoneTitle = mExtraRingtoneTitles[i].toString();
  106. break;
  107. }
  108. }
  109. }
  110.  
  111. if (ringtoneTitle == null) {
  112. Ringtone ringtone = RingtoneManager.getRingtone(mContext,Uri.parse(mValue));
  113. String title = ringtone.getTitle(mContext);
  114. if (title != null && title.length() > 0)
  115. ringtoneTitle = title;
  116. }
  117.  
  118. }
  119.  
  120. CharSequence summary = super.getSummary();
  121.  
  122. if (ringtoneTitle != null) {
  123. if (summary != null)
  124. return String.format(summary.toString(),ringtoneTitle);
  125. else
  126. return ringtoneTitle;
  127. } else return summary;
  128. }
  129.  
  130. @Override
  131. protected void onPrepareDialogBuilder(Builder builder) {
  132.  
  133. final Map<String,Uri> sounds = new LinkedHashMap<String,Uri>();
  134.  
  135. if (mExtraRingtones != null) {
  136. for (CharSequence extraRingtone : mExtraRingtones) {
  137. Uri uri = uriFromRaw(extraRingtone.toString());
  138. String title = getExtraRingtoneTitle(extraRingtone);
  139.  
  140. sounds.put(title,uri);
  141. }
  142. }
  143.  
  144. if (mShowDefault) {
  145. Uri uriDefault = RingtoneManager.getDefaultUri(mRingtoneType);
  146. if (uriDefault != null) {
  147. Ringtone ringtoneDefault = RingtoneManager.getRingtone(mContext,uriDefault);
  148. if (ringtoneDefault != null) {
  149. sounds.put(ringtoneDefault.getTitle(mContext),uriDefault);
  150. }
  151. }
  152. }
  153.  
  154. if (mShowSilent)
  155. sounds.put(mContext.getString(R.string.silent),Uri.parse(""));
  156.  
  157.  
  158. sounds.putAll(getSounds(RingtoneManager.TYPE_NOTIFICATION));
  159.  
  160.  
  161. final String[] titleArray = sounds.keySet().toArray(new String[0]);
  162. final Uri[] uriArray = sounds.values().toArray(new Uri[0]);
  163.  
  164. int index = mValue != null ? Arrays.asList(uriArray).indexOf(Uri.parse(mValue)) : -1;
  165.  
  166. builder.setSingleChoiceItems(titleArray,index,new DialogInterface.OnClickListener() {
  167.  
  168. public void onClick(DialogInterface dialog,int which) {
  169.  
  170. if (ringtone != null)
  171. ringtone.stop();
  172.  
  173. String title = titleArray[which];
  174. Uri uri = uriArray[which];
  175.  
  176. if (uri != null) {
  177. if (uri.toString().length() > 0) {
  178. ringtone = RingtoneManager.getRingtone(mContext,uri);
  179. ringtone.play();
  180. }
  181. mValue = uri.toString();
  182. } else mValue = null;
  183.  
  184. }
  185. });
  186.  
  187. builder.setPositiveButton(R.string.ok,this);
  188. builder.setNegativeButton(R.string.cancel,this);
  189.  
  190. }
  191.  
  192. @Override
  193. protected void onDialogClosed(boolean positiveResult) {
  194.  
  195. super.onDialogClosed(positiveResult);
  196.  
  197. if (ringtone != null)
  198. ringtone.stop();
  199.  
  200. if (positiveResult && callChangeListener(mValue)) {
  201. persistString(mValue);
  202. notifyChanged();
  203. }
  204.  
  205. }
  206.  
  207. @Override
  208. protected Object onGetDefaultValue(TypedArray a,int index) {
  209. return a.getString(index);
  210. }
  211.  
  212. @Override
  213. protected void onSetInitialValue(boolean restoreValue,Object defaultValue) {
  214.  
  215. if (restoreValue)
  216. mValue = getPersistedString("");
  217. else {
  218. if (mExtraRingtones != null && defaultValue != null && defaultValue.toString().length() > 0) {
  219.  
  220. int index = Arrays.asList(mExtraRingtones).indexOf((CharSequence) defaultValue);
  221. if (index >= 0)
  222. mValue = uriFromRaw(defaultValue.toString()).toString();
  223. else mValue = (String) defaultValue;
  224.  
  225. } else mValue = (String) defaultValue;
  226.  
  227. persistString(mValue);
  228. }
  229.  
  230.  
  231. }
  232.  
  233.  
  234. }

RES /值/ attrs.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <resources>
  3. <declare-styleable name="ExtraRingtonePreference">
  4. <attr name="ringtoneType">
  5. <!-- Ringtones. -->
  6. <flag name="ringtone" value="1" />
  7. <!-- Notification sounds. -->
  8. <flag name="notification" value="2" />
  9. <!-- Alarm sounds. -->
  10. <flag name="alarm" value="4" />
  11. <!-- All available ringtone sounds. -->
  12. <flag name="all" value="7" />
  13. </attr>
  14. <attr name="showSilent" format="boolean"/>
  15. <attr name="showDefault" format="boolean"/>
  16. <attr name="extraRingtones" format="reference"/>
  17. <attr name="extraRingtoneTitles" format="reference"/>
  18. </declare-styleable>
  19.  
  20. </resources>

RES /值/ strings.xml中

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3.  
  4. <string name="silent">Silent</string>
  5. <string name="ok">OK</string>
  6. <string name="cancel">Cancel</string>
  7. <string name="ringtoneTitle">Ringtone</string>
  8. <string name="ringtoneSummary">Ringtone: %s</string>
  9.  
  10. <string-array name="extraRingtones">
  11. <item>deichkind_sone_musik</item>
  12. <item>madonna_like_a_virgin</item>
  13. </string-array>
  14.  
  15. <string-array name="extraRingtoneTitles">
  16. <item>Sone Musik</item>
  17. <item>Like A Virgin</item>
  18. </string-array>
  19. </resources>

RES /生

  1. res
  2. raw
  3. deichkind_sone_musik.mp3
  4. madonna_like_a_virgin.mp3

RES / XML /的preferences.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <PreferenceScreen
  3. xmlns:android="http://schemas.android.com/apk/res/android"
  4. xmlns:auto="http://schemas.android.com/apk/res-auto">
  5.  
  6. <de.almisoft.test.ExtraRingtonePreference
  7. android:key="ringtone"
  8. android:title="@string/ringtoneTitle"
  9. android:summary="@string/ringtoneSummary"
  10. android:defaultValue="deichkind_sone_musik"
  11. auto:ringtoneType="notification"
  12. auto:showSilent="true"
  13. auto:showDefault="true"
  14. auto:extraRingtones="@array/extraRingtones"
  15. auto:extraRingtoneTitles="@array/extraRingtoneTitles"/>
  16.  
  17. <!-- set android:defaultValue
  18. to "deichkind_sone_musik" for your custom mp3
  19. to "" for silent
  20. to "content://settings/system/notification_sound" for system default ringtone -->
  21.  
  22. </PreferenceScreen>

猜你在找的Android相关文章