android - 铃声偏好主题

标签 android android-preferences android-theme

我正在创建一个 Android 应用程序,完全在 Holo.Light 主题中。 除了铃声首选项外,所有首选项都很轻!

我什至尝试在 Preferences.xml 中设置 BGColor 和 textColor:

<RingtonePreference
        android:icon="@drawable/ic_menu_note"
        android:key="ringtone"
        android:persistent="true"
        android:summary="@string/settings_ringtone2"            
        android:background="#000000"
        android:textColor="#ffffff"
        android:title="@string/settings_ringtone" />

Android 忽略一切..

有人知道如何将 RingtonePreference 的主题更改为 Holo.Light 吗?

最佳答案

我自己找到了答案,因为看起来,没有应用程序可以触及 Ringtonemanager 的设置..

所以我所做的是扩展 Listpreference:

    package de.Psychologie.socialintelligence;

import java.io.IOException;

import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.res.TypedArray;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Parcel;
import android.os.Parcelable;
import android.preference.ListPreference;
import android.util.AttributeSet;

public class CustomRingtonepreference extends ListPreference{

private MediaPlayer mMediaPlayer;
CharSequence[] mEntries;
CharSequence[] mEntryValues;
private int mClickedDialogEntryIndex;
private String mValue;

public CustomRingtonepreference(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomRingtonepreference(Context context) {
    super(context);
}
/**
 * Sets the value of the key. This should be one of the entries in
 * {@link #getEntryValues()}.
 * 
 * @param value The value to set for the key.
 */
public void setValue(String value) {
    mValue = value;

    persistString(value);
}

/**
 * Sets the value to the given index from the entry values.
 * 
 * @param index The index of the value to set.
 */
public void setValueIndex(int index) {
    if (mEntryValues != null) {
        setValue(mEntryValues[index].toString());
    }
}

/**
 * Returns the value of the key. This should be one of the entries in
 * {@link #getEntryValues()}.
 * 
 * @return The value of the key.
 */
public String getValue() {
    return mValue; 
}

/**
 * Returns the entry corresponding to the current value.
 * 
 * @return The entry corresponding to the current value, or null.
 */
public CharSequence getEntry() {
    int index = getValueIndex();
    return index >= 0 && mEntries != null ? mEntries[index] : null;
}

 public int findIndexOfValue(String value) {
    if (value != null && mEntryValues != null) {
        for (int i = mEntryValues.length - 1; i >= 0; i--) {
            if (mEntryValues[i].equals(value)) {
                return i;
            }
        }
    }
    return -1;
}

private int  getValueIndex() {

    return findIndexOfValue(mValue);
}

@Override
protected void onPrepareDialogBuilder(Builder builder) {
    super.onPrepareDialogBuilder(builder);

    mMediaPlayer = new MediaPlayer();
    mEntries = getEntries();
    mEntryValues = getEntryValues();

    if (mEntries == null || mEntryValues == null) {
        throw new IllegalStateException(
                "ListPreference requires an entries array and an entryValues array.");
    }

    mClickedDialogEntryIndex = getValueIndex();
    builder.setSingleChoiceItems(mEntries, mClickedDialogEntryIndex,
            new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    mClickedDialogEntryIndex = which;

                    String value = mEntryValues[which].toString();
                    try {
                        playSong(value);
                    } catch (IllegalStateException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });

    builder.setPositiveButton("OK", this);
    builder.setNegativeButton("Abbrechen", this);
}

private void playSong(String path) throws IllegalArgumentException,
    IllegalStateException, IOException {

    //Log.d("ringtone", "playSong :: " + path);

    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path);
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_RING);
//  mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

@Override
protected void onRestoreInstanceState(Parcelable state) {
    if (state == null || !state.getClass().equals(SavedState.class)) {
        // Didn't save state for us in onSaveInstanceState
        super.onRestoreInstanceState(state);
        return;
    }

    SavedState myState = (SavedState) state;
    super.onRestoreInstanceState(myState.getSuperState());
    setValue(myState.value);
}

private static class SavedState extends BaseSavedState {
    String value;

    public SavedState(Parcel source) {
        super(source);
        value = source.readString();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        super.writeToParcel(dest, flags);
        dest.writeString(value);
    }

    public SavedState(Parcelable superState) {
        super(superState);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<SavedState> CREATOR =
            new Parcelable.Creator<SavedState>() {
        public SavedState createFromParcel(Parcel in) {
            return new SavedState(in);
        }

        public SavedState[] newArray(int size) {
            return new SavedState[size];
        }
    };
}

 @Override
protected void onDialogClosed(boolean positiveResult) {
    super.onDialogClosed(positiveResult);

    if (positiveResult && mClickedDialogEntryIndex >= 0 && mEntryValues != null) {
        String value = mEntryValues[mClickedDialogEntryIndex].toString();
        if (callChangeListener(value)) {
            setValue(value);
        }
    }

    mMediaPlayer.stop();
    mMediaPlayer.release();
}

@Override
protected Object onGetDefaultValue(TypedArray a, int index) {
    return a.getString(index);
}

@Override
protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
    setValue(restoreValue ? getPersistedString(mValue) : (String) defaultValue);
}

@Override
protected Parcelable onSaveInstanceState() {
    final Parcelable superState = super.onSaveInstanceState();
    if (isPersistent()) {
        // No need to save instance state since it's persistent
        return superState;
    }

    final SavedState myState = new SavedState(superState);
    myState.value = getValue();
    return myState;
}
}

然后添加我选择的编程方式的值:

//Import All Ringtones
    RingtoneManager rm = new RingtoneManager(UserSettingActivity.this);
    rm.setType(RingtoneManager.TYPE_ALARM|RingtoneManager.TYPE_RINGTONE );
    final Cursor ringtones = rm.getCursor();

    List<String> mEntries = new ArrayList<String>();
    List<String> mEntryValues = new ArrayList<String>();

    for (ringtones.moveToFirst(); !ringtones.isAfterLast(); ringtones.moveToNext()) {
        mEntries.add(ringtones.getString(RingtoneManager.TITLE_COLUMN_INDEX));
        mEntryValues.add(ringtones.getString(RingtoneManager.URI_COLUMN_INDEX));
    }
    ringtonepref.setEntryValues(mEntryValues.toArray(new CharSequence[mEntryValues.size()]));
    ringtonepref.setEntries(mEntries.toArray(new CharSequence[mEntries.size()]));

对于使用默认铃声的初始启动:

//Sets the default Alarm to the chosen Value
            ringtonepref.setValue(RingtoneManager.getActualDefaultRingtoneUri(getBaseContext(), RingtoneManager.TYPE_ALARM).toString());

希望对大家有帮助;)

关于android - 铃声偏好主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589467/

相关文章:

连接到已配置网络时的 Android WiFi 管理器网络切换

android - 如何减小封装尺寸

android - 如何删除 PreferenceScreen 中的左缩进?

android - 设置样式android :windowIsTranslucent seems to conflict with windowAnimationStyle

android - 主题、样式还是硬编码?

android - NullPointerException(可能是由getStringExtra或getTimeInstance引起的)

java - Android Renderscript 类型与 U8_4 不匹配

android - 在 android 中有什么方法可以在卸载后保留 SharedPreferences

Android SDK 使用 PreferenceActivity 添加设置

java - toast 继承主题背景