更新时间:2023-02-27 10:46:53
我找到了一个解决方案,由于此链接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 !
再次感谢您安德鲁吨。