简体   繁体   中英

How can i update my activities after preference changes immediately?

I want to update my activity immediately after a user changes a setting like language or time format. Here is my code:

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


}

This is 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>

This only works, if i restart the app. However I want to update it immediately just after the change? (If you want, I can provide other source codes in my app, I know there are lots of resources but I couldn't get this thing working.)

I also think I don't really understand the xListener concept in Java. I read about it a lot but can you clarify or provide additional links to me. Thanks, in advance.

You need to receive the system triggers when the user changes them. To do this, you need to have a class to receive the information, for example for the Time change, put this class in the main Activity class:

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

Write a method doStuffYouNeedToDo in the Activity which does whatever you need to when the time or timezone is changed. To make sure it is called, put this in your class:

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

and this in onCreate :

registerReceiver(timeChangedReceiver, timeChangeIntentFilter);

And to stop it when the Activity stops, put this in onDestroy :

unregisterReceiver(timeChangedReceiver);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM