如何在偏好立即更改后更新我的活动?

时间:2013-08-29 17:50:15

标签: java android sharedpreferences appsettings

我希望在用户更改语言或时间格式等设置后立即更新我的活动。 这是我的代码:

public class SettingsActivity extends PreferenceActivity implements OnSharedPreferenceChangeListener {
    public static final String HOUR_FORMAT = "hourFormat";
    public static final String LANGUAGE_CHOICE = "languageSelection";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);

    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
        if (key.equals("hourFormat")) {
            Preference connectionPref = findPreference(key);
            // Set summary to be the user-description for the selected value
            connectionPref.setSummary(Boolean.toString(sharedPreferences.getBoolean(key, true)));
        }
        else if(key.equals("languageSelection")) {
            Preference connectionPref = findPreference(key);
            // Set summary to be the user-description for the selected value
            connectionPref.setSummary(sharedPreferences.getString(key, "English"));
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        // Set up a listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        // Unregister the listener whenever a key changes
        getPreferenceScreen().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
    }


}

这是preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <CheckBoxPreference
        android:key="hourFormat"
        android:title="@string/hour24"
        android:summary="@string/hour24Explanation"
        android:defaultValue="true" />
    <ListPreference
            android:key="languageSelection"
            android:entries="@array/languageValues"
            android:summary="@string/languageSelection"
            android:entryValues="@array/languageValues"
            android:title="@string/language" />
</PreferenceScreen>

只有重新启动应用程序才有效。但是我想在更改后立即更新它? (如果你愿意,我可以在我的应用程序中提供其他源代码,我知道有很多资源,但我无法使这个东西工作。)

我还认为我并不真正理解Java中的xListener概念。我读了很多,但你可以澄清或提供给我的其他链接。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

当用户更改系统触发器时,您需要接收系统触发器。为此,您需要有一个类来接收信息,例如时间更改,将此类放在主Activity类中:

private final BroadcastReceiver timeChangedReceiver = new BroadcastReceiver(){         @覆盖         public void onReceive(Context context,Intent intent){             final String action = intent.getAction();

        if (action.equals(Intent.ACTION_TIME_CHANGED) ||
            action.equals(Intent.ACTION_TIMEZONE_CHANGED))
        {
            doStuffYouNeedToDo();
        }
    }
};

在Activity中编写一个方法doStuffYouNeedToDo,它可以在更改时间或时区时执行您需要的任何操作。为了确保它被调用,请将它放在你的班级中:

// For intent filter for catching timezone etc changes
private static IntentFilter timeChangeIntentFilter;
static {
    timeChangeIntentFilter = new IntentFilter();
    timeChangeIntentFilter.addAction(Intent.ACTION_TIMEZONE_CHANGED);
    timeChangeIntentFilter.addAction(Intent.ACTION_TIME_CHANGED);
}

,这在onCreate

registerReceiver(timeChangedReceiver, timeChangeIntentFilter);

要在Activity停止时将其停止,请将其放入onDestroy

unregisterReceiver(timeChangedReceiver);