• 使用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;
        }
    }
  • 相关阅读:
    开启CTF大门
    关于windows下scapy出现log_runtime问题
    Python关于Threading暂停恢复解决办法
    angr入门之CLE
    Linux信号量
    IDApython 命令
    Array 数组对象
    随机数 random()
    四舍五入round()
    向下取整floor()
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4307506.html
Copyright © 2020-2023  润新知