如何动态设置首选项的值?

时间:2013-03-25 19:31:21

标签: android android-preferences

我使用了preferenceActivity的一个特殊实现,它允许使用标题并且与旧的Android版本兼容。我想要完成的是我希望在ListPreference时重置CheckBoxPreference的值未选中,但只要调用setValue()setEntries(),我就会获得null pointer Exception。这是我的代码:

import java.util.List;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Build;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.ListPreference;
import android.preference.PreferenceManager;
import android.util.Log;
import com.actionbarsherlock.app.SherlockPreferenceActivity;

public class FragmentPreferences extends SherlockPreferenceActivity implements
    OnSharedPreferenceChangeListener {
private static final String TAG = "FragmentPreferences";
private boolean themeIsDirty = false;
private ListPreference prefDspProfile;

@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(ActivityOriginal.THEME);
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
        addPreferencesFromResource(R.xml.preference_general);
        addPreferencesFromResource(R.xml.preference_layout);
        addPreferencesFromResource(R.xml.preference_audio);
        addPreferencesFromResource(R.xml.preference_dsp);
        addPreferencesFromResource(R.xml.preference_error);
    }
    // turn off unaccessible dsp effects based on android version
    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1) {
        if (!android.media.audiofx.AcousticEchoCanceler.isAvailable()) {
            setEchoCancelerAsIncompatible();
        }
        if (!android.media.audiofx.NoiseSuppressor.isAvailable()) {
            setNoiseSuppressorAsIncompatible();
        }
        if (!android.media.audiofx.AutomaticGainControl.isAvailable()) {
            setGainControlAsIncompatible();
        }
    } else {
        setEchoCancelerAsIncompatible();
        setNoiseSuppressorAsIncompatible();
        setGainControlAsIncompatible();

    }

}

private void setGainControlAsIncompatible() {
    CheckBoxPreference prefDspAutomaticGainControl = (CheckBoxPreference) getPreferenceScreen()
            .findPreference("pref_dsp_automatic_gain_control");
    prefDspAutomaticGainControl.setChecked(false);
    prefDspAutomaticGainControl
            .setSummaryOff(R.string.pref_dsp_automatic_gain_control_incompatible);
    prefDspAutomaticGainControl.setEnabled(false);
}

private void setNoiseSuppressorAsIncompatible() {
    CheckBoxPreference prefDspNoiseSuppressor = (CheckBoxPreference) getPreferenceScreen()
            .findPreference("pref_dsp_noise_suppressor");
    prefDspNoiseSuppressor.setChecked(false);
    prefDspNoiseSuppressor
            .setSummaryOff(R.string.pref_dsp_noise_suppressor_incompatible);
    prefDspNoiseSuppressor.setEnabled(false);

}

private void setEchoCancelerAsIncompatible() {
    CheckBoxPreference prefDspEchoCanceler = (CheckBoxPreference) getPreferenceScreen()
            .findPreference("pref_dsp_echo_canceler");
    prefDspEchoCanceler.setChecked(false);
    prefDspEchoCanceler
            .setSummaryOff(R.string.pref_dsp_echo_canceler_incompatible);
    prefDspEchoCanceler.setEnabled(false);

}

@SuppressLint("NewApi")
@Override
public void onBuildHeaders(List<Header> target) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        loadHeadersFromResource(R.xml.preference_headers, target);

        for (Header header : target) {
            switch (header.titleRes) {
            case R.string.prefs_header1_title:
                header.iconRes = ActivityOriginal.getIcon(
                        getSupportActionBar().getThemedContext(),
                        "ic_menu_pref_general");
                break;
            case R.string.prefs_header2_title:
                header.iconRes = ActivityOriginal.getIcon(
                        getApplicationContext(), "ic_menu_pref_layout");
                break;
            case R.string.prefs_header3_title:
                header.iconRes = ActivityOriginal.getIcon(
                        getApplicationContext(), "ic_menu_pref_sound");
                break;
            case R.string.prefs_header4_title:
                header.iconRes = ActivityOriginal.getIcon(
                        getApplicationContext(), "ic_menu_pref_error");
                break;
            case R.string.prefs_header5_title:
                header.iconRes = ActivityOriginal.getIcon(
                        getApplicationContext(), "ic_menu_pref_dsp");
                break;
            }
        }
    }
}

@Override
public void onStop() {

    /*
     * Toast.makeText(this.getApplicationContext(),
     * getString(R.string.reset_to_make_changes), Toast.LENGTH_LONG).show();
     */

    super.onStop();
}

@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {
    Log.i(TAG, "changed key is : " + key);
    SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(this);
    if ("pref_dsp_bass_boost".equals(key)
            || "pref_dsp_low_pass_filter".equals(key)) {
        initPrefsDsp();
        if (prefs.getBoolean("pref_dsp_bass_boost", true)
                && prefs.getBoolean("pref_dsp_low_pass_filter", true)) {
            prefDspProfile.setValue("0");
            prefDspProfile = null;
            Log.i(TAG, "set the pref_dsp_profile to 0 ");

        } else {
            prefDspProfile.setValue("1");
            prefDspProfile = null;
            Log.i(TAG, "set the pref_dsp_profile to 1 ");
        }
    }
    if ("pref_dsp_profile".equals(key)) {
        initPrefsDsp();
        if ("0".equals(prefDspProfile.getValue())) {
            CheckBoxPreference prefDspBassBoost = (CheckBoxPreference) findPreference("pref_dsp_low_pass_filter");
            prefDspBassBoost.setChecked(true);

            CheckBoxPreference prefDspLowPassFilter = (CheckBoxPreference) findPreference("pref_dsp_bass_boost");
            prefDspLowPassFilter.setChecked(true);
        }
    }
    if ("theme_preference".equals(key)) {
        Log.i(this.getClass().getName(), "themeIsDirty");
        themeIsDirty = true;
        Intent in = new Intent();
        in.putExtra("themeIsDirty", themeIsDirty);
        setResult(RESULT_OK, in);
        PreferenceManager.getDefaultSharedPreferences(this)
                .unregisterOnSharedPreferenceChangeListener(this);

    }

}

