1

当应用程序运行时,使用什么代码来检测 SharedPreferences 中所做的任何更改?我的 SharedPreferences 中所做的更改仅在应用程序关闭并重新启动时才会反映出来。它不会在同一会话期间更改。

要获取首选项,我在 SettingsActivity.java 中使用以下代码

@Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference("unit_type"));
    }

在activity.java中

final SharedPreferences unitpreference = PreferenceManager.getDefaultSharedPreferences(this);
final String unitstring = unitpreference.getString("unit_type", "");

但是我应该在哪里写一些代码以便立即进行更改?

完整更新代码:SettingsActivity.java

package com.basic.mileagecalculatorwithsettings;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceCategory;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
import java.util.List;

public class SettingsActivity extends PreferenceActivity 
{

private static final boolean ALWAYS_SIMPLE_PREFS = false;
protected static final String TAG = null;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setupActionBar();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Display the fragment as the main content.
        getFragmentManager().beginTransaction()
                .replace(android.R.id.content, new GeneralPreferenceFragment())
                .commit();
    } else {
        // use normal preferences
        setupSimplePreferencesScreen();

        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(mPrefsListener);
    }
}

SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
        if ("unit_type".equals(key)) {
            Log.v(TAG, "Setting has been changed!");
        }
    }

};
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() 
{
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) 
    {
        getActionBar().setDisplayHomeAsUpEnabled(true);
        }
}
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        switch (item.getItemId()) 
        {
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState) 
    {
        super.onPostCreate(savedInstanceState);

        //setupSimplePreferencesScreen();
    }
    private void setupSimplePreferencesScreen() {
        if (!isSimplePreferences(this)) {
            return;
        }
        addPreferencesFromResource(R.xml.pref_general);}
    @Override
    public boolean onIsMultiPane() 
    {
        return isXLargeTablet(this) && !isSimplePreferences(this);
    }
    private static boolean isXLargeTablet(Context context) 
    {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }
    private static boolean isSimplePreferences(Context context) 
    {
        return ALWAYS_SIMPLE_PREFS
                || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
                || !isXLargeTablet(context);
    }
    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onBuildHeaders(List<Header> target) 
    {
        if (!isSimplePreferences(this)) 
        {
            //loadHeadersFromResource(R.xml.pref_headers, target);
        }
    };
    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) 
        {
            String stringValue = value.toString();

            if (preference instanceof ListPreference) 
            {
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);
                preference.setSummary(index >= 0 ? listPreference.getEntries()[index]:null);
            }
            else if (preference instanceof RingtonePreference) 
            {
                if (TextUtils.isEmpty(stringValue))
                {
                    //preference.setSummary(R.string.pref_ringtone_silent);
                }
                else 
                {
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue));
                    if (ringtone == null) 
                    {preference.setSummary(null);}
                    else 
                    {
                        String name = ringtone.getTitle(preference.getContext());
                        preference.setSummary(name);
                    }
                }
            }
            else 
            {
                preference.setSummary(stringValue);
            }
            return true;
        }
};
private static void bindPreferenceSummaryToValue(Preference preference) 
{
    preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
    sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(),""));
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class GeneralPreferenceFragment extends PreferenceFragment 
{
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref_general);
        bindPreferenceSummaryToValue(findPreference("example_list"));
        bindPreferenceSummaryToValue(findPreference("fuel_type"));
        bindPreferenceSummaryToValue(findPreference("unit_type"));

    }
    @Override
    public void onResume() {
        super.onResume();


    }
    public void onPause(Bundle savedInstanceState)
    {
      super.onPause();
      addPreferencesFromResource(R.xml.pref_general);

     }
}
}

pref_general.xml

<ListPreference
    android:defaultValue="1"
    android:entries="@array/pref_example_list_titles"
    android:entryValues="@array/pref_example_list_values"
    android:key="city_list"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/pref_title_add_friends_to_messages" android:enabled="true"/>

<ListPreference
    android:defaultValue="1"
    android:entries="@array/pref_fuel_type_titles"
    android:entryValues="@array/pref_fuel_type_values"
    android:key="fuel_type"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/pref_title_select_fuel" android:enabled="true"/>

<ListPreference
    android:defaultValue="1"
    android:entries="@array/pref_units_titles"
    android:entryValues="@array/pref_units_values"
    android:key="unit_type"
    android:negativeButtonText="@null"
    android:positiveButtonText="@null"
    android:title="@string/pref_title_unit" android:enabled="true"/>

</PreferenceScreen>
4

2 回答 2

1

我检查了您的代码,看起来您根本没有创建GeneralPreferenceFragment。所以,如果我添加以下内容:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setupActionBar();

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new GeneralPreferenceFragment())
            .commit();
}

并删除setupSimplePreferencesScreen()呼叫,然后它可以在最新的 Android 版本上正常工作。

如果您需要支持旧版本,我会这样做:

public class SettingsActivity extends PreferenceActivity {

