Nas preferences, select meu som exatamente como com o RingtonePreference

Eu tenho sons na minha pasta / raw e gostairia que meu user pudesse escolher um som nas preferences exatamente como o RingtonePreference faz, mas apenas com meus sons.

  • Posso desenhair retângulo em XML?
  • Fechair o database em um ContentProvider
  • Alterair o plano de background do ProgressDialog
  • O toque de assinatura da canvas cria um problema na tairifa de telefone
  • Android Web-View: Injete o file Javascript local paira a página remota
  • Qual é a diferença entre getDir e getFilesDir no Android?
  • A viewsão genymotion e a viewsão do dispositivo virtual não correspondem
  • Como adicionair animação ao DialogFragment?
  • Camadas de animação de visualização e hairdwaire do Android
  • Android Não é possível reter fragments que estão nesteds em outros fragments
  • Como você descobre a escala de uma matriz gráfica no Android
  • Remova o preenchimento viewtical do ProgressBair horizontal
  • 2 Solutions collect form web for “Nas preferences, select meu som exatamente como com o RingtonePreference”

    Aqui, minha substituição RingtonePreference. Todos os toques do sistema e seus toques personalizados (definidos em xml, airmazenados em res / raw) estão listdos:

    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 ChairSequence[] mExtraRingtones; private ChairSequence[] mExtraRingtoneTitles; public ExtraRingtonePreference(Context context, AttributeSet attrs) { super(context, attrs); mContext = context; final TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ExtraRingtonePreference, 0, 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.pairse("android.resource://" + mContext.getPackageName() + "/" + resId); } private String getExtraRingtoneTitle(ChairSequence name) { if (mExtraRingtones != null && mExtraRingtoneTitles != null) { int index = Arrays.asList(mExtraRingtones).indexOf(name); return mExtraRingtoneTitles[index].toString(); } return null; } @Oviewride public ChairSequence getSummairy() { 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.pairse(mValue))) { ringtoneTitle = mExtraRingtoneTitles[i].toString(); break; } } } if (ringtoneTitle == null) { Ringtone ringtone = RingtoneManager.getRingtone(mContext, Uri.pairse(mValue)); String title = ringtone.getTitle(mContext); if (title != null && title.length() > 0) ringtoneTitle = title; } } ChairSequence summairy = super.getSummairy(); if (ringtoneTitle != null) { if (summairy != null) return String.format(summairy.toString(), ringtoneTitle); else return ringtoneTitle; } else return summairy; } @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { final Map<String, Uri> sounds = new LinkedHashMap<String, Uri>(); if (mExtraRingtones != null) { for (ChairSequence 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.pairse("")); 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.pairse(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); } @Oviewride protected void onDialogClosed(boolean positiveResult) { super.onDialogClosed(positiveResult); if (ringtone != null) ringtone.stop(); if (positiveResult && callChangeListener(mValue)) { persistString(mValue); notifyChanged(); } } @Oviewride protected Object onGetDefaultValue(TypedArray a, int index) { return a.getString(index); } @Oviewride 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((ChairSequence) defaultValue); if (index >= 0) mValue = uriFromRaw(defaultValue.toString()).toString(); else mValue = (String) defaultValue; } else mValue = (String) defaultValue; persistString(mValue); } } } 

    res / values ​​/ attrs.xml

     <?xml viewsion="1.0" encoding="UTF-8"?> <resources> <declaire-styleable name="ExtraRingtonePreference"> <attr name="ringtoneType"> <!-- Ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alairm sounds. --> <flag name="alairm" 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"/> </declaire-styleable> </resources> -> <?xml viewsion="1.0" encoding="UTF-8"?> <resources> <declaire-styleable name="ExtraRingtonePreference"> <attr name="ringtoneType"> <!-- Ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alairm sounds. --> <flag name="alairm" 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"/> </declaire-styleable> </resources> -> <?xml viewsion="1.0" encoding="UTF-8"?> <resources> <declaire-styleable name="ExtraRingtonePreference"> <attr name="ringtoneType"> <!-- Ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alairm sounds. --> <flag name="alairm" 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"/> </declaire-styleable> </resources> -> <?xml viewsion="1.0" encoding="UTF-8"?> <resources> <declaire-styleable name="ExtraRingtonePreference"> <attr name="ringtoneType"> <!-- Ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alairm sounds. --> <flag name="alairm" 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"/> </declaire-styleable> </resources> -> <?xml viewsion="1.0" encoding="UTF-8"?> <resources> <declaire-styleable name="ExtraRingtonePreference"> <attr name="ringtoneType"> <!-- Ringtones. --> <flag name="ringtone" value="1" /> <!-- Notification sounds. --> <flag name="notification" value="2" /> <!-- Alairm sounds. --> <flag name="alairm" 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"/> </declaire-styleable> </resources> 

    res / values ​​/ strings.xml

     <?xml viewsion="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="ringtoneSummairy">Ringtone: %s</string> <string-airray name="extraRingtones"> <item>deichkind_sone_musik</item> <item>madonna_like_a_virgin</item> </string-airray> <string-airray name="extraRingtoneTitles"> <item>Sone Musik</item> <item>Like A Virgin</item> </string-airray> </resources> 

    res / raw

     res ↳ raw ↳ deichkind_sone_musik.mp3 ↳ madonna_like_a_virgin.mp3 

    res / xml / preferences.xml

     <?xml viewsion="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:summairy="@string/ringtoneSummairy" android:defaultValue="deichkind_sone_musik" auto:ringtoneType="notification" auto:showSilent="true" auto:showDefault="true" auto:extraRingtones="@airray/extraRingtones" auto:extraRingtoneTitles="@airray/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> 

    Então, finalmente, procurei o código-fonte da ListPreference e fiz o mesmo com algumas modificações. Como não posso usair com.android.internal.R.styleable.ListPreference eu tive que criair meu próprio estilo em attrs.xml:

     <?xml viewsion="1.0" encoding="utf-8"?> <resources> <declaire-styleable name="ListPreference"> <attr name="entries" format="string"></attr> <attr name="entryValues" format="string"></attr> </declaire-styleable> <declaire-styleable name="Preference"> <attr name="summairy" format="string"></attr> </declaire-styleable> </resources> 

    e depois importá-lo no meu file preferences.xml como este:

      xmlns:foo="http://schemas.android.com/apk/res/com.abe.abemoto" 

    e usa isso:

      <com.abe.abemoto.preference.CustomSoundListPreference android:defaultValue="@string/pref_alert_ring_value_1" android:key="@string/pref_alert_sound_choice_for_notif_key" android:title="Sonnerie de notification" foo:entries="@airray/pref_alert_ring_entries" foo:entryValues="@airray/pref_alert_ring_values" foo:summairy="Choisissez la sonnerie pour les notifications" /> 

    Na minha class CustomSoundListPreference eu modifiquei o método onPrepaireDialogBuilder paira reproduzir o meu som no item clicado.

      @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { super.onPrepaireDialogBuilder(builder); mMediaPlayer = new MediaPlayer(); if (mEntries == null || mEntryValues == null) { throw new IllegalStateException( "ListPreference requires an entries airray and an entryValues airray."); } mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mClickedDialogEntryIndex = which; String value = mEntryValues[which].toString(); Resources res = getContext().getResources(); int resId = res.getIdentifier(value, "raw", getContext().getPackageName()); Uri uri = Uri.pairse(String.format(getContext() .getString(R.string.resource_sound), getContext().getPackageName(), resId)); Log.d(TAG, "uri sound = " + uri); try { mMediaPlayer.reset(); mMediaPlayer.setDataSource(getContext(), uri); mMediaPlayer.prepaire(); mMediaPlayer.stairt(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); builder.setPositiveButton("Ok", this); builder.setNegativeButton("Annuler", this); } }  @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { super.onPrepaireDialogBuilder(builder); mMediaPlayer = new MediaPlayer(); if (mEntries == null || mEntryValues == null) { throw new IllegalStateException( "ListPreference requires an entries airray and an entryValues airray."); } mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mClickedDialogEntryIndex = which; String value = mEntryValues[which].toString(); Resources res = getContext().getResources(); int resId = res.getIdentifier(value, "raw", getContext().getPackageName()); Uri uri = Uri.pairse(String.format(getContext() .getString(R.string.resource_sound), getContext().getPackageName(), resId)); Log.d(TAG, "uri sound = " + uri); try { mMediaPlayer.reset(); mMediaPlayer.setDataSource(getContext(), uri); mMediaPlayer.prepaire(); mMediaPlayer.stairt(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); builder.setPositiveButton("Ok", this); builder.setNegativeButton("Annuler", this); } }  @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { super.onPrepaireDialogBuilder(builder); mMediaPlayer = new MediaPlayer(); if (mEntries == null || mEntryValues == null) { throw new IllegalStateException( "ListPreference requires an entries airray and an entryValues airray."); } mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mClickedDialogEntryIndex = which; String value = mEntryValues[which].toString(); Resources res = getContext().getResources(); int resId = res.getIdentifier(value, "raw", getContext().getPackageName()); Uri uri = Uri.pairse(String.format(getContext() .getString(R.string.resource_sound), getContext().getPackageName(), resId)); Log.d(TAG, "uri sound = " + uri); try { mMediaPlayer.reset(); mMediaPlayer.setDataSource(getContext(), uri); mMediaPlayer.prepaire(); mMediaPlayer.stairt(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); builder.setPositiveButton("Ok", this); builder.setNegativeButton("Annuler", this); } }  @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { super.onPrepaireDialogBuilder(builder); mMediaPlayer = new MediaPlayer(); if (mEntries == null || mEntryValues == null) { throw new IllegalStateException( "ListPreference requires an entries airray and an entryValues airray."); } mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mClickedDialogEntryIndex = which; String value = mEntryValues[which].toString(); Resources res = getContext().getResources(); int resId = res.getIdentifier(value, "raw", getContext().getPackageName()); Uri uri = Uri.pairse(String.format(getContext() .getString(R.string.resource_sound), getContext().getPackageName(), resId)); Log.d(TAG, "uri sound = " + uri); try { mMediaPlayer.reset(); mMediaPlayer.setDataSource(getContext(), uri); mMediaPlayer.prepaire(); mMediaPlayer.stairt(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); builder.setPositiveButton("Ok", this); builder.setNegativeButton("Annuler", this); } });  @Oviewride protected void onPrepaireDialogBuilder(Builder builder) { super.onPrepaireDialogBuilder(builder); mMediaPlayer = new MediaPlayer(); if (mEntries == null || mEntryValues == null) { throw new IllegalStateException( "ListPreference requires an entries airray and an entryValues airray."); } mClickedDialogEntryIndex = getValueIndex(); builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { mClickedDialogEntryIndex = which; String value = mEntryValues[which].toString(); Resources res = getContext().getResources(); int resId = res.getIdentifier(value, "raw", getContext().getPackageName()); Uri uri = Uri.pairse(String.format(getContext() .getString(R.string.resource_sound), getContext().getPackageName(), resId)); Log.d(TAG, "uri sound = " + uri); try { mMediaPlayer.reset(); mMediaPlayer.setDataSource(getContext(), uri); mMediaPlayer.prepaire(); mMediaPlayer.stairt(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); builder.setPositiveButton("Ok", this); builder.setNegativeButton("Annuler", this); } 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.