private void initPrefsDsp() {
    /*
     * CharSequence[] dspEntries = getResources().getStringArray(
     * R.array.prefs_dsp_entries); CharSequence[] dspValues =
     * getResources().getStringArray( R.array.prefs_dsp_values);
     */
    CharSequence[] dspEntries = { "Dark grey", "Light grey" };
    CharSequence[] dspValues = { "0", "1" };
    prefDspProfile = (ListPreference) findPreference("pref_dsp_profile");
    if (prefDspProfile == null) {
        Log.i(TAG, "prefDspProfile is null");
        return;
    }
    prefDspProfile.setEntries(dspEntries);
    prefDspProfile.setEntryValues(dspValues);

}

@Override
protected void onResume() {
    super.onResume();
    PreferenceManager.getDefaultSharedPreferences(this)
            .registerOnSharedPreferenceChangeListener(this);
    /*
     * getPreferenceScreen().getSharedPreferences()
     * .registerOnSharedPreferenceChangeListener(this);
     */

}

@Override
public void onBackPressed() {
    // the following block will warn user after back button is pressed
    PreferenceManager.getDefaultSharedPreferences(this)
            .unregisterOnSharedPreferenceChangeListener(this);
    /*
     * getPreferenceScreen().getSharedPreferences()
     * .unregisterOnSharedPreferenceChangeListener(this);
     */
    Intent in = new Intent();
    in.putExtra("themeIsDirty", themeIsDirty);
    setResult(RESULT_OK, in);
    super.onBackPressed();
}

@Override
protected void onPause() {
    super.onPause();

}

  }

preference_dsp.xml

<?xml version="1.0" encoding="utf-8"?>

<ListPreference
    android:defaultValue="0"
    android:dialogTitle="@string/pref_dsp_title_dialog"
    android:entries="@array/prefs_dsp_entries"
    android:entryValues="@array/prefs_dsp_values"
    android:key="pref_dsp_profile"
    android:summary="@string/pref_dsp_summary"
    android:title="@string/pref_dsp_title" />

<CheckBoxPreference
    android:defaultValue="true"
    android:key="pref_dsp_low_pass_filter"
    android:summaryOff="@string/pref_dsp_low_pass_filter_disabled"
    android:summaryOn="@string/pref_dsp_low_pass_filter_enabled"
    android:title="@string/pref_dsp_low_pass_filter_title" />
<CheckBoxPreference
    android:defaultValue="true"
    android:key="pref_dsp_bass_boost"
    android:summaryOff="@string/pref_dsp_bass_boost_disabled"
    android:summaryOn="@string/pref_dsp_bass_boost_enabled"
    android:title="@string/pref_dsp_bass_boost_title" />
<CheckBoxPreference
    android:defaultValue="true"
    android:key="pref_dsp_echo_canceler"
    android:summaryOff="@string/pref_dsp_echo_canceler_disabled"
    android:summaryOn="@string/pref_dsp_echo_canceler_enabled"
    android:title="@string/pref_dsp_echo_canceler_title" />
<CheckBoxPreference
    android:defaultValue="true"
    android:key="pref_dsp_noise_suppressor"
    android:summaryOff="@string/pref_dsp_noise_suppressor_disabled"
    android:summaryOn="@string/pref_dsp_noise_suppressor_enabled"
    android:title="@string/pref_dsp_noise_suppressor_title" />
<CheckBoxPreference
    android:defaultValue="false"
    android:key="pref_dsp_automatic_gain_control"
    android:summaryOff="@string/pref_dsp_automatic_gain_control_disabled"
    android:summaryOn="@string/pref_dsp_automatic_gain_control_enabled"
    android:title="@string/pref_dsp_automatic_gain_control_title" />
<ListPreference
    android:defaultValue="2.0f"
    android:dialogTitle="@string/prefs_dsp_sound_gain_dialog_title"
    android:entries="@array/prefs_dsp_sound_gain_entries"
    android:entryValues="@array/prefs_dsp_sound_gain_values"
    android:key="prefs_dsp_sound_gain"
    android:summary="@string/prefs_dsp_sound_gain_summary"
    android:title="@string/prefs_dsp_sound_gain_title" />

我无法在网上找到关于如何动态修改preference值的完整示例。

2 个答案:

答案 0 :(得分:4)

尝试(例如布尔值):

SharedPreferences.Editor prefs = PreferenceManager.getDefaultSharedPreferences(this).edit();
prefs.putBoolean("your_pref", false);
prefs.commit();

答案 1 :(得分:1)

findPreference("pref_dsp_profile")正在返回null。基于您目前共享的代码不明显的原因,没有名为pref_dsp_profile的首选项可访问。也许你的情况不成立(Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB),或者这些偏好不在这些文件中。