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();

posted @ 2014-11-16 00:17  skyyhu  阅读(3034)  评论(0编辑  收藏  举报