现在的位置: 首页 > 编程语言 > 正文

代码片段之SharedPreferences

2018年08月27日 编程语言 ⁄ 共 3232字 ⁄ 字号 评论关闭

public final class PreferenceUtils {

    public static final int DEFFAULT_PAGE = 2;

    public static final String START_PAGE = "start_page";

    private static PreferenceUtils sInstance;

    private final SharedPreferences mPreferences;
     
    public PreferenceUtils(final Context context) {
        mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
    }

     
    public static final PreferenceUtils getInstace(final Context context) {
        if (sInstance == null) {
            sInstance = new PreferenceUtils(context.getApplicationContext());
        }
        return sInstance;
    }

    
    public void setStartPage(final int value) {
        ApolloUtils.execute(false, new AsyncTask<Void, Void, Void>() {
            @Override
            protected Void doInBackground(final Void... unused) {
                final SharedPreferences.Editor editor = mPreferences.edit();
                editor.putInt(START_PAGE, value);
                SharedPreferencesCompat.apply(editor);

                return null;
            }
        }, (Void[])null);
    }

    public final int getStartPage() {
        return mPreferences.getInt(START_PAGE, DEFFAULT_PAGE);
    }

}

public final class SharedPreferencesCompat {

    private final static Method mApplyMethod = findApplyMethod();

    /**
     * @return The apply() method from {@link SharedPreferences.Editor}.
     */
    private final static Method findApplyMethod() {
        try {
            final Class<Editor> class1 = SharedPreferences.Editor.class;
            return class1.getMethod("apply");
        } catch (final NoSuchMethodException ignored) {
        }
        return null;
    }

    /**
     * @param editor The {@link SharedPreferences.Editor} to use.
     */
    public static void apply(final SharedPreferences.Editor editor) {
        if (mApplyMethod != null) {
            try {
                mApplyMethod.invoke(editor);
                return;
            } catch (final InvocationTargetException ignored) {
            } catch (final IllegalAccessException ignored) {
            }
        }
        editor.commit();
    }
}

public abstract void apply ()

Added in API level 9

Commit your preferences changes back from this Editor to the SharedPreferences object
it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call apply wins.

Unlike commit(),
which writes its preferences out to persistent storage synchronously, apply() commits
its changes to the in-memory SharedPreferences immediately
but starts an asynchronous commit to disk and you won't be notified of any failures. If another editor on this SharedPreferences does
a regular commit()while
apply() is
still outstanding, the commit() will
block until all async commits are completed as well as the commit itself.

As SharedPreferences instances
are singletons within a process, it's safe to replace any instance ofcommit() with apply() if
you were already ignoring the return value.

You don't need to worry about Android component lifecycles and their interaction with apply() writing to disk. The framework
makes sure in-flight disk writes from apply() complete before switching states.

The SharedPreferences.Editor interface isn't expected to be implemented directly. However, if you previously did implement it and are now getting errors about missing apply(), you can
simply callcommit() from apply().

public abstract boolean commit ()

Added in API level 1

Commit your preferences changes back from this Editor to the SharedPreferences object
it is editing. This atomically performs the requested modifications, replacing whatever is currently in the SharedPreferences.

Note that when two editors are modifying preferences at the same time, the last one to call commit wins.

If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.

Returns
  • Returns true if the new values were successfully written to persistent storage.

抱歉!评论已关闭.