<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SharePerference 使用詳解

          共 5894字,需瀏覽 12分鐘

           ·

          2020-11-07 16:03

          和你一起終身學習,這里是程序員Android

          經典好文推薦,通過閱讀本文,您將收獲以下知識點:


          一、 SharedPreferences 簡介
          二、SharedPreferences 保存數(shù)據(jù)的方法
          三、SharedPreferences讀取數(shù)據(jù)的方法
          四、總結SharePerference Utils 封裝類使用方法
          五、SharedPreferences 數(shù)據(jù)保存位置

          一、 SharedPreferences 簡介

          SharedPreferencesAndroid的一個接口類,是Android 數(shù)據(jù)存儲(保存內部)的一種方法。主要以*.xml的形式保存在Android /data/data/com.***包名/shared_prefs下,SharedPreferences 類提供了一個通用框架,以便用戶能夠保存和檢索原始數(shù)據(jù)類型的鍵值對,原始數(shù)據(jù)類型如下:BooleanInt,Float,Long,String。

          1.SharedPreferences 使用方法如下:

          1. 創(chuàng)建保存數(shù)據(jù)的xml文件

          2. 使用Editor 向xml文件中保存數(shù)據(jù)

          3. commit()保存數(shù)據(jù)

          4. xml保存地方
            /data/data/com.***包名/shared_prefs

          二、SharedPreferences 保存數(shù)據(jù)的方法

          主要使用putBoolean() 和 putString()putInt()等方法添加值。

          三、SharedPreferences讀取數(shù)據(jù)的方法

          主要使用 getBoolean()和 getString()、getInt()等 獲取保存的數(shù)據(jù)

          四、總結SharePerference Utils 封裝類使用方法

          1.移除SharePerference 保存的值

              private static SharedPreferences sp;
          private static String SPXMLNAME = "sp_config";

          /**
          * @param ctx
          * 上下文環(huán)境
          * @param key
          * 要從config.xml移除節(jié)點的name的名稱
          */

          public static void removeKey(Context ctx, String key) {
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().remove(key).commit();
          }

          2.保存,獲取 Boolean 類型值方法

              // 1,存儲boolean變量方法
          public static void putBoolean(Context ctx, String key, boolean value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putBoolean(key, value).commit();
          }

          // 2,讀取boolean變量方法
          public static boolean getBoolean(Context ctx, String key, boolean defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getBoolean(key, defValue);
          }

          3.保存,獲取String類型值方法


          public static void putString(Context ctx, String key, String value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putString(key, value).commit();
          }

          public static String getString(Context ctx, String key, String defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getString(key, defValue);
          }

          4.保存,獲取Int類型值方法

              //
          public static void putInt(Context ctx, String key, int value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putInt(key, value).commit();
          }

          public static int getInt(Context ctx, String key, int defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getInt(key, defValue);
          }

          5. SharePerferenceUtils

          package com.programandroid.Utils;

          import android.content.Context;
          import android.content.SharedPreferences;

          /*
          * SharePerferenceUtils.java
          *
          * Created on: 2017-9-24
          * Author: wangjie
          *
          * Welcome attention to weixin public number get more info
          *
          * WeiXin Public Number : ProgramAndroid
          * 微信公眾號 :程序員Android
          *
          */

          public class SharePerferenceUtils {

          private static SharedPreferences sp;
          private static String SPXMLNAME = "sp_config";

          /**
          * @param ctx
          * 上下文環(huán)境
          * @param key
          * 要從config.xml移除節(jié)點的name的名稱
          */

          public static void removeKey(Context ctx, String key) {
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().remove(key).commit();
          }

          // 1,存儲boolean變量方法
          public static void putBoolean(Context ctx, String key, boolean value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putBoolean(key, value).commit();
          }

          // 2,讀取boolean變量方法
          public static boolean getBoolean(Context ctx, String key, boolean defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getBoolean(key, defValue);
          }

          public static void putString(Context ctx, String key, String value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putString(key, value).commit();
          }

          public static String getString(Context ctx, String key, String defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getString(key, defValue);
          }

          //
          public static void putInt(Context ctx, String key, int value) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          sp.edit().putInt(key, value).commit();
          }

          public static int getInt(Context ctx, String key, int defValue) {
          // name存儲文件名稱
          if (sp == null) {
          sp = ctx.getSharedPreferences(SPXMLNAME, Context.MODE_PRIVATE);
          }
          return sp.getInt(key, defValue);
          }

          }

          6. Activity 類中使用方法如下:

          • 1.保存數(shù)據(jù)

          保存數(shù)據(jù)調用方法如下:

          SharePerferenceUtils.putInt(getApplicationContext(), "int_key", 1);
          • 2.獲取數(shù)據(jù)

          獲取數(shù)據(jù)調用方法如下:

          SharePerferenceUtils.getString(getApplicationContext(), "string_key", "default_values");

          五、SharedPreferences 數(shù)據(jù)保存位置

          SharedPreferences保存在app內部(/data/data/com.***包名/shared_prefs),當手動清除APK 數(shù)據(jù)的時候,保存的數(shù)據(jù)會被清除掉

          至此 SharedPreferences的使用方法以基本完成。

          至此,本篇已結束。轉載網(wǎng)絡的文章,小編覺得很優(yōu)秀,歡迎點擊閱讀原文,支持原創(chuàng)作者,如有侵權,懇請聯(lián)系小編刪除。同時感謝您的閱讀,期待您的關注。


          瀏覽 40
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  亚洲中文字幕在线观看视频了 | 日韩有码电影中文字幕 | 黄色免费操逼视频 | 做爱高清无码视频免费 | 国产精品高潮在线 |