android SharedPreferences

一、写入

SharedPreferences perference = getSharedPreferences("ServerAccess", Context.MODE_PRIVATE);
Editor editor = perference.edit();
editor.putString("name", "zj");
editor.commit();//未调用commit前,数据实际是没有存储进文件中的。 调用后,存储存储

Toast.makeText(ServerAccessActivity.this, "写入成功", Toast.LENGTH_LONG).show();

 

二、读取

SharedPreferences perference2 = getSharedPreferences("ServerAccess", Context.MODE_PRIVATE);

String returnStr = perference2.getString("name", "no");
Toast.makeText(this, returnStr, Toast.LENGTH_LONG).show();

 

 

 

你可能感兴趣的:(android SharedPreferences)