列出所有系统铃声和您的自定义铃声(以xml定义,存储在res / raw中)
ExtraringtonePreference.java
package de.almisoft.test;import java.util.Arrays;import java.util.linkedHashMap;import java.util.Map;import java.util.TreeMap;import de.almisoft.test.R;import androID.app.AlertDialog.Builder;import androID.content.Context;import androID.content.DialogInterface;import androID.content.res.TypedArray;import androID.database.Cursor;import androID.media.ringtone;import androID.media.ringtoneManager;import androID.net.Uri;import androID.preference.DialogPreference;import androID.util.AttributeSet;public class ExtraringtonePreference extends DialogPreference { private Context mContext; private String mValue; private ringtone ringtone; private int mringtoneType; private boolean mShowSilent; private boolean mShowDefault; private CharSequence[] mExtraringtones; private CharSequence[] mExtraringtoneTitles; public ExtraringtonePreference(Context context,AttributeSet attrs) { super(context,attrs); mContext = context; final TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ExtraringtonePreference,0); mringtoneType = a.getInt(R.styleable.ExtraringtonePreference_ringtoneType,ringtoneManager.TYPE_ringtone); mShowDefault = a.getBoolean(R.styleable.ExtraringtonePreference_showDefault,true); mShowSilent = a.getBoolean(R.styleable.ExtraringtonePreference_showSilent,true); mExtraringtones = a.getTextArray(R.styleable.ExtraringtonePreference_extraringtones); mExtraringtoneTitles = a.getTextArray(R.styleable.ExtraringtonePreference_extraringtoneTitles); a.recycle(); } public ExtraringtonePreference(Context context) { this(context,null); } public String getValue() { return mValue; } private Map<String,Uri> getSounds(int type) { ringtoneManager ringtoneManager = new ringtoneManager(mContext); ringtoneManager.setType(type); Cursor cursor = ringtoneManager.getCursor(); Map<String,Uri> List = new TreeMap<String,Uri>(); while (cursor.movetoNext()) { String notificationTitle = cursor.getString(ringtoneManager.Title_ColUMN_INDEX); Uri notificationUri = ringtoneManager.getringtoneUri(cursor.getposition()); List.put(notificationTitle,notificationUri); } return List; } private Uri uriFromraw(String name) { int resID = mContext.getResources().getIDentifIEr(name,"raw",mContext.getPackagename()); return Uri.parse("androID.resource://" + mContext.getPackagename() + "/" + resID); } private String getExtraringtoneTitle(CharSequence name) { if (mExtraringtones != null && mExtraringtoneTitles != null) { int index = Arrays.asList(mExtraringtones).indexOf(name); return mExtraringtoneTitles[index].toString(); } return null; } @OverrIDe public CharSequence getSummary() { String ringtoneTitle = null; if (mValue != null) { if (mValue.length() == 0) ringtoneTitle = mContext.getString(R.string.silent); if (ringtoneTitle == null && mExtraringtones != null && mExtraringtoneTitles != null) { for (int i = 0; i < mExtraringtones.length; i++) { Uri urIExtra = uriFromraw(mExtraringtones[i].toString()); if (urIExtra.equals(Uri.parse(mValue))) { ringtoneTitle = mExtraringtoneTitles[i].toString(); break; } } } if (ringtoneTitle == null) { ringtone ringtone = ringtoneManager.getringtone(mContext,Uri.parse(mValue)); String Title = ringtone.getTitle(mContext); if (Title != null && Title.length() > 0) ringtoneTitle = Title; } } CharSequence summary = super.getSummary(); if (ringtoneTitle != null) { if (summary != null) return String.format(summary.toString(),ringtoneTitle); else return ringtoneTitle; } else return summary;} @OverrIDe protected voID onPrepareDialogBuilder(Builder builder) { final Map<String,Uri> sounds = new linkedHashMap<String,Uri>(); if (mExtraringtones != null) { for (CharSequence extraringtone : mExtraringtones) { Uri uri = uriFromraw(extraringtone.toString()); String Title = getExtraringtoneTitle(extraringtone); sounds.put(Title,uri); } } if (mShowDefault) { Uri urIDefault = ringtoneManager.getDefaultUri(mringtoneType); if (urIDefault != null) { ringtone ringtoneDefault = ringtoneManager.getringtone(mContext,urIDefault); if (ringtoneDefault != null) { sounds.put(ringtoneDefault.getTitle(mContext),urIDefault); } } } if (mShowSilent) sounds.put(mContext.getString(R.string.silent),Uri.parse("")); sounds.putAll(getSounds(ringtoneManager.TYPE_NOTIFICATION)); final String[] TitleArray = sounds.keySet().toArray(new String[0]); final Uri[] uriArray = sounds.values().toArray(new Uri[0]); int index = mValue != null ? Arrays.asList(uriArray).indexOf(Uri.parse(mValue)) : -1; builder.setSingleChoiceItems(TitleArray,index,new DialogInterface.OnClickListener() { public voID onClick(DialogInterface dialog,int which) { if (ringtone != null) ringtone.stop(); String Title = TitleArray[which]; Uri uri = uriArray[which]; if (uri != null) { if (uri.toString().length() > 0) { ringtone = ringtoneManager.getringtone(mContext,uri); ringtone.play(); } mValue = uri.toString(); } else mValue = null; } }); builder.setPositivebutton(R.string.ok,this); builder.setNegativebutton(R.string.cancel,this); } @OverrIDe protected voID onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (ringtone != null) ringtone.stop(); if (positiveResult && callchangelistener(mValue)) { persistString(mValue); notifyChanged(); } } @OverrIDe protected Object onGetDefaultValue(TypedArray a,int index) { return a.getString(index); } @OverrIDe protected voID onSetinitialValue(boolean restoreValue,Object defaultValue) { if (restoreValue) mValue = getPersistedString(""); else { if (mExtraringtones != null && defaultValue != null && defaultValue.toString().length() > 0) { int index = Arrays.asList(mExtraringtones).indexOf((CharSequence) defaultValue); if (index >= 0) mValue = uriFromraw(defaultValue.toString()).toString(); else mValue = (String) defaultValue; } else mValue = (String) defaultValue; persistString(mValue); } }}
RES /值/ attrs.xml
<?xml version="1.0" enCoding="UTF-8"?><resources> <declare-styleable name="ExtraringtonePreference"> <attr name="ringtoneType"> <!-- ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alarm sounds. --> <flag name="alarm" value="4" /> <!-- All available ringtone sounds. --> <flag name="all" value="7" /> </attr> <attr name="showSilent" format="boolean"/> <attr name="showDefault" format="boolean"/> <attr name="extraringtones" format="reference"/> <attr name="extraringtoneTitles" format="reference"/> </declare-styleable></resources>
RES /值/ strings.xml中
<?xml version="1.0" enCoding="utf-8"?><resources> <string name="silent">Silent</string> <string name="ok">OK</string> <string name="cancel">Cancel</string> <string name="ringtoneTitle">ringtone</string> <string name="ringtoneSummary">ringtone: %s</string> <string-array name="extraringtones"> <item>deichkind_sone_musik</item> <item>madonna_like_a_virgin</item> </string-array> <string-array name="extraringtoneTitles"> <item>Sone Musik</item> <item>like A Virgin</item> </string-array></resources>
RES /生
res ↳ raw ↳ deichkind_sone_musik.mp3 ↳ madonna_like_a_virgin.mp3
RES / XML /的preferences.xml
<?xml version="1.0" enCoding="utf-8"?><PreferenceScreen xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:auto="http://schemas.androID.com/apk/res-auto"> <de.almisoft.test.ExtraringtonePreference androID:key="ringtone" androID:title="@string/ringtoneTitle" androID:summary="@string/ringtoneSummary" androID:defaultValue="deichkind_sone_musik" auto:ringtoneType="notification" auto:showSilent="true" auto:showDefault="true" auto:extraringtones="@array/extraringtones" auto:extraringtoneTitles="@array/extraringtoneTitles"/> <!-- set androID:defaultValue to "deichkind_sone_musik" for your custom mp3 to "" for silent to "content://settings/system/notification_sound" for system default ringtone --></PreferenceScreen>总结
以上是内存溢出为你收集整理的android – 在首选项中,选择我的声音就像铃声优先全部内容,希望文章能够帮你解决android – 在首选项中,选择我的声音就像铃声优先所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)