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;
        }
    }
  • 相关阅读:
    主线程到子线程的相互切换
    IOS通过OTA部署App
    IOS应用之间调用
    静态库详解
    ObjectC的函数调用机制详解消息
    iOS6新特征:参考资料和示例汇总
    杭电acm2025
    杭电acm2051
    杭电acm1009
    杭电acm2099
  • 原文地址:https://www.cnblogs.com/Smart-Du/p/4307506.html
Copyright © 2011-2022 走看看