默认共享preferences给我服务的错误的价值观 [英] Default Shared Preferences give me wrong values in Service

查看:178
本文介绍了默认共享preferences给我服务的错误的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在那里我已经定义了一个XML格式的复选框preference一个preferenceFragment。我需要在服务检查此值,但它总是给我的旧值。我注意到,当我重新启动应用程序的值正确更改。

I have a PreferenceFragment where I have defined a CheckBoxPreference in XML. I need to check this value in a Service, but it always gives me the old value. I noticed the value is correctly changed when I restart the application.

我的preference片段:

My Preference Fragment :

public class OptionsFragment extends PreferenceFragment 
{

    public static final String WIFI_ONLY = "wifi";

    private SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance());

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.config);
    }

 }

我的config.xml文件:

My config.xml :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

    <CheckBoxPreference
        android:defaultValue="true"
        android:key="wifi"
        android:summary="Check if you want to use wifi only"
        android:title="Use Wifi only" />

</PreferenceScreen>

我的服务:

SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(MyApplication.getInstance());        
Log.d(TAG, "isWifiOnly : "+settings.getBoolean(OptionsFragment.WIFI_ONLY, true));

日志总是返回相同的值不管,如果我改变它或没有,除非我重新启动应用程序。此外,在我的MainActivity我在OnCreate()中的行:

The log always return the same value no matter if I change it or not, except if i restart the app. Also, in my MainActivity I have that line in OnCreate():

PreferenceManager.setDefaultValues(getApplicationContext(), R.xml.config, false);

它创建如果需要的话默认值的配置文件。

It creates the config file with the default value if needed.

我做错了什么,问题是什么?

I'm doing something wrong, the question is what ?

推荐答案

我找到了一个解决方案,由于此链接<一个href=\"http://stackoverflow.com/questions/10546511/managing-shared$p$pferences-in-both-$p$pferenceactivity-and-service-within-same-ap\">Managing

I found a solution thanks to this link Managing SharedPreferences in both PreferenceActivity and Service within same app and thanks to Andrew T. :

问题是多进程模式。如果您有声明的清单了Android服务:过程=(这是我的情况),那么有必要设置一个多进程模式

The issue was the multi process mode. If you have a Service which is declared in the manifest with a android:process="" (which is my case) then it's necessary to set a multi process mode.

下面是我做过什么:

在preferenceFragment:

in PreferenceFragment :

public static final String CONFIG_NAME = "pref";

...

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


    getPreferenceManager().setSharedPreferencesName(CONFIG_NAME);
    getPreferenceManager().setSharedPreferencesMode(Context.MODE_MULTI_PROCESS);

    addPreferencesFromResource(R.xml.config);

    ...
}

我的服务:

SharedPreferences settings = MyApplication.getInstance().getSharedPreferences(OptionsFragment.CONFIG_NAME, Context.MODE_MULTI_PROCESS);

和我MainActivity这种方式设置的默认值:

and I set the default values in MainActivity this way :

PreferenceManager.setDefaultValues(getApplicationContext(), OptionsFragment.CONFIG_NAME, Context.MODE_MULTI_PROCESS, R.xml.config, false);

而现在它工作正常。希望这将有助于!

And now it works fine. Hope it will help !

再次感谢您安德鲁吨。

这篇关于默认共享preferences给我服务的错误的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