Android 利用SharedPreferences保存与删除 用户登录数据
创建SharedPreferences对象:
SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", MODE_PRIVATE); Editor editor = sharedPreferences.edit(); editor.putString("username", “用户名”); editor.putString("userid", "用户ID"); editor.commit();
读取数据:
SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", MODE_PRIVATE);
String username = sharedPreferences.getString("username", "none");
String userid = sharedPreferences.getString("userid", "");
删除数据:
SharedPreferences sharedPreferences = context.getSharedPreferences("userInfo", MODE_PRIVATE); sharedPreferences.edit().clear().commit();
路漫漫其修远兮 吾将上下而求索