Android shares a Sharedpreferences tool class for easy data saving

Source: Internet
Author: User

We usually save some data, will use the sharedpreferences, he is stored in the phone, the specific path is data/data/your package name/shared_prefs/Save the file name. XML, The use of sharedpreferences is also very simple, I wrote a Sharedpreferences tool class, and then saved here, and so on I need to save data directly from here Copy code, haha

The tool class is as follows

[Java]View Plaincopy
  1. Package com.example.shortcut;
  2. Import Android.content.Context;
  3. Import android.content.SharedPreferences;
  4. /**
  5. * Sharedpreferences of a tool class, call SetParam can save string, Integer, Boolean, Float, long type parameters
  6. * Also call GetParam to get the data stored in the phone
  7. * @author xiaanming
  8. *
  9. */
  10. Public class Sharedpreferencesutils {
  11. /** 
  12. * File name stored in the phone
  13. */
  14. private static final String file_name = "Share_date";
  15. /** 
  16. * Method of saving data, we need to get the specific type of the data to be saved, then call different save methods according to the type
  17. * @param context
  18. * @param key
  19. * @param Object
  20. */
  21. public static void SetParam (context context, String key, Object object) {
  22. String type = Object.getclass (). Getsimplename ();
  23. Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private);
  24. Sharedpreferences.editor Editor = Sp.edit ();
  25. if ("String". Equals (Type)) {
  26. Editor.putstring (Key, (String) object);
  27. }
  28. Else if ("Integer". Equals (Type)) {
  29. Editor.putint (Key, (Integer) object);
  30. }
  31. Else if ("Boolean". Equals (Type)) {
  32. Editor.putboolean (Key, (Boolean) object);
  33. }
  34. Else if ("Float". Equals (Type)) {
  35. Editor.putfloat (Key, (Float) object);
  36. }
  37. Else if ("Long". Equals (Type)) {
  38. Editor.putlong (Key, (Long) object);
  39. }
  40. Editor.commit ();
  41. }
  42. /** 
  43. * Get the method to save the data, we are based on the default value to save the specific type of data, and then call the relative method to get the values
  44. * @param context
  45. * @param key
  46. * @param defaultobject
  47. * @return
  48. */
  49. public Static Object GetParam (context context, String key, Object defaultobject) {
  50. String type = Defaultobject.getclass (). Getsimplename ();
  51. Sharedpreferences sp = context.getsharedpreferences (file_name, context.mode_private);
  52. if ("String". Equals (Type)) {
  53. return sp.getstring (Key, (String) defaultobject);
  54. }
  55. Else if ("Integer". Equals (Type)) {
  56. return Sp.getint (Key, (Integer) defaultobject);
  57. }
  58. Else if ("Boolean". Equals (Type)) {
  59. return Sp.getboolean (Key, (Boolean) defaultobject);
  60. }
  61. Else if ("Float". Equals (Type)) {
  62. return Sp.getfloat (Key, (Float) defaultobject);
  63. }
  64. Else if ("Long". Equals (Type)) {
  65. return Sp.getlong (Key, (Long) defaultobject);
  66. }
  67. return null;
  68. }
  69. }

It's also easy to use, save the data

[Java]View Plaincopy
    1. Sharedpreferencesutils.setparam (This, "String", "xiaanming");
    2. Sharedpreferencesutils.setparam (This, "int", 10);
    3. Sharedpreferencesutils.setparam (This, "Boolean", true);
    4. Sharedpreferencesutils.setparam (This, "Long", 100L);
    5. Sharedpreferencesutils.setparam (This, "float", 1.1f);


Get Data

[Java]View Plaincopy
  1. Sharedpreferencesutils.getparam (timeractivity.                                                                                         This, "String", " "); Sharedpreferencesutils.getparam (timeractivity.   This, "int", 0);
  2. Sharedpreferencesutils.getparam (timeractivity.   This, "Boolean", false);
  3. Sharedpreferencesutils.getparam (timeractivity.   This, "long", 0L);
  4. Sharedpreferencesutils.getparam (timeractivity.   This, "float", 0.0f);

is not very convenient, hope to read the article you have a little help!

Android shares a Sharedpreferences tool class for easy data saving

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.