我有一个MainActivity和一个从该Activity调用的PreferenceActivity。我还有一个服务运行查询这些首选项。
当我打印这些值时。我明白了:
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50
然后我打开PreferenceActivity。打印出来:
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50
我将pref_scrobble_percentage更改为7,然后强制打印
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50
我关闭了PreferenceActivity,然后强行打印:
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50
关闭MainActivity,然后强制打印:
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 50
我杀了应用程序,然后强制打印:
D/pref_scrobble(4083): true
D/pref_show_notification(4083): true
D/pref_scrobble_only_on_wifi(4083): false
D/pref_scrobble_percentage(4083): 7
为什么在应用程序被杀死时保存首选项,而不是在我更改其值或关闭PreferenceActivity时保存首选项?
编辑好的,发布相关代码。
查询prefs是这样的:
public static boolean getScrobbleEnabled(final Context ctx) {
final String key = ctx.getString(R.string.pref_scrobble);
final SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(ctx);
printPrefs(settings);
return settings.getBoolean(key, true);
}
private static void printPrefs(final SharedPreferences settings) {
Map<String, ?> map = settings.getAll();
for (String str : map.keySet()) {
Log.d(str, map.get(str).toString());
}
}
我在PreferenceActivity上膨胀的xml是:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/scrobbler_conf" >
<CheckBoxPreference
android:defaultValue="true"
android:key="@string/pref_scrobble"
android:summary="@string/enable_scrobbling_subtitle"
android:title="@string/enable_scrobbling" />
<CheckBoxPreference
android:defaultValue="true"
android:dependency="@string/pref_scrobble"
android:key="@string/pref_show_notification"
android:summary="@string/show_notification_subtitle"
android:title="@string/show_notification" />
<com.garli.lastfm.controller.preferences.SeekBarPreference
android:defaultValue="50"
android:dependency="@string/pref_scrobble"
android:key="@string/pref_scrobble_percentage"
android:max="100"
android:summary="@string/scrobble_percentage_subtitle"
android:title="@string/scrobble_percentage" />
<CheckBoxPreference
android:defaultValue="false"
android:dependency="@string/pref_scrobble"
android:key="@string/pref_scrobble_only_on_wifi"
android:summary="@string/scrobble_only_on_wifi_subtitle"
android:title="@string/scrobble_only_on_wifi" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/ads" >
<Preference
android:defaultValue="false"
android:key="@string/pref_remove_ads"
android:summary="@string/ads_subtitle"
android:title="@string/ads" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/about" >
<Preference
android:key="pref_version"
android:title="@string/version" />
</PreferenceCategory>
</PreferenceScreen>
默认情况下会处理这些首选项。更改CheckBoxPreferences也不起作用。
答案 0 :(得分:1)
我遇到过这个问题,服务没有看到之前的偏好,我不记得细节,但返回像这样的SharedPreferences解决了它。我记得因为服务实际上不在一个单独的过程中而认为这看起来很奇怪,但它解决了这个问题。 文档在这面旗帜上似乎有些混乱。
public static SharedPreferences getPref(Context context)
{
int mode = android.os.Build.VERSION.SDK_INT >= 11 ?
Context.MODE_MULTI_PROCESS :
Context.MODE_PRIVATE;
return context.getSharedPreferences(SHARED_PREF_NAME, mode);
}
另外,你是否正确地在onCreate()?
中膨胀XMLaddPreferencesFromResource(R.xml.preferences);
请查看此更改默认共享首选项名称和模式。
public class MyPreferencesActivity extends PreferenceActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
PreferenceManager prefMgr = getPreferenceManager();
prefMgr.setSharedPreferencesName("my_preferences");
prefMgr.setSharedPreferencesMode(MODE_MULTI_PROCESS);
addPreferencesFromResource(R.xml.preferences);
}
}
如果您解决了问题,请告诉我们。
答案 1 :(得分:0)
我使用这些静态方法来保存应用程序的整数首选项。这适用于我的所有应用程序,因为我使用常见的R.string.app_name和activity,这是一个用于标识我的首选项文件的上下文。
我称之为。
int x = GoPreferences.getInt(getActivity(),MAP_TYPE,GoogleMap.MAP_TYPE_NORMAL);
...
GoPreferences.putInt(getActivity(),MAP_TYPE, x);
...
//simple static preferences methods
public class GoPreferences {
public static void putInt(Context context, String s, int b) {
SharedPreferences sharedPref = context.getSharedPreferences(
context.getString(R.string.app_name), Context.MODE_PRIVATE);
sharedPref.edit().putInt(s, b).commit();
}
public static int getInt(Context context, String s, int defaultvalue) {
defaultvalue = context.getSharedPreferences(context.getString(R.string.app_name),
Context.MODE_PRIVATE).getInt(s, defaultvalue);
return defaultvalue;
}
}
祝你好运
Danny117