项目作者: JSpiner

项目描述 :
android advanced sharedpreference library
高级语言: Java
项目地址: git://github.com/JSpiner/Prefer.git
创建时间: 2017-02-25T05:53:00Z
项目社区:https://github.com/JSpiner/Prefer

开源协议:

下载


" class="reference-link">Prefer img

Prefer is android advanced sharedpreference library.

Supporting Integer, String, Float, Long, Boolean.

Functions

init prefer class with context

  1. public static void init(@NonNull Context context);
  2. public static void init(@NonNull Context context, @NonNull String fileName);

you can get sharedpreference object

  1. public static synchronized SharedPreferences getSharedPreferences();

set/get key-value pair

  1. public static void set(@NonNull String key, @NonNull Object value);
  2. public static <T> T get(@NonNull String key, @NonNull T defaultValue);

Init Prefer

init prefer AppApplication.java

  1. class AppApplication extends Application {
  2. @Override
  3. public void onCreate() {
  4. super.onCreate();
  5. Prefer.init(this);
  6. }
  7. }

add application in AndroidManifest.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="net.jspiner.preferlib">
  4. <application
  5. android:allowBackup="true"
  6. android:name=".AppApplication" <-- this line
  7. android:icon="@mipmap/ic_launcher"
  8. android:label="@string/app_name"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" ></action>
  14. <category android:name="android.intent.category.LAUNCHER" ></category>
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. </manifest>

Set value

Prefer automatically check the type of this and save it.
It’s very simple.

Before you know Prefer

  1. void setDataUsingSharedPreference(){
  2. SharedPreferences sharedPreferences;
  3. SharedPreferences.Editor editor;
  4. sharedPreferences = getBaseContext().getSharedPreferences("data", MODE_PRIVATE);
  5. editor = sharedPreferences.edit();
  6. editor.putInt("int1", 1);
  7. editor.putInt("int2", 2);
  8. editor.putString("string1", "hello1");
  9. editor.putBoolean("boolean", true);
  10. editor.putFloat("float1", 1.3f);
  11. editor.commit();
  12. // very complex...
  13. }

After you know Prefer

  1. void setDataUsingPrefer(){
  2. Prefer.set("int1", 1);
  3. Prefer.set("int2", 2);
  4. Prefer.set("string1", "hello1");
  5. Prefer.set("boolean", true);
  6. Prefer.set("float1", 1.3f);
  7. // awesome! very simple!
  8. }

Get Value

Using default value, the type is automatically checked and loaded.

Before you know Prefer

  1. void getDataUsingSharedPreference(){
  2. SharedPreferences sharedPreferences;
  3. SharedPreferences.Editor editor;
  4. sharedPreferences = getBaseContext().getSharedPreferences("data", MODE_PRIVATE);
  5. editor = sharedPreferences.edit();
  6. int int1 = sharedPreferences.getInt("int1", 0);
  7. int int2 = sharedPreferences.getInt("int2", 2);
  8. String string1 = sharedPreferences.getString("string1", "none");
  9. Boolean boolean1 = sharedPreferences.getBoolean("boolean", true);
  10. Float float1 = sharedPreferences.getFloat("float1", 0f);
  11. // too many functions.... very very complex
  12. }

After you know Prefer

  1. void getDataUsingPrefer(){
  2. int int1 = Prefer.get("int1", 0);
  3. int int2 = Prefer.get("int2", 0);
  4. String string1 = Prefer.get("string1", "none");
  5. Boolean boolean1 = Prefer.get("boolean1", true);
  6. Float float1 = Prefer.get("float1", 0f);
  7. // Automatically cast everything to as single function!!
  8. // Amaaaaaazing!
  9. }