2019-11-15

保存数据:

第一种方法

第一步 //定义sp变量

private static final String  Temp_Sms="Temp_Sms";


第二步 保存数据在onStop方法处理

protected void onStop(){

super.onStop();

    SharedPreferences.Editor editor=getSharedPreferences(Temp_Sms,MODE_PRIVATE).edit();

        editor.putString("sms_content",editTextMsg.getText().toString());

        editor.apply();

}


第三步 取值调用展示

SharedPreferences sp = getSharedPreferences(Temp_Sms,MODE_PRIVATE);

String content=sp.getString("sms_content", "");

editTextMsg.setText(content);


第二种方法


/**

* 用户数据操作类

*/

public class UserDao {

private static StringUserDaoTable ="Jason";  //这里命名中间随意

  private static StringUserInfoObject ="UserInfoObject";

  /**

* 获得用户名

*

    * @param context

    * @return

    */

  public static StringgetUserName(Context context) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      String username = preferences.getString("UserName", "");

      return username;

  }

/**

* 获得密码

*

    * @param context

    * @return

    */

  public static StringgetPassWord(Context context) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      String password = preferences.getString("PassWord", "");

      return password;

  }

/**

* 是否记住密码

*

    * @param context

    * @return

    */

  public static boolean getRememberPassWord(Context context) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      boolean isRemember = preferences.getBoolean("RememberPassWord", false);

      return isRemember;

  }

/**

* 保存是否保存密码

*

    * @param context

    * @param username

    * @param password

    */

  public static void saveRememberPassWord(Context context, boolean isRemember) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      Editor editor = preferences.edit();

      editor.putBoolean("RememberPassWord", isRemember);

      editor.commit();

  }

/**

* 保存密码

*

    * @param context

    * @param username

    * @param password

    */

  public static void saveUserPassWord(Context context, String password) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      Editor editor = preferences.edit();

      editor.putString("PassWord", password);

      editor.commit();

  }

/**

* 保存用户名密码

*

    * @param context

    * @param username

    * @param password

    */

  public static void saveUserNamePassWord(Context context, String username, String password) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      Editor editor = preferences.edit();

      editor.putString("UserName", username);

      editor.putString("PassWord", password);

      editor.commit();

  }

// /**

//  * 保存用户信息

//  *

//  * @param context

//  * @param userinfo

//  */

// public static void saveUserInfo(Context context, String userinfo) {

//    SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

//    Editor editor = preferences.edit();

//    editor.putString("UserInfo", userinfo);

//    editor.commit();

// }

  /**

* 保存用户信息对象

*

    * @param context

    * @param userinfo

    */

  public static void saveUserInfo(Context context, LoginUser userinfo) {

PreferencesService service =new PreferencesService(context);

      service.saveObject(UserInfoObject, userinfo);

  }

/**

* 获取用户信息对象

*

    * @param context

    * @return

    */

  public static LoginUsergetUserInfoModel(Context context) {

LoginUser model =null;

      PreferencesService service =new PreferencesService(context);

      model = (LoginUser) service.deSerialization(UserInfoObject);

      return model;

  }

/**

* 获取用户信息

*

    * @param context

    * @return

    */

  public static StringgetUserInfo(Context context) {

SharedPreferences preferences = context.getSharedPreferences(UserDaoTable, Activity.MODE_PRIVATE);

      return preferences.getString("UserInfo", "");

  }

}



PreferencesService文件在下面

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.StreamCorruptedException;

import java.util.HashMap;

import java.util.Map;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.SharedPreferences.Editor;

import android.util.Base64;

/**

* 轻量级保存工具

*/

public class PreferencesService {

private static SharedPreferencespreferences;

  /** 构造器(无参数) */

  public PreferencesService() {

}

/** 构造器(有参数) */

  public PreferencesService(Context context) {

if (null ==preferences) {

preferences = android.preference.PreferenceManager.getDefaultSharedPreferences(context);

      }

}

/** 保存字段 */

  public void save(String key, String value) {

Editor edit =preferences.edit();

      /** 数据是放在内存中的 */

      edit.putString(key, value);

      /** 提交方法,把内存中的数据提交到文件中 */

      edit.commit();

  }

/** 根据key值获取value值 */

  public StringgetValue(String key, String defaltValue) {

String object =preferences.getString(key, defaltValue);

      return object;

  }

/** 保存字段 */

