0

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

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 概念。我读了很多,但你能澄清或提供额外的链接给我吗?提前致谢。

4

1 回答 1

0

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

private final BroadcastReceiver timeChangedReceiver = new BroadcastReceiver() { @Override 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);
于 2013-08-29T18:08:30.970 回答