Base-Android quick development framework (II) -- SharedPreferences and androidpreferences in data storage

Source: Internet
Author: User

Base-Android quick development framework (II) -- SharedPreferences and androidpreferences in data storage

For App developers, abstraction means to present data to users in various ways and collect user data. Collect User data, including user input, touch, sensor, etc. The displayed data comes from various business systems through the network, and the user's

Input data. In this process, data storage becomes important. This is why I started my explanation. Data storage can effectively reduce the number of server accesses and user traffic, speed up App response, and even simplify application business logic. Data

This section describes two common data caching Methods: Android SharedPreferences file data storage as a chapter, and sqlite as a chapter. In addition, I will not introduce basic APIs in this series of introductions, which is suitable for children's shoes with a certain degree of foundation.

Simple and rough, start SharedPreferences. SharedPreferences is a lightweight storage class on the Android platform. It is used to save some common configurations of applications. It is saved in xml format. The overall efficiency is not particularly high.

A lot better. During xml processing, Dalvik will use the local XML Parser parsing that comes with the underlying layer. The following code is used directly.

/**
* SharedPreferences data storage tool
*
*
*/
Public class SPUtil {
Private static String PreferenceName = "Constant ";

/**
* Store complex data object fields
*
* @ Param key
* @ Param t
* @ Return
*/
Public static <T> boolean saveObjectToShare (String key, T t ){
Return saveObjectToShare (AppContext. getApplication (), PreferenceName,
Key, t );
}

/**
* Store complex data field objects
*
* @ Param context
* @ Param key
* @ Param t
* @ Return
*/
Public static <T> boolean saveObjectToShare (Context context, String key, T t ){
Return saveObjectToShare (context, PreferenceName, key, t );
}

/**
*
* @ Param context
* @ Param name
* @ Param key
* @ Param t
* @ Return
*/

Public static <T> boolean saveObjectToShare (Context context, String name,
String key, T t ){
Try {
SharedPreferences sp = context. getSharedPreferences (name,
Context. MODE_PRIVATE );
// Storage
Editor editor = sp. edit ();
If (t = null ){
Editor. putString (key ,"");
Editor. commit ();
Return true;
}
ByteArrayOutputStream toByte = new ByteArrayOutputStream ();
ObjectOutputStream oos;

Oos = new ObjectOutputStream (toByte );
Oos. writeObject (t );
// Encode byte [] with Base64
String payCityMapBase64 = new String (Base64.encode (
ToByte. toByteArray (), Base64.DEFAULT ));

Editor. putString (key, payCityMapBase64 );
Editor. commit ();
Return true;
} Catch (IOException e ){
E. printStackTrace ();
Return false;
}
}

/**
* Obtain complex data objects
*
* @ Param key
* @ Return
*/
Public static <T> T getObjectFromShare (String key ){
Return getObjectFromShare (AppContext. getApplication (), PreferenceName,
Key );
}

/**
* Obtain complex data objects
*
* @ Param context
* @ Param key
* @ Return
*/
Public static <T> T getObjectFromShare (Context context, String key ){
Return getObjectFromShare (context, PreferenceName, key );
}

/**
* Obtain complex data objects
*
* @ Param key
* @ Param object
*/
@ SuppressWarnings ("unchecked ")
Public static <T> T getObjectFromShare (Context context, String name,
String key ){
Try {
SharedPreferences sp = context. getSharedPreferences (name,
Context. MODE_PRIVATE );
String payCityMapBase64 = sp. getString (key ,"");
If (payCityMapBase64.length () = 0 ){
Return null;
}
Byte [] base64Bytes = Base64
. Decode (payCityMapBase64, Base64.DEFAULT );
ByteArrayInputStream bais = new ByteArrayInputStream (base64Bytes );
ObjectInputStream ois = new ObjectInputStream (bais );
Return (T) ois. readObject ();
} Catch (Exception e ){
E. printStackTrace ();
}
Return null;
}

/**
* String type data is returned. The default value is "".
*
* @ Param key
* @ Return
*/
Public static String getString (String key ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Return sp. getString (key ,"");
}

/**
* Stores boolean data types.
*
* @ Param key
* @ Param value
*/
Public static void saveboolean (String key, boolean value ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Editor editor = sp. edit ();
Editor. putBoolean (key, value );
Editor. commit ();
}

/**
* Return boolean data. The default value is true;
*
* @ Param key
* @ Return
*/
Public static boolean getBoolean (String key ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Return sp. getBoolean (key, false );
}

/**
* Int Data Type
*
* @ Param key
* @ Param value
*/
Public static void saveInt (String key, int value ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Editor editor = sp. edit ();
Editor. putInt (key, value );
Editor. commit ();
}

/**
* Int type data is returned. The default value is true;
*
* @ Param key
* @ Return
*/
Public static int getInt (String key ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Return sp. getInt (key, 0 );
}

/**
* Storage float data type
*
* @ Param key
* @ Param value
*/
Public static void saveFloat (String key, float value ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Editor editor = sp. edit ();
Editor. putFloat (key, value );
Editor. commit ();
}

/**
* Float data is returned. The default value is true;
*
* @ Param key
* @ Return
*/
Public static float getFloat (String key ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Return sp. getFloat (key, 0 );
}

/**
* Long Data Type
*
* @ Param key
* @ Param value
*/
Public static void saveLong (String key, long value ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Editor editor = sp. edit ();
Editor. putLong (key, value );
Editor. commit ();
}

/**
* Long data is returned. The default value is true;
*
* @ Param key
* @ Return
*/
Public static long getLong (String key ){
SharedPreferences sp = AppContext. getApplication ()
. GetSharedPreferences (PreferenceName, Context. MODE_PRIVATE );
Return sp. getLong (key, 0 );
}
}

As a tool class, SPUtil encapsulates all the basic static write and read methods of SharedPreferences. Contains int long String boolean and complex objects. You can quickly perform SharedPreferences operations. Apps are commonly used in system configurations, such as common
In the personal settings, there will be whether to allow message pushing, whether to automatically detect version update checkbox. I have tried some projects to store these settings on the server through the network, but most of them are stored locally. In addition, sometimes there are some simple caches and reads of data sets in listview, which are not required.
It is necessary to create sqlte tables and store object serial numbers directly. Sometimes it can save a lot of trouble. Next I will begin to explain how to store sqlite data. If you have any suggestions, please leave me a message.
 
 

Related Article

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.