gpt4 book ai didi

android - PreferenceActivity 在应用程序被销毁时保存首选项

转载 作者:行者123 更新时间:2023-11-29 01:44:46 27 4
gpt4 key购买 nike

我有一个 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 时保存?

编辑 好的,发布相关代码。

查询首选项是这样完成的:

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 也不起作用。

最佳答案

我以前遇到过一个服务没有看到首选项的问题,我不记得细节但是像这样返回 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() 中以正确的方式扩充 XML?

addPreferencesFromResource(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);
}
}

如果您解决了问题,请告诉我们。

关于android - PreferenceActivity 在应用程序被销毁时保存首选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22183755/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com