sharedpreferences存储本地数据

本文介绍了一个自定义的 SharedPreferences 工具类 SharedPreferenceUtil 的实现细节,包括如何在 Android 应用中初始化该工具类以及使用它来存储和读取各种类型的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先需要自己分装一个工具类SharedPreferenceUtil

package com.example.administrator.blackstore.mvp.view.friend;

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

import java.util.Set;

/*
 * 保存数据的类
 */
public class SharedPreferenceUtil {

   private final static String PREFERENCE_NAME = "login_message";
   private static SharedPreferences preferences;
   private static Editor editor;
   public static final String LOGIN_STR = "loginstr";
   public static final String DISTANCE = "updatedistance";
   public static final String FIRST_FLASH = "isFirstIn";
   public static final String DEVICETOKEN = "deviceToken";

   public static void initPreference(Context context) {
      preferences = context.getSharedPreferences(PREFERENCE_NAME,
            Context.MODE_PRIVATE);
      editor = preferences.edit();
   }
   
   public static void putInt(String key, int value) {
      editor.putInt(key, value).commit();
   }

   public static int getInt(String key, int defValue) {
      return preferences.getInt(key, defValue);
   }

   public static void putLong(String key, long value) {
      editor.putLong(key, value).commit();
   }

   public static long getLong(String key, long defValue) {
      return preferences.getLong(key, defValue);
   }

   public static void putString(String key, String value) {
      try {
         editor.putString(key, value).commit();

      } catch (Exception e) {
         e.printStackTrace();
      }
   }
   public static void putStringSet(String key, Set<String> value){
//    return editor.putStringSet(key,value);
      editor.putStringSet(key,value).commit();

   }
   public static Set<String> getStringSet(String key,Set<String> defValue){
      return preferences.getStringSet(key,defValue);
   }
   public static String getString(String key, String defValue) {

      try {
         return preferences.getString(key, defValue);
      } catch (Exception e) {
         e.printStackTrace();
      }
      return preferences.getString(key, defValue);
   }

   public static void putBoolean(String key, boolean value) {
      editor.putBoolean(key, value).commit();
   }

   public static boolean getBoolean(String key, boolean defValue) {
      return preferences.getBoolean(key, defValue);
   }

   public static boolean remove(String key) {
      return editor.remove(key).commit();
   }

   public static boolean contains(String key) {
      return preferences.contains(key);
   }
}
把实体类粘贴到项目中,然后在程序入口applisction的oncreate的方法里面初始化SharedPerfencers

SharedPreferenceUtil.initPreference(appContext);
//初始化以后,就可以保存了

保存的方法

SharedPreferenceUtil.putInt("user_id",0);
SharedPreferenceUtil.putString("shareid_shop",shareid);
还可以保存集合,但是只能保存Set,不能保存list,保存Set的方法同上

在需要的地方取数据的时候

SharedPreferenceUtil.getInt("user_id", 0);//后面的为默认值






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值