简体   繁体   中英

Android - Preferences API onSharedPreferenceChanged - Which way is correct?

I am using the Preferences-API to implement Settings into my App.
I have chosen to use a PreferenceFragment .

My PreferenceFragment contains my onSharedPreferenceChanged code.
However, for one of my Preferences , I'm not sure which way to go about achieving my goal.
The goal is: based on the TogglePreference, either enable or disable my Receiver.

Below, I have included the 2 different ways I thought to do it.
My reasoning for the two examples is #1 doesn't use findPreference(key) and #2 does.
So I am requesting feedback - which way is correct?


public static final String PREF_GPS_STATE_LISTENER = "pref_gpsStateListener";

private SharedPreferences.OnSharedPreferenceChangeListener prefChangeListener;
...
...

prefChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged
        (SharedPreferences sharedPreferences, String key) {

if (key.equals(PREF_GPS_STATE_LISTENER)) {

    final PackageManager pacMan = 
        getActivity().getApplicationContext().getPackageManager();
    final ComponentName compLocationReceiver = new ComponentName
        ("com.studio2bdesigns.testapp021019", ".LocationReceiver");
    final SharedPreferences getPrefs = 
        PreferenceManager.getDefaultSharedPreferences(getActivity());

    if (getPrefs.getBoolean(PREF_GPS_STATE_LISTENER, true)) {

        //  ENABLE THE RECEIVER
        pacMan.setComponentEnabledSetting(compLocationReceiver, 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    } else if (!getPrefs.getBoolean(PREF_GPS_STATE_LISTENER, true)) {

        //  DISABLE THE RECEIVER
        pacMan.setComponentEnabledSetting(compLocationReceiver, 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

    }
}
}

(OR) is this the correct way to do it :


private SharedPreferences.OnSharedPreferenceChangeListener prefChangeListener;
...
...

prefChangeListener = new SharedPreferences.OnSharedPreferenceChangeListener() {
    @Override
    public void onSharedPreferenceChanged
        (SharedPreferences sharedPreferences, String key) {

if (key.equals(PREF_GPS_STATE_LISTENER)) {

    Preference gpsStateListenerPref = findPreference(key);

    final PackageManager pacMan = 
        getActivity().getApplicationContext().getPackageManager();
    final ComponentName compLocationReceiver = new ComponentName
        ("com.studio2bdesigns.testapp021019", ".LocationReceiver");

    if (gpsStateListenerPref.isEnabled()) {

        //  ENABLE THE RECEIVER
        pacMan.setComponentEnabledSetting(compLocationReceiver, 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    } else if (!gpsStateListenerPref.isEnabled()) {

        //  DISABLE THE RECEIVER
        pacMan.setComponentEnabledSetting(compLocationReceiver, 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

    }
}
}

At first I thought Option #1 was correct - but the reason I came up with Option #2 is because the first example doesn't use findPreference(key); - which is the entire point of having key passed within onSharedPreferenceChanged isn't it?

Feedback on this is greatly appreciated! Thanks!

The key is used to match the visual control with shared preferences. This is really a matter for preference. I prefer to use number 1 since I am not dependent on a view, and makes shared preferences your source of truth.

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