    private static final boolean ALWAYS_SIMPLE_PREFS = false;
    protected static final String TAG = null;

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            // Display the fragment as the main content.
            getFragmentManager().beginTransaction()
                    .replace(android.R.id.content, new GeneralPreferenceFragment())
                    .commit();
        } else {
            // use normal preferences
            setupSimplePreferencesScreen();

            PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(mPrefsListener);
        }
    }

    SharedPreferences.OnSharedPreferenceChangeListener mPrefsListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
        @Override
        public void onSharedPreferenceChanged(final SharedPreferences sharedPreferences, final String key) {
            Log.v(TAG, "Setting has been changed!");
        }
    };

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    private void setupActionBar()
    {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        {
            getActionBar().setDisplayHomeAsUpEnabled(true);
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case android.R.id.home:
                //NavUtils.navigateUpFromSameTask(this);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    protected void onPostCreate(Bundle savedInstanceState)
    {
        super.onPostCreate(savedInstanceState);

        //setupSimplePreferencesScreen();
    }
    private void setupSimplePreferencesScreen() {
        if (!isSimplePreferences(this)) {
            return;
        }
        addPreferencesFromResource(R.xml.pref_general);
    }

    @Override
    public boolean onIsMultiPane() {
        return isXLargeTablet(this) && !isSimplePreferences(this);
    }

    private static boolean isXLargeTablet(Context context) {
        return (context.getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_XLARGE;
    }
    private static boolean isSimplePreferences(Context context) {
        return ALWAYS_SIMPLE_PREFS
                || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB
                || !isXLargeTablet(context);
    }

    @Override
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public void onBuildHeaders(List<Header> target) {
        if (!isSimplePreferences(this)) {
            //loadHeadersFromResource(R.xml.pref_headers, target);
        }
    }

    private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
        @Override
        public boolean onPreferenceChange(Preference preference, Object value) {
            Log.v(TAG, "Setting has been changed!");

            String stringValue = value.toString();

            if (preference instanceof ListPreference) {
                ListPreference listPreference = (ListPreference) preference;
                int index = listPreference.findIndexOfValue(stringValue);
                preference.setSummary(index >= 0 ? listPreference.getEntries()[index]:null);
            }
            else if (preference instanceof RingtonePreference) {
                if (TextUtils.isEmpty(stringValue)) {
                    //preference.setSummary(R.string.pref_ringtone_silent);
                } else {
                    Ringtone ringtone = RingtoneManager.getRingtone(
                            preference.getContext(), Uri.parse(stringValue));
                    if (ringtone == null) {
                        preference.setSummary(null);
                    } else {
                        String name = ringtone.getTitle(preference.getContext());
                        preference.setSummary(name);
                    }
                }
            } else {
                preference.setSummary(stringValue);
            }

            return true;
        }
    };
    private static void bindPreferenceSummaryToValue(Preference preference){
        preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
        sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,PreferenceManager.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(),""));
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public static class GeneralPreferenceFragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            addPreferencesFromResource(R.xml.pref_general);
            bindPreferenceSummaryToValue(findPreference("example_list"));
            bindPreferenceSummaryToValue(findPreference("fuel_type"));
            bindPreferenceSummaryToValue(findPreference("unit_type"));
        }

        @Override
        public void onResume() {
            super.onResume();
        }

        public void onPause(Bundle savedInstanceState) {
            super.onPause();
            addPreferencesFromResource(R.xml.pref_general);
        }
    }
}

我已经使用以下 pref_general.xml 检查了该代码:

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
        android:title="Title category">

        <ListPreference
            android:key="example_list"
            android:title="example_list"
            android:summary="example_list"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="example_list" />
        <ListPreference
            android:key="fuel_type"
            android:title="fuel_type"
            android:summary="fuel_type"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="fuel_type" />
        <ListPreference
            android:key="unit_type"
            android:title="unit_type"
            android:summary="unit_type"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="unit_type" />
    </PreferenceCategory>

</PreferenceScreen>

并且能够观察Setting has been changed!日志。

基本上,您的代码的问题是您既不是 createGeneralPreferenceFragment也没有侦听器使用SharedPreferences.OnSharedPreferenceChangeListener. 我建议使代码更具可读性,这样您可能更容易发现问题。

于 2013-07-04T06:08:27.650 回答
0

如果在SharedPreferences您关闭应用程序之前存在未保存的问题,您可能没有提交/应用更改(正如 user1283633 在他/她的评论中建议的那样)。

来自SharedPrecerences.Editor的 JavaDoc :

用于修改 SharedPreferences 对象中的值的接口。您在编辑器中所做的所有更改都是批处理的,并且在您调用 commit() 或 apply() 之前不会复制回原始 SharedPreferences

我假设您SharedPreferences.Editor在向SharedPreferences商店添加数据时使用 ,完成添加数据后,您可以commit()尝试apply()Editor. 选择commit()你是否关心结果(返回布尔值),否则你可以选择apply()(void)。

于 2013-07-04T06:17:58.040 回答