android键值对

本文详细探讨了Android中的键值对存储原理,包括Shared Preferences、SQLite数据库和Intent中的键值对使用方式,以及它们在应用数据持久化中的角色。通过实例分析,解析了如何有效地管理和读取键值对数据,提升应用的用户体验。

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

/**
 * 键值对写工具
 */
package  xx.utils;


import java.io.IOException;
import java.util.Map;
import java.util.Map.Entry;

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

public class EditKeyValueUtils {
    /**
     * 将对象base64编码之后写入默认文件的指定的键
     * @param context
     * @param object 对象
     * @param key 指定键
     * @return
     */
    public boolean editObjectToLocal(Context context,Object object,String key){
        return editObjectToLocal("application", context, object, key);

    }
    /**
     * 
     * 将对象base64编码之后写入制定的键
     * 
     * @param filePath 写入的文件名称
     * @param context
     * @param object 对象
     * @param key 指定键
     * @return
     */
    public boolean editObjectToLocal(String filePath,Context context,Object object,String key){
        try {
            String objectStr = new Base64Utils().encodetoBase64(object);
            SharedPreferences preferences=context.getSharedPreferences(filePath, Activity.MODE_PRIVATE);
            Editor editor=preferences.edit();
            editor.putString(key, objectStr);
            editor.commit();
            return true;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }

    }
    /**
     * 从默认文件中取得指定键的值
     * @param context
     * @param key
     * @return
     */
    public Object getObjectFromLocal(Context context,String key){
        return getObjectFromLocal("application", context, key);

    }
    /**
     * 从文件中取得指定键的值
     * @param filePath
     * @param context
     * @param key
     * @return
     */
    public Object getObjectFromLocal(String filePath,Context context,String key){

        try {
            SharedPreferences preferences=context.getSharedPreferences(filePath, Activity.MODE_PRIVATE);
            String value=preferences.getString(key, "");
            return new Base64Utils().decodeFromBase64(value);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }

    }
    /**
     * 清空默认文件键值对
     * @param context
     */
    public void clearObjectLocal(Context context){
        clearObjectLocal("application", context);

    }
    /**
     * 清空文件键值对
     * @param filePath
     * @param context
     */
    public void clearObjectLocal(String filePath,Context context){

        SharedPreferences preferences=context.getSharedPreferences(filePath, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        Map<String, ?> map = preferences.getAll();
        for(Entry<String, ?> set:map.entrySet()){
            editor.remove(set.getKey());
        }
        editor.commit();

    }
    /**
     * 移除键值对
     * @param context
     * @param key
     */
    public void removeObjectLocal(Context context,String key){
        removeObjectLocal("application", context,key);
    }
    /**
     * 移除指定文件键值对
     * @param filePath
     * @param context
     * @param key
     */
    public void removeObjectLocal(String filePath,Context context,String key){

        SharedPreferences preferences=context.getSharedPreferences(filePath, Context.MODE_PRIVATE);
        Editor editor = preferences.edit();
        editor.remove(key);
        editor.commit();

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值