如何检测首选项是否发生更改?

发布于 2024-10-17 04:20:21 字数 1205 浏览 9 评论 0原文

我有一个类扩展 PreferenceActivity 并显示我的应用程序的首选项屏幕。是否可以检查首选项是否有任何更改?

这有助于...

http://developer.android.com/reference/ android/content/SharedPreferences.OnSharedPreferenceChangeListener.html

其他相关帖子: SharedPreferences.onSharedPreferenceChangeListener 未被一致调用


public class PreferenceClass extends PreferenceActivity {

    OnSharedPreferenceChangeListener listener;

    public void onCreate(Bundle savedInstanceState) {
        SharedPreferences prefs = this.getSharedPreferences("settings", 0);
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {

            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                int flag = 1;
            }
        };
        prefs.registerOnSharedPreferenceChangeListener(listener);
        super.onCreate(null);
        addPreferencesFromResource(R.xml.settings);
    }
}

I have a class that extends PreferenceActivity and shows the preference screen of my app. Is it possible to check if any changes were made to the preferences?

This helps...

http://developer.android.com/reference/android/content/SharedPreferences.OnSharedPreferenceChangeListener.html

Other related post:
SharedPreferences.onSharedPreferenceChangeListener not being called consistently


public class PreferenceClass extends PreferenceActivity {

    OnSharedPreferenceChangeListener listener;

    public void onCreate(Bundle savedInstanceState) {
        SharedPreferences prefs = this.getSharedPreferences("settings", 0);
        listener = new SharedPreferences.OnSharedPreferenceChangeListener() {

            public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
                int flag = 1;
            }
        };
        prefs.registerOnSharedPreferenceChangeListener(listener);
        super.onCreate(null);
        addPreferencesFromResource(R.xml.settings);
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

深白境迁sunset 2024-10-24 04:20:21

在您的 PreferenceActivity 中执行操作

SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
                           SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // your stuff here
    }
};

,即使其成为您的 PreferenceActivity 类的成员,然后在 PreferenceActivity.onCreate 中执行 registerOnSharedPreferenceChangeListener(spChanged) () 方法。

这就是我所做的,我从来没有遇到过问题。

否则,这是您在侦听器中进行的条件检查出了问题。发布代码。

编辑:

从您发布的代码中,您应该将 prefs 设为类成员变量,以便它具有全局范围。

并执行 prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); 而不是 getSharedPreferences 因为您尚未创建该文件。

要创建文件,您需要使用PreferenceManager。要获取 PreferenceManager,请使用 Activity.getPreferenceManager()

Do

SharedPreferences.OnSharedPreferenceChangeListener spChanged = new
                           SharedPreferences.OnSharedPreferenceChangeListener() {
            @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
            String key) {
        // your stuff here
    }
};

In your PreferenceActivity, ie make it a member of your PreferenceActivity class and then do registerOnSharedPreferenceChangeListener(spChanged) in the PreferenceActivity.onCreate() method.

That's what I do and I never have a problem.

Else it's your conditional checking in the listener that is at fault. Post the code.

EDIT:

From the code you posted, you should make prefs a class member variable so it has a global scope.

And do prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); instead of getSharedPreferences because you haven't created that file.

To create a file you need to use PreferenceManager. To get a PreferenceManager, use Activity.getPreferenceManager().

小巷里的女流氓 2024-10-24 04:20:21

PreferenceActivity 类中,实现 SharedPreferences.OnSharedPreferenceChangeListener 接口。将所需的 onSharedPreferenceChanged 方法添加到您的类中,并在 onCreate 中注册它。

请参阅此处的示例代码:

public class MyPreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fw_preferences); //deprecated
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // handle the preference change here
    }

}

In your PreferenceActivity class, implement the SharedPreferences.OnSharedPreferenceChangeListener interface. Add the required onSharedPreferenceChanged method to your class and register it in the onCreate.

See sample code here:

public class MyPreferences extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.fw_preferences); //deprecated
        PreferenceManager.getDefaultSharedPreferences(this).registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        // handle the preference change here
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文