SPUtils

Android SPUtils 类详解
本文介绍了一个用于 Android 开发中简化 SharedPreferences 操作的实用工具类 SPUtils。该类提供了多种方法来存储和读取不同类型的 SharedPreferences 数据,如布尔值、字符串、整数等。此外,还介绍了如何获取 SharedPreferences 的所有数据。
package cn.itcast.demo;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import java.util.Map;

public class SPUtils {
   private Context context;
   private SharedPreferences sp;
   private Editor editor;

   public SPUtil(Context context) {
      this.context = context;
      sp = this.context.getSharedPreferences("config", Context.MODE_APPEND);
      editor = sp.edit();
   }

   public void getInstance(Context context, String filename) {
      this.context = context;
      sp = context.getSharedPreferences(filename, Context.MODE_APPEND);
      editor = sp.edit();
   }

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

   public boolean getBoolean(String key, Boolean defValue) {
      return sp.getBoolean(key, defValue);
   }

   public void putString(String key, String value) {
      if (key == null) {
         return;
      }
      editor.putString(key, value);
      editor.commit();
   }

   public String getString(String key, String defValue) {
      return sp.getString(key, defValue);
   }

   public void putInt(String key, int value) {
      editor.putInt(key, value);
      editor.commit();
   }

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

   public Map<String, ?> getAll() {
      return sp.getAll();
   }
}

 

转载于:https://www.cnblogs.com/loaderman/p/6435097.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值