Skip to content

Latest commit

 

History

History
49 lines (43 loc) · 1.63 KB

README.md

File metadata and controls

49 lines (43 loc) · 1.63 KB

BasePreferences

Use simple annotations to access preferences.

How to implement and use

  1. implement your preferences class and extend from BasePreferences
    public class Preferences extends BasePreferences {
        ...
    }
  2. define your key names of preferences
    public static final String KEY_API_KEY = "API_KEY";
    public static final String KEY_UID = "UID";
    public static final String KEY_PSID = "PSID";
  3. declare member fields with PreferenceProperty annotation, PreferenceProperty must setup the key and the value of specified type
    @PreferenceProperty(key = KEY_UID, stringValue = "USER_1234")
    public String uid;
    @PreferenceProperty(key = KEY_IS_PASSED, boolValue = true)
    public boolean isPassed;
    @PreferenceProperty(key = KEY_EXPIRE_TIME, longValue = 123456789L)
    public long expireTime;
    @PreferenceProperty(key = KEY_RATIO, floatValue = 1.67f)
    public long ratio;
  4. call loadAll() to load all preferences
  5. save a preference with two steps
    • assign the value into the field
    • call save(KEY) method to save the value from field into preference, the KEY is your definitions in step 2
      Preferences preferences = new Preferences(context)
      preferences.uid = "USER_5678";
      preferences.save(Preferences.KEY_UID);
      
  6. you can also save all by calling saveAll() method

Properties

Supported types with default value in PreferenceProperty annotation:

  • String with stringValue
  • boolean with boolValue
  • int with intValue
  • long with longValue
  • float with floatValue