  public void save(String key, Boolean value) {

Editor edit =preferences.edit();

      /** 数据是放在内存中的 */

      edit.putBoolean(key, value);

      /** 提交方法,把内存中的数据提交到文件中 */

      edit.commit();

  }

/** 根据key值获取value值 */

  public boolean getValue(String key) {

boolean object =preferences.getBoolean(key, false);

      return object;

  }

/** 获取保存的字段 */

  public MapgetPreferences(String... param) {

Map map =new HashMap();

      for (int i =0; i < param.length; i++) {

map.put(param[i], preferences.getString(param[i], ""));

      }

return map;

  }

/** 将要保存的hashmap集合转化成字符串 */

  private StringSceneList2String(HashMap hashmap)throws IOException {

/** 实例化一个ByteArrayOutputStream对象,用来装载压缩后的字节文件。 */

      ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();

      /** 然后将得到的字符数据装载到ObjectOutputStream */

      ObjectOutputStream objectOutputStream =new ObjectOutputStream(byteArrayOutputStream);

      /** writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它 */

      objectOutputStream.writeObject(hashmap);

      /** 最后,用Base64.encode将字节文件转换成Base64编码保存在String中 */

      String SceneListString =new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));

      /** 关闭objectOutputStream */

      objectOutputStream.close();

      return SceneListString;

  }

/** 将字符串转化成hashmap集合 */

  private HashMapString2SceneList(String SceneListString)throws StreamCorruptedException, IOException, ClassNotFoundException {

byte[] mobileBytes = Base64.decode(SceneListString.getBytes(), Base64.DEFAULT);

      ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(mobileBytes);

      ObjectInputStream objectInputStream =new ObjectInputStream(byteArrayInputStream);

      @SuppressWarnings("unchecked")

HashMap SceneList = (HashMap) objectInputStream.readObject();

      objectInputStream.close();

      return SceneList;

  }

/** 存储集合对象 */

  public boolean putHashMap(String key, HashMap hashmap) {

Editor editor =preferences.edit();

      try {

String liststr = SceneList2String(hashmap);

        editor.putString(key, liststr);

      }catch (IOException e) {

return false;

      }

return editor.commit();

  }

/** 获取集合对象 */

  public HashMapgetHashMap(String key) {

String liststr =preferences.getString(key, "");

      try {

return String2SceneList(liststr);

      }catch (Exception e) {

return null;

      }

}

/** 反序列化对象(获取序列化对象) */

  public ObjectdeSerialization(String key) {

String seria =preferences.getString(key, null);

      Object object;

      try {

String redStr = java.net.URLDecoder.decode(seria, "UTF-8");

        ByteArrayInputStream byteArrayInputStream =new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));

        ObjectInputStream objectInputStream =new ObjectInputStream(byteArrayInputStream);

        object = objectInputStream.readObject();

        objectInputStream.close();

        byteArrayInputStream.close();

      }catch (Exception e) {

return null;

      }

return object;

  }

/** 序列化对象 */

  private Stringserialize(Object object) {

ByteArrayOutputStream byteArrayOutputStream =new ByteArrayOutputStream();

      ObjectOutputStream objectOutputStream;

      String serStr;

      try {

objectOutputStream =new ObjectOutputStream(byteArrayOutputStream);

        objectOutputStream.writeObject(object);

        serStr = byteArrayOutputStream.toString("ISO-8859-1");

        serStr = java.net.URLEncoder.encode(serStr, "UTF-8");

        objectOutputStream.close();

        byteArrayOutputStream.close();

      }catch (IOException e) {

return null;

      }

return serStr;

  }

/** 保存序列化对象 */

  public boolean saveObject(String key, Object object) {

String strObject = serialize(object);

      Editor edit =preferences.edit();

      edit.putString(key, strObject);

      return edit.commit();

  }

}


调用保存方法

boolean checked = UserDao.getRememberPassWord(this);

remember_password.setChecked(checked);

remember_password.setOnCheckedChangeListener((buttonView, isChecked) -> UserDao.saveRememberPassWord(LoginActivity.this, isChecked));

if (remember_password.isChecked()) {

UserDao.saveUserNamePassWord(LoginActivity.this, username, password);

}else {

UserDao.saveUserNamePassWord(LoginActivity.this, username, "");

}

你可能感兴趣的:(2019-11-15)