相信Android的这个最简单的存储方式大家都很熟悉了,但是有一个小小技巧,也许你没有用过,今天就跟大家分享一下,我们可以把SharedPreferences封装在一个工具类中,当我们需要写数据和读数据的时候,就可以直接通过工具类的set和get方法来完成,类似JavaBean,这样使用起来就比较方便,快捷(建议项目中使用次数比较多使用)。好了,直接看看这段简单的代码吧:

[java] view plain copy
  1. publicclassSharePreferenceUtil{
  2. privateSharedPreferencessp;
  3. privateSharedPreferences.Editoreditor;
  4. publicSharePreferenceUtil(Contextcontext,Stringfile){
  5. sp=context.getSharedPreferences(file,context.MODE_PRIVATE);
  6. editor=sp.edit();
  7. }
  8. //用户的密码
  9. publicvoidsetPasswd(Stringpasswd){
  10. editor.putString("passwd",passwd);
  11. editor.commit();
  12. }
  13. publicStringgetPasswd(){
  14. returnsp.getString("passwd","");
  15. }
  16. //用户的id,即QQ号
  17. publicvoidsetId(Stringid){
  18. editor.putString("id",id);
  19. editor.commit();
  20. }
  21. publicStringgetId(){
  22. returnsp.getString("id","");
  23. }
  24. //用户的昵称
  25. publicStringgetName(){
  26. returnsp.getString("name","");
  27. }
  28. publicvoidsetName(Stringname){
  29. editor.putString("name",name);
  30. editor.commit();
  31. }
  32. //用户的邮箱
  33. publicStringgetEmail(){
  34. returnsp.getString("email","");
  35. }
  36. publicvoidsetEmail(Stringemail){
  37. editor.putString("email",email);
  38. editor.commit();
  39. }
  40. //用户自己的头像
  41. publicIntegergetImg(){
  42. returnsp.getInt("img",0);
  43. }
  44. publicvoidsetImg(inti){
  45. editor.putInt("img",i);
  46. editor.commit();
  47. }
  48. //ip
  49. publicvoidsetIp(Stringip){
  50. editor.putString("ip",ip);
  51. editor.commit();
  52. }
  53. publicStringgetIp(){
  54. returnsp.getString("ip",Constants.SERVER_IP);
  55. }
  56. //端口
  57. publicvoidsetPort(intport){
  58. editor.putInt("port",port);
  59. editor.commit();
  60. }
  61. publicintgetPort(){
  62. returnsp.getInt("port",Constants.SERVER_PORT);
  63. }
  64. //是否在后台运行标记
  65. publicvoidsetIsStart(booleanisStart){
  66. editor.putBoolean("isStart",isStart);
  67. editor.commit();
  68. }
  69. publicbooleangetIsStart(){
  70. returnsp.getBoolean("isStart",false);
  71. }
  72. //是否第一次运行本应用
  73. publicvoidsetIsFirst(booleanisFirst){
  74. editor.putBoolean("isFirst",isFirst);
  75. editor.commit();
  76. }
  77. publicbooleangetisFirst(){
  78. returnsp.getBoolean("isFirst",true);
  79. }
  80. }


第二种方法是更加简单方便:取值时只用传入context和对应的key,就能获取到对应的value;设值时,也是传入context和对应key和value即可,非常方便实用。

[java] view plain copy
  1. publicclassPreferenceUtils{
  2. publicstaticStringgetPrefString(Contextcontext,Stringkey,
  3. finalStringdefaultValue){
  4. finalSharedPreferencessettings=PreferenceManager
  5. .getDefaultSharedPreferences(context);
  6. returnsettings.getString(key,defaultValue);
  7. }
  8. publicstaticvoidsetPrefString(Contextcontext,finalStringkey,
  9. finalStringvalue){
  10. finalSharedPreferencessettings=PreferenceManager
  11. .getDefaultSharedPreferences(context);
  12. settings.edit().putString(key,value).commit();
  13. }
  14. publicstaticbooleangetPrefBoolean(Contextcontext,finalStringkey,
  15. finalbooleandefaultValue){
  16. finalSharedPreferencessettings=PreferenceManager
  17. .getDefaultSharedPreferences(context);
  18. returnsettings.getBoolean(key,defaultValue);
  19. }
  20. publicstaticbooleanhasKey(Contextcontext,finalStringkey){
  21. returnPreferenceManager.getDefaultSharedPreferences(context).contains(
  22. key);
  23. }
  24. publicstaticvoidsetPrefBoolean(Contextcontext,finalStringkey,
  25. finalbooleanvalue){
  26. finalSharedPreferencessettings=PreferenceManager
  27. .getDefaultSharedPreferences(context);
  28. settings.edit().putBoolean(key,value).commit();
  29. }
  30. publicstaticvoidsetPrefInt(Contextcontext,finalStringkey,
  31. finalintvalue){
  32. finalSharedPreferencessettings=PreferenceManager
  33. .getDefaultSharedPreferences(context);
  34. settings.edit().putInt(key,value).commit();
  35. }
  36. publicstaticintgetPrefInt(Contextcontext,finalStringkey,
  37. finalintdefaultValue){
  38. finalSharedPreferencessettings=PreferenceManager
  39. .getDefaultSharedPreferences(context);
  40. returnsettings.getInt(key,defaultValue);
  41. }
  42. publicstaticvoidsetPrefFloat(Contextcontext,finalStringkey,
  43. finalfloatvalue){
  44. finalSharedPreferencessettings=PreferenceManager
  45. .getDefaultSharedPreferences(context);
  46. settings.edit().putFloat(key,value).commit();
  47. }
  48. publicstaticfloatgetPrefFloat(Contextcontext,finalStringkey,
  49. finalfloatdefaultValue){
  50. finalSharedPreferencessettings=PreferenceManager
  51. .getDefaultSharedPreferences(context);
  52. returnsettings.getFloat(key,defaultValue);
  53. }
  54. publicstaticvoidsetSettingLong(Contextcontext,finalStringkey,
  55. finallongvalue){
  56. finalSharedPreferencessettings=PreferenceManager
  57. .getDefaultSharedPreferences(context);
  58. settings.edit().putLong(key,value).commit();
  59. }
  60. publicstaticlonggetPrefLong(Contextcontext,finalStringkey,
  61. finallongdefaultValue){
  62. finalSharedPreferencessettings=PreferenceManager
  63. .getDefaultSharedPreferences(context);
  64. returnsettings.getLong(key,defaultValue);
  65. }
  66. publicstaticvoidclearPreference(Contextcontext,
  67. finalSharedPreferencesp){
  68. finalEditoreditor=p.edit();
  69. editor.clear();
  70. editor.commit();
  71. }
  72. }

更多相关文章

  1. Python3原生编写月份计算工具
  2. 一款常用的 Squid 日志分析工具
  3. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android中存取简单的数据
  6. 一个「Pure Android」主义者的手机桌面
  7. android - ANR keyDispatchingTimedOut
  8. Android(安卓)用户界面---XML布局
  9. Android(安卓)APP性能优化(最新总结)

随机推荐

  1. Android圆形控件
  2. Android共享元素
  3. Android最快的模拟器Genymotion试用小结
  4. Android判断程序是否第一次运行
  5. Android闹钟设置的解决方案
  6. Android中hybrid开发的基础知识
  7. android基础学习--->adapter那点事儿
  8. Android中有几种数据存储方式,每种方式有
  9. Android(安卓)ApiDemo学习(五)Animation—
  10. HTML中的javascript交互