Kotlin版本SharedPreferences數(shù)據(jù)保存

  • Java版數(shù)據(jù)保存
  • Kotlin版本數(shù)據(jù)保存
  • ContentProvider獲取Context

一、Java版數(shù)據(jù)保存

??在android開發(fā)中通常我們會使用SharedPreferences進行少量數(shù)據(jù)的保存,并對SharedPreferences進行簡單封裝,代碼可能如下:

  • 簡單對SharedPreferences包裝,簡化調用,避免遺漏apply()。
import android.content.Context;
import android.content.SharedPreferences;

public class SharedPrefUtil {
    private SharedPreferences mSharedPrefs;
    private SharedPreferences.Editor mEditor;

    public SharedPrefUtil(Context ctx, String name) {
        mSharedPrefs = ctx.getSharedPreferences(name, Context.MODE_PRIVATE);
        mEditor = mSharedPrefs.edit();
    }

    public void putString(String name, String value) {
        mEditor.putString(name, value).apply();
    }

    public String getString(String name, String defaultValue) {
        return mSharedPrefs.getString(name, defaultValue);
    }

    public void putLong(String name, Long value) {
        mEditor.putLong(name, value).apply();
    }

    public Long getLong(String name, Long defaultValue) {
        return mSharedPrefs.getLong(name, defaultValue);
    }
    
    .......
}
  • 假設我們需要存儲 用戶名用戶ID 就會有下面的代碼:
import android.content.Context;

public class Setting {
    private static volatile Setting INSTANCE = null;

    private final SharedPrefUtil mUtil;

    private Setting(Context ctx) {
        mUtil = new SharedPrefUtil(ctx, "setting.json");
    }

    public static Setting getInstance(Context ctx) {
        if (INSTANCE == null) {
            synchronized (Setting.class) {
                if (INSTANCE == null) INSTANCE = new Setting(ctx);
            }
        }
        return INSTANCE;
    }

    private String name = null;
    private Long uid = -1L;

    public String getName() {
        if (name == null)
            name = mUtil.getString("name", "");
        return name;
    }

    public void setName(String name) {
        mUtil.putString("name", name == null ? "" : name);
        this.name = name;
    }

    public Long getUid() {
        if (uid == -1L)
            uid = mUtil.getLong("uid", -1L);
        return uid;
    }

    public void setUid(Long uid) {
        mUtil.putLong("uid", uid);
        this.uid = uid;
    }
}
  • 調用的代碼是這樣的
final Context context = this.getBaseContext();
Setting.getInstance(context).setName("小三");
String name = Setting.getInstance(context).getName();

??上面的代碼實現(xiàn)起來并不復雜,使用起來好像也不麻煩,但當我們需要增加保存的數(shù)據(jù)時,就需要在Setting對象中增加大量的set、get方法,而且使用的時候都要先獲去Setting實例。如果能像使用變量一樣使用SharedPreferences會多么美好?。?!

二、Kotlin版本數(shù)據(jù)保存

  • kotlin屬性代理版本,可以讓我們像使用變量一樣存儲和獲取數(shù)據(jù)
class SharedPref<T>(
    private val context: Context,
    private val name: String,
    private val defValue: T,
    private val pref: String = "default",
    private val commit: Boolean = false
) : ReadWriteProperty<Any?, T> {

    private val prefs by lazy {
        context.getSharedPreferences(pref, Context.MODE_PRIVATE)
    }

    override fun getValue(thisRef: Any?, property: KProperty<*>): T =
        findPreference(findProperName(property))

    override fun setValue(thisRef: Any?, property: KProperty<*>, value: T) =
        putPreference(findProperName(property), value)

    private fun findProperName(property: KProperty<*>) = if (name.isEmpty()) property.name else name

    private fun findPreference(key: String): T = when (defValue) {
        is Int -> prefs.getInt(key, defValue)
        is Long -> prefs.getLong(key, defValue)
        is Float -> prefs.getFloat(key, defValue)
        is Boolean -> prefs.getBoolean(key, defValue)
        is String -> prefs.getString(key, defValue)
        else -> throw IllegalArgumentException("Unsupported type.")
    } as T

    private fun putPreference(key: String, value: T) {
        val edit = prefs.edit().apply {
            when (value) {
                is Int -> putInt(key, value)
                is Long -> putLong(key, value)
                is Float -> putFloat(key, value)
                is Boolean -> putBoolean(key, value)
                is String -> putString(key, value)
                else -> throw IllegalArgumentException("Unsupported type.")
            }
        }
        commit.yes { edit.commit() }.other { edit.apply() }
    }

}
  • 假設我們需要存儲 用戶名 和 用戶ID 就會有下面的代碼:
private lateinit var APPCTX: Context

object Setting {
    var name by SharedPref(APPCTX, "", "")
    var uid by SharedPref(APPCTX, "uid", -1L)
}
  • 調用的代碼是這樣的
Setting.name = "小三"
Log.e(Setting.name)

三、ContentProvider獲取Context

??上面的Context來源都是通過外部傳遞過來的,如果在module中使用就會變得不方便。不過通過在AndroidManifest.xml中注冊ContentProvider就可以解決這個問題。

  • 創(chuàng)建自己的ContentProvider對象,在此對象中就有我們需要的Context屬性。
class InitProvider : ContentProvider() {

    override fun onCreate(): Boolean {
        // 這里可以獲得 context 屬性
        return false
    }

    override fun insert(uri: Uri, values: ContentValues?): Uri? = null

    override fun query(
        uri: Uri,
        projection: Array<out String>?,
        selection: String?,
        selectionArgs: Array<out String>?,
        sortOrder: String?
    ): Cursor? = null

    override fun update(
        uri: Uri,
        values: ContentValues?,
        selection: String?,
        selectionArgs: Array<out String>?
    ): Int = 0

    override fun delete(uri: Uri, selection: String?, selectionArgs: Array<out String>?): Int = 0

    override fun getType(uri: Uri): String? = null
}
  • AndroidManifest.xml注冊我們定義的ContentProvider對象。
    <application>
        <!-- 去context實現(xiàn) -->
        <provider
            android:name=".init.InitProvider"
            android:authorities="${applicationId}.common.provider"
            android:exported="false"
            android:multiprocess="true" />
    </application>

??這樣我們在主app:module中不用添加一句代碼就已經可以在需要Contextmodule中或得到Context對象。

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

  • 2.1 Activity 2.1.1 Activity的生命周期全面分析 典型情況下的生命周期:在用戶參與的情況下...
    AndroidMaster閱讀 3,272評論 0 8
  • 1 Android四種數(shù)據(jù)持久化方式 Android有四種數(shù)據(jù)持久化方式: SharePreference 輕量...
    Kevin_Junbaozi閱讀 1,339評論 2 1
  • 數(shù)據(jù)存儲部分 那些在內存中的瞬時數(shù)據(jù)存儲到存儲設備中,保證即使手機或電腦關機了,這些數(shù)據(jù)也不會丟失. 文件存儲 不...
    小徐andorid閱讀 807評論 2 7
  • 空氣球飛上天,里面裝著你的希望與善良,越過高高的山崗,在一片靜謐的玫瑰叢林停留,在荒原的野草中飛舞。 離開時你放了...
    天天楊閱讀 373評論 0 0
  • 在學習SQLite之前,首先了解下數(shù)據(jù)持久化的幾種方式: 定義:數(shù)據(jù)持久化是通過文件將數(shù)據(jù)存儲在磁盤上 IOS下主...
    NeverMore奈文摩爾閱讀 1,608評論 0 2

友情鏈接更多精彩內容