Account Preferences API for Android

Account Kit allows you to store a limited quantity of key/value pairs associated with a user account. This can be useful if, for example, your application does not have a server component, but you'd still like to share a users settings or preferences across devices.

You may store up to 100 key/value pairs per user. A key is a string of up to 100 characters; allowed characters are uppercase and lowercase letters, numerals, and the underscore. A value is a string of up to 1000 characters.

To interact with user preferences, you must get the AccountPreferences instance from an AKFAccountKit instance. The following sample code shows how to use AccountPreferences and implement appropriate listener objects to respond to changes:

public class MyActivity extends Activity {
  @Override
  public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load all preferences for this account:
    AccountKit.getAccountPreferences().loadPreferences(new PrefsLoadListener());

    // Load a specific preference:
    AccountKit.getAccountPreferences().loadPreference("nickname", new SinglePrefLoadListener());

    // Set a preference:
    AccountKit.getAccountPreferences().setPreference("favoritecolor", "3b5998", new PrefSetListener());

    // Delete a preference:
    AccountKit.getAccountPreferences().loadPreference("timesincelastlogin ", new PrefDeleteListener());
  }

  private class PrefsLoadListener implements AccountPreferences.OnLoadPreferencesListener {
    public void onLoadPreferences(
        @Nullable Map<String, String> preferences,
        @Nullable AccountKitError error) {
      if (error != null) {
        // ... handle error appropriately ...
        return;
      }

      // You now have access to all existing user preferences in a Map
    }
  }

  private class SinglePrefLoadListener implements AccountPreferences.OnLoadPreferenceListener {
    public void onLoadPreference(String key, @Nullable String value, @Nullable AccountKitError error) {
      if (error != null) {
        // ... handle error appropriately ...
        return;
      }

      // ... use the delivered value and key ...
    }
  }

  private class PrefSetListener implements AccountPreferences.OnSetPreferenceListener {
    public void onSetPreference(String key, String value, @Nullable AccountKitError error) {
      if (error != null) {
        // ... handle error appropriately ...
        return;
      }

      // ... use the delivered value and key ...
    }
  }

  private class PrefDeleteListener implements AccountPreferences.OnDeletePreferenceListener {
    public void onDeletePreference(String key, @Nullable AccountKitError error) {
      if (error != null) {
        // ... handle error appropriately ...
        return;
      }

      // You have been notified that the value associated with "key" was deleted
    }
  }
}