zoukankan      html  css  js  c++  java
  • 使用SharedPreference保存一些简单的信息

    package com.example.testsharedpreference;
    
    import java.util.HashMap;
    import java.util.Map;
    
    import android.content.Context;
    import android.content.SharedPreferences;
    
    public class MyPreference {
    
        private Context context;
    
        public MyPreference(Context context) {
            this.context = context;
        }
    
        /**
         * 保存信息 (保存位置:“/data/data/应用程序包/shared_prefs”)
         * @param name
         * @param pswd
         * @return
         */
        public boolean saveMessage(String name, String pswd) {
            boolean flag = false;
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    "userinfo", Context.MODE_PRIVATE);
            //对数据进行编辑
            SharedPreferences.Editor edit = sharedPreferences.edit();
            edit.putString("name", name);
            edit.putString("pswd", pswd);
            //将数据持久化到存储介质当中
            edit.commit();
            return flag;
        }
    
        /**
         * @return    以map形式返回一些存储的信息
         */
        public Map<String, String> getMessage() {
            Map<String, String> map = new HashMap<String, String>();
            SharedPreferences sharedPreferences = context.getSharedPreferences(
                    "userinfo", Context.MODE_PRIVATE);
            String name = sharedPreferences.getString("name", "");
            String pswd = sharedPreferences.getString("pswd", "");
            map.put("name", name);
            map.put("pswd", pswd);
            return map;
        }
    }
  • 相关阅读:
    css 边框添加四个角效果
    PS 怎么去掉噪点
    Packet Tracer——添加温度计
    zabbix监控mysql数据
    日常检测
    mysql的基本命令操作
    判断字典中是否有这个单词
    批量创建用户
    检测局域网ip存活状态
    spring boot gradle build:bootRepackage failed
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4307506.html
Copyright © 2011-2022 走看看