【问题标题】:PreferenceActivity saving preferences when app is destroyed销毁应用程序时的 PreferenceActivity 保存首选项
【发布时间】:2014-04-06 16:25:58
【问题描述】:

我有一个 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 也不起作用。

【问题讨论】:

  • 没有看到相关代码,我们应该如何猜测?
  • 希望你现在有足够的相关代码:)
  • 好的,点了;)。这很奇怪。 +1 以吸引更多关注!
  • 所以,我一直在做我的研究。我的实际问题可以恢复为:我如何将我的 PreferenceActivity 首选项持久化到 APPLICATION 上下文而不是 Activity 上下文?
  • f***的正确拼写是F)@#。

标签: android preferenceactivity


【解决方案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() 中以正确的方式膨胀 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);
    }
}

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

【讨论】:

  • 有没有办法在默认共享首选项上指定模式?
  • 这是一种更改默认共享首选项的名称和模式的方法。
【解决方案2】:

我使用这些静态方法为应用程序保存整数首选项。这适用于我的所有应用程序,因为我使用通用的 R.string.app_name 和活动,这是识别我的首选项文件的上下文。

我这样称呼它。

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;
}
}

祝你好运

丹尼117

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-29
    • 2018-09-04
    相关资源
    最近更新 